This is an excerpt of my upcoming ebook Laravel Testing 101 . If you haven’t read the previous chapters yet (available for free here: Adding Tests to your Laravel CRUD Application: Where to Start? and here: What should we be testing in a [laravel] CRUD application? ), please do so before reading this one.

Now that we have a better idea about the functionalities we should be testing in our Laravel application, let’s start with testing what a guest can do, since it is less complicated than what a logged in user can do.

As we discussed I the previous chapter , here are the functionalities related to guests that we have in the application:

/articles Make sure thatphpUnit is working properly with your application

Before we start writing any tests, let’s make sure that PHPUnit is working properly with your application.

The binary of PHPUnit is included in vendor/bin/phpunit in your project, so all you need to do is to execute it (from your project directory).

You should see this result:

[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

Even though we haven’t written any tests yet, Laravel includes the following example tests:

/tests/Feature/ExampleTest.php /tests/Unit/ExampleTest.php

P.S.: I recommend adding an alias for the command above, so you would not need to type vendor/bin/phpunit each time you want to run the tests

For example, I’m using this alias:

alias lphpunit="vendor/bin/phpunit"

1/ A guest could see all the articles when visiting /articles

Since we are going to test functionalities related to the ArticleController , let’s first create a class dedicated to this controller.

php artisan make:test ArticleControllerTest

[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

Note that I didn’t pass the --unit flag to the command, which mean we are not creating a unit test but rather a feature test . The newly created class should be located in /tests/Feature/ArticleControllerTest.php

You can get rid of the testExample that was included with the ArticleControllerTest .

Let’s create our first tests.

When executing PHPUnit, it will look for all the public method that either start with test or that have @test in their dockblock.

So you could either use this format:

public function testGuestCouldSeeListOfArticles() { ... }

or this one:

/** * @test */ public function it_allows_anyone_to_see_list_all_articles() { ... }

I prefer the second one since it is much easier to read.

I’ll start with the most basic test. I just want to make sure whenever I hit the /articles route, I get a valid page back.

/** * @test */ public function it_allows_anyone_to_see_list_all_articles() { $response = $this->get(route('get_all_articles')); $response->assertSuccessful(); }

Save the file and run PHPUnit (either using vendor/bin/phpunit or with the lphpunit alias we created earlier).

[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

Our tests passed

Note that, even though we wrote just one test and one assertion PHPUnit tells us that we have 3 tests and 3 assertions.

Note: An assertion is testing one single “thing”,

a test could

contain multiple assertions

. The test we wrote above contains just a

single assertion


The reason behind this, is that PHPUnit will run all the tests in the /tests directory. Running all the tests each time is not a problem at the stage we are in right now, but if you want to run just a single test, you can pass the name of the test (the name of the method) as a parameter to the --filter flag like this:

lphpunit --filter=it_allows_anyone_to_see_list_all_articles

[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

… and yes, as you might have guessed it you could add an alias for this command in order to save time next time you want to run just a single test.

alias lphpunit="vendor/bin/phpunit" alias lphpunitf="lphpunit --filter="

lphpunitf it_allows_anyone_to_see_list_all_articles

[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

As you can tell, we want to test more than just getting a valid page, we want to make sure that we are getting the right page.

We can test this with the following steps:

make sure that we are getting the right view make sure that the view contains the variables needed for this page

Laravel provides two method to test the above:

$response->assertViewIs('articles.index'); $response->assertViewHas('articles');

Our test class should now look like this:

<?php namespace Tests\Feature; use Tests\TestCase; use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\RefreshDatabase; class ArticleControllerTest extends TestCase { /** * @test */ public function it_allows_anyone_to_see_list_all_article() { $response = $this->get(route('get_all_articles')); $response->assertSuccessful(); $response->assertViewIs('articles.index'); $response->assertViewHas('articles'); } }
[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel  ...

Now that we can test that we are getting the right view (with the right variable), we no longer need to keep the first assertion, since it is implicit.

2/ A guest could see a singlearticle

Now that we tested that a guest user could see the list of all articles, let’s make sure that she could view individual articles as well.

In order to ensure that this functionality (showing individual articles to guest users) works as expected, we would need the following steps:

GET articles.view $article

Our test should look like this:

/** * @test */ public function it_allows_anyone_to_see_individual_articles() { $article = Article::get()->random(); $response = $this->get(route('view_article', ['id' => $article->id])); $response->assertViewIs('articles.view'); $response->assertViewHas('article'); $returnedArticle = $response->original->article; $this->assertEquals($article->id, $returnedArticle->id, "The returned article is different from the one we requested"); }


We can access the returned

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

本文标题:[Laravel Testing 101] Writing tests for guest user functionalities on a Laravel ...

技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(20)