SlideShare una empresa de Scribd logo
1 de 30
Laravel Unit Testing
BY DR. HASSAN AMIN
VP TECHNOLOGY
COEUS SOLUTIONS
Agenda
 What is unit test ?
 Why write unit tests ?
 Excuses for not Writing Unit Tests
 When to Implement Unit Testing
Steps for Writing Unit Test
Introduction to PHPUnit and Laravel Test Helpers
Assertions
Examples
Unit Test
“Unit test checks a single
assumption about behavior of the
system”
Key Reasons for Writing Unit Tests
 Manual testing is tedious, time consuming and unreliable
 Pity the fool who does not write tests(Coding Horrors : https://blog.codinghorror.com/i-pity-the-
fool-who-doesnt-write-unit-tests/)
 Unit testing reduces number of bugs in new features
 Unit testing reduces number of bugs in existing features
 Testing Makes Development Faster
(http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html)
 Unit Testing is fun for those who love programming challenges
 Unit Tests offer best defense against folly of another programmer messing up with your code
 Unit tests act as a safety net when you change existing code
 Unit tests are best documentation of behavior of the system
Key Reasons for Writing Unit Tests
(Cont’d)
Excuses for not Writing Unit Tests
"I don't know how to write tests."
"Writing tests is too hard."
"I don't have enough time to write tests."
"Testing is not my job."
"My code is too simple for tests.”
Reference
http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html
When to Implement Unit Testing
When building new classes that contain important business logic.
When you have functionality that is used and relied upon heavily
throughout your app.
When you want to refactor some functionality that doesn’t already
have unit tests and is relatively easy to turn into testable code
without altering the existing business logic (this never happens…)
Steps for Writing Unit Test
In every test, we generally perform the following three distinct tasks:
1. Arrange : We arrange or initialize some data.
2. Execute : We execute a function to act on this data.
3. Verify : We assert or verify that the output matches what we expected.
Example
class HelperTest extends TestCase {
public function testSum() {
$data = [1,2,3]; // 1) Arrange
$result = Helper::sum($data); // 2) Act
$this->assertEquals(6, $result); // 3) Assert
}
public function testSomethingElse() {
// ...
}
}
Introduction to PHPUnit and Laravel Test
Helpers
PHPUnit is one of the oldest and most well known unit testing packages for PHP
It is primarily designed for unit testing, which means testing your code in the smallest
components possible
The purpose of this presentation is to introduce you to the basics of PHPUnit testing, using
both the default PHPUnit assertions, and the Laravel test helpers.
The aim is for you to be confident writing basic tests for your applications
Getting Started
Create a new Laravel Application :-
$ composer create-project laravel/laravel testapp
To create a new test class, we can either create a new file manually - or run the helpful Artisan
make:test command provided by Laravel.
In order to create a test class called BasicTest, we just need to run this artisan command:
$ php artisan make:test BasicTest
Laravel Testing Conventions
The convention for test classes is that they are stored within ./tests/ in your application
directory.
◦ Inside test folder, each test class is named as <name>Test.php.
◦ This format allows PHPUnit to find each test class — it will ignore anything that does not end in
Test.php.
In a new Laravel application, you will notice two files in the ./tests/ directory: ExampleTest.php
and TestCase.php.
The TestCase.php file is a bootstrap file for setting up the Laravel environment within our tests.
This allows us to use Laravel facades in tests, and provides the framework for the testing
helpers, which we will look at shortly.
Structure of Laravel Test Class
<?php
class BasicTest extends TestCase
{
public function testExample()
{
$this->assertTrue(true);
}
}
Running Tests with PHPUnit
You can run your PHPUnit tests by running the phpunit command:
$ ./vendor/bin/phpunit
If phpunit is not found, then it can easily be installed on Ubuntu using
$ sudo apt-get install phpunit
If you are sure the method name is unique you can only filter by method name :-
$ phpunit --filter {TestMethodName}
However it is safer to specify the file path/reference as well
$ phpunit --filter {TestMethodName} {FilePath}
Basic PHPUnit Assertions
 Assertion is a true/false statement for checking some assumption about
a unit
PHPUnit has 90 different types of assertions
Seven of the basic PHPUnit assertions to write tests are:
 assertTrue()
 assertFalse()
 assertEquals()
 assertNull()
 assertContains()
 assertCount()
 assertEmpty()
Basic Assertions (Cont’d)
assertTrue() and assertFalse() allow you to assert that a value equates to either true or false.
This means they are perfect for testing methods that return boolean values.
assertEquals() is used to compare the actual value of the variable to the expected value.
 assertContains() asserts that an expected value exists within the provided array,
assertCount() asserts the number of items in the array matches the specified amount,
 assertEmpty() asserts that the provided array is empty.
and so on
Example | Box Class
Checkout relevant code for Box Class over here :-
https://semaphoreci.com/community/tutorials/getting-started-with-phpunit-in-laravel
Example 1| assertTrue and assertFalse
<?php
use AppBox;
class BasicTest extends TestCase
{
public function testHasItemInBox()
{
$box = new Box(['cat', 'toy', 'torch']);
$this->assertTrue($box->has('toy'));
$this->assertFalse($box->has('ball'));
}
}
Example 2 | assertEqual and assertNull
public function testTakeOneFromTheBox()
{
$box = new Box(['torch']);
$this->assertEquals('torch', $box->takeOne());
// Null, now the box is empty
$this->assertNull($box->takeOne());
}
Example 3 | assertCount, assertContains
and assertEmpty
public function testStartsWithALetter()
{
$box = new Box(['toy', 'torch', 'ball', 'cat', 'tissue']);
$results = $box->startsWith('t');
$this->assertCount(3, $results);
$this->assertContains('toy', $results);
$this->assertContains('torch', $results);
$this->assertContains('tissue', $results);
// Empty array if passed even
$this->assertEmpty($box->startsWith('s'));
}
Laravel Test Helpers
When you are building an application that includes complex views, navigation and forms, you
will want to test these components too.
This is where Laravel's test helpers make things just as easy as unit testing simple components.
 Here’s some of the Laravel test helpers :-
visit
see
dontsee
click(‘link')
seePageIs(‘link');
Example 1 | Using Laravel Test Helpers
Code :
public function testBasicExample()
{
$this->visit('/')
->see('Laravel 5');
}
 Without any prior knowledge of how the test helpers work, we can assume it means something like
this:
when I visit / (webroot)
I should see 'Laravel 5‘
 A simple test like this may prevent you from deploying a broken application if a change somewhere
else causes the page to no longer display the right information.
Example 2 | Using Laravel Test Helpers
Test Page Content
public function testDisplaysAlpha()
{
$this->visit('/alpha')
->see('Alpha')
->dontSee('Beta');
}
 Another basic test, that visits page /alpha checks for text ‘Alpha’ and also ensures that pages
does not contain text ‘Beta’
Example 3 | Using Laravel Test Helpers
Test Page Links
public function testClickNextForBeta()
{
$this->visit('/alpha')
->click('Next')
->seePageIs('/beta');
}
 Another great way to test is to visit a page, click on specific links and check loading of
corresponding page !
Example 4 | Using Laravel Test Helpers
Test Database
public function testDatabase()
{
// Make call to application...
$this->seeInDatabase('users', ['name'=>'john','email' => 'john@clivern.com']);
}
 Laravel also provides a variety of helpful tools to make it easier to test your database driven
applications.
 As seen above, you may use the seeInDatabase helper to assert that data exists in the database
matching a given set of criteria.
Here data is being checked in user table with columns name and email
Example 5 | Using Laravel Test Helpers
Test Session
public function testSession()
{
$this->withSession(['foo' => 'bar']);
$this->assertSessionHas('foo','bar');
$this->assertSessionMissing('foo1');
}
Example 6 | Using Laravel Test Helpers
Test Model
 Let write unit test for our Cat model !
class Cat extends Model
{
//
//
public function setAgeAttribute($age)
{
$this->attributes['age'] = $age * 7;
}
}
Example 6 | Using Laravel Test Helpers
Test Model (Cont’d)
public function testAutomaticallyCompensatesForCatYears()
{
$cat = new Cat;
$cat->age = 6;
$this->assertEquals(42, $cat->age); // true
}
References
https://laravel.com/docs/5.2/testing#sessions-and-authentication
https://semaphoreci.com/community/tutorials/getting-started-with-phpunit-in-laravel
Questions

Más contenido relacionado

La actualidad más candente

Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Simplilearn
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
Denis Bazhin
 

La actualidad más candente (20)

Code coverage
Code coverageCode coverage
Code coverage
 
What is JUnit? | Edureka
What is JUnit? | EdurekaWhat is JUnit? | Edureka
What is JUnit? | Edureka
 
QSpiders - Automation using Selenium
QSpiders - Automation using SeleniumQSpiders - Automation using Selenium
QSpiders - Automation using Selenium
 
REST API testing with SpecFlow
REST API testing with SpecFlowREST API testing with SpecFlow
REST API testing with SpecFlow
 
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
Selenium WebDriver Tutorial | Selenium WebDriver Tutorial For Beginner | Sele...
 
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
Selenium Training | TestNG Framework For Selenium | Selenium Tutorial For Beg...
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
SonarQube Presentation.pptx
SonarQube Presentation.pptxSonarQube Presentation.pptx
SonarQube Presentation.pptx
 
TestNG with selenium
TestNG with seleniumTestNG with selenium
TestNG with selenium
 
Selenium with java
Selenium with javaSelenium with java
Selenium with java
 
Testing with Spring: An Introduction
Testing with Spring: An IntroductionTesting with Spring: An Introduction
Testing with Spring: An Introduction
 
Unit Testing in Python
Unit Testing in PythonUnit Testing in Python
Unit Testing in Python
 
Automation - web testing with selenium
Automation - web testing with seleniumAutomation - web testing with selenium
Automation - web testing with selenium
 
Selenium WebDriver
Selenium WebDriverSelenium WebDriver
Selenium WebDriver
 
Continuous Quality with Postman
Continuous Quality with PostmanContinuous Quality with Postman
Continuous Quality with Postman
 
BDD Approach with Karate Framework in Service Tests
BDD Approach with Karate Framework in Service TestsBDD Approach with Karate Framework in Service Tests
BDD Approach with Karate Framework in Service Tests
 
Introduction to APIs & how to automate APIs testing with selenium web driver?
Introduction to APIs & how to automate APIs testing with selenium web driver?Introduction to APIs & how to automate APIs testing with selenium web driver?
Introduction to APIs & how to automate APIs testing with selenium web driver?
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in Python
 
TestNG introduction
TestNG introductionTestNG introduction
TestNG introduction
 
API_Testing_with_Postman
API_Testing_with_PostmanAPI_Testing_with_Postman
API_Testing_with_Postman
 

Similar a Laravel Unit Testing

Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
varuntaliyan
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
Yi-Huan Chan
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest module
PyCon Italia
 

Similar a Laravel Unit Testing (20)

Unit testing for WordPress
Unit testing for WordPressUnit testing for WordPress
Unit testing for WordPress
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Pengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDDPengenalan Unit Testing dan TDD
Pengenalan Unit Testing dan TDD
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
Fighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnitFighting Fear-Driven-Development With PHPUnit
Fighting Fear-Driven-Development With PHPUnit
 
Presentation
PresentationPresentation
Presentation
 
PHPUnit best practices presentation
PHPUnit best practices presentationPHPUnit best practices presentation
PHPUnit best practices presentation
 
Testing in Laravel
Testing in LaravelTesting in Laravel
Testing in Laravel
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
L2624 labriola
L2624 labriolaL2624 labriola
L2624 labriola
 
New and improved: Coming changes to the unittest module
 	 New and improved: Coming changes to the unittest module 	 New and improved: Coming changes to the unittest module
New and improved: Coming changes to the unittest module
 
We Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End DevelopmentWe Are All Testers Now: The Testing Pyramid and Front-End Development
We Are All Testers Now: The Testing Pyramid and Front-End Development
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnit
 
TEST AUTOMATION: AKA QUALITY CONTROL
TEST AUTOMATION: AKA QUALITY CONTROLTEST AUTOMATION: AKA QUALITY CONTROL
TEST AUTOMATION: AKA QUALITY CONTROL
 
TEST AUTOMATION: AKA QUALITY CONTROL
TEST AUTOMATION: AKA QUALITY CONTROLTEST AUTOMATION: AKA QUALITY CONTROL
TEST AUTOMATION: AKA QUALITY CONTROL
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 

Más de Dr. Syed Hassan Amin

An OCR System for recognition of Urdu text in Nastaliq Font
An OCR System for recognition of Urdu text in Nastaliq FontAn OCR System for recognition of Urdu text in Nastaliq Font
An OCR System for recognition of Urdu text in Nastaliq Font
Dr. Syed Hassan Amin
 

Más de Dr. Syed Hassan Amin (12)

Greenplum versus redshift and actian vectorwise comparison
Greenplum versus redshift and actian vectorwise comparisonGreenplum versus redshift and actian vectorwise comparison
Greenplum versus redshift and actian vectorwise comparison
 
Introduction To Docker
Introduction To  DockerIntroduction To  Docker
Introduction To Docker
 
Multitier holistic Approach for urdu Nastaliq Recognition
Multitier holistic Approach for urdu Nastaliq RecognitionMultitier holistic Approach for urdu Nastaliq Recognition
Multitier holistic Approach for urdu Nastaliq Recognition
 
Understandig PCA and LDA
Understandig PCA and LDAUnderstandig PCA and LDA
Understandig PCA and LDA
 
Agile Scrum Methodology
Agile Scrum MethodologyAgile Scrum Methodology
Agile Scrum Methodology
 
Thin Controllers Fat Models - How to Write Better Code
Thin Controllers Fat Models - How to Write Better CodeThin Controllers Fat Models - How to Write Better Code
Thin Controllers Fat Models - How to Write Better Code
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Software Project Management Tips and Tricks
Software Project Management Tips and TricksSoftware Project Management Tips and Tricks
Software Project Management Tips and Tricks
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Learning Technology Leadership from Steve Jobs
Learning Technology Leadership from Steve JobsLearning Technology Leadership from Steve Jobs
Learning Technology Leadership from Steve Jobs
 
Understanding and Managing Technical Debt
Understanding and Managing Technical DebtUnderstanding and Managing Technical Debt
Understanding and Managing Technical Debt
 
An OCR System for recognition of Urdu text in Nastaliq Font
An OCR System for recognition of Urdu text in Nastaliq FontAn OCR System for recognition of Urdu text in Nastaliq Font
An OCR System for recognition of Urdu text in Nastaliq Font
 

Laravel Unit Testing

  • 1. Laravel Unit Testing BY DR. HASSAN AMIN VP TECHNOLOGY COEUS SOLUTIONS
  • 2. Agenda  What is unit test ?  Why write unit tests ?  Excuses for not Writing Unit Tests  When to Implement Unit Testing Steps for Writing Unit Test Introduction to PHPUnit and Laravel Test Helpers Assertions Examples
  • 3. Unit Test “Unit test checks a single assumption about behavior of the system”
  • 4. Key Reasons for Writing Unit Tests  Manual testing is tedious, time consuming and unreliable  Pity the fool who does not write tests(Coding Horrors : https://blog.codinghorror.com/i-pity-the- fool-who-doesnt-write-unit-tests/)  Unit testing reduces number of bugs in new features  Unit testing reduces number of bugs in existing features  Testing Makes Development Faster (http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html)  Unit Testing is fun for those who love programming challenges  Unit Tests offer best defense against folly of another programmer messing up with your code  Unit tests act as a safety net when you change existing code  Unit tests are best documentation of behavior of the system
  • 5. Key Reasons for Writing Unit Tests (Cont’d)
  • 6. Excuses for not Writing Unit Tests "I don't know how to write tests." "Writing tests is too hard." "I don't have enough time to write tests." "Testing is not my job." "My code is too simple for tests.” Reference http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html
  • 7. When to Implement Unit Testing When building new classes that contain important business logic. When you have functionality that is used and relied upon heavily throughout your app. When you want to refactor some functionality that doesn’t already have unit tests and is relatively easy to turn into testable code without altering the existing business logic (this never happens…)
  • 8. Steps for Writing Unit Test In every test, we generally perform the following three distinct tasks: 1. Arrange : We arrange or initialize some data. 2. Execute : We execute a function to act on this data. 3. Verify : We assert or verify that the output matches what we expected.
  • 9. Example class HelperTest extends TestCase { public function testSum() { $data = [1,2,3]; // 1) Arrange $result = Helper::sum($data); // 2) Act $this->assertEquals(6, $result); // 3) Assert } public function testSomethingElse() { // ... } }
  • 10. Introduction to PHPUnit and Laravel Test Helpers PHPUnit is one of the oldest and most well known unit testing packages for PHP It is primarily designed for unit testing, which means testing your code in the smallest components possible The purpose of this presentation is to introduce you to the basics of PHPUnit testing, using both the default PHPUnit assertions, and the Laravel test helpers. The aim is for you to be confident writing basic tests for your applications
  • 11. Getting Started Create a new Laravel Application :- $ composer create-project laravel/laravel testapp To create a new test class, we can either create a new file manually - or run the helpful Artisan make:test command provided by Laravel. In order to create a test class called BasicTest, we just need to run this artisan command: $ php artisan make:test BasicTest
  • 12. Laravel Testing Conventions The convention for test classes is that they are stored within ./tests/ in your application directory. ◦ Inside test folder, each test class is named as <name>Test.php. ◦ This format allows PHPUnit to find each test class — it will ignore anything that does not end in Test.php. In a new Laravel application, you will notice two files in the ./tests/ directory: ExampleTest.php and TestCase.php. The TestCase.php file is a bootstrap file for setting up the Laravel environment within our tests. This allows us to use Laravel facades in tests, and provides the framework for the testing helpers, which we will look at shortly.
  • 13. Structure of Laravel Test Class <?php class BasicTest extends TestCase { public function testExample() { $this->assertTrue(true); } }
  • 14. Running Tests with PHPUnit You can run your PHPUnit tests by running the phpunit command: $ ./vendor/bin/phpunit If phpunit is not found, then it can easily be installed on Ubuntu using $ sudo apt-get install phpunit If you are sure the method name is unique you can only filter by method name :- $ phpunit --filter {TestMethodName} However it is safer to specify the file path/reference as well $ phpunit --filter {TestMethodName} {FilePath}
  • 15. Basic PHPUnit Assertions  Assertion is a true/false statement for checking some assumption about a unit PHPUnit has 90 different types of assertions Seven of the basic PHPUnit assertions to write tests are:  assertTrue()  assertFalse()  assertEquals()  assertNull()  assertContains()  assertCount()  assertEmpty()
  • 16. Basic Assertions (Cont’d) assertTrue() and assertFalse() allow you to assert that a value equates to either true or false. This means they are perfect for testing methods that return boolean values. assertEquals() is used to compare the actual value of the variable to the expected value.  assertContains() asserts that an expected value exists within the provided array, assertCount() asserts the number of items in the array matches the specified amount,  assertEmpty() asserts that the provided array is empty. and so on
  • 17. Example | Box Class Checkout relevant code for Box Class over here :- https://semaphoreci.com/community/tutorials/getting-started-with-phpunit-in-laravel
  • 18. Example 1| assertTrue and assertFalse <?php use AppBox; class BasicTest extends TestCase { public function testHasItemInBox() { $box = new Box(['cat', 'toy', 'torch']); $this->assertTrue($box->has('toy')); $this->assertFalse($box->has('ball')); } }
  • 19. Example 2 | assertEqual and assertNull public function testTakeOneFromTheBox() { $box = new Box(['torch']); $this->assertEquals('torch', $box->takeOne()); // Null, now the box is empty $this->assertNull($box->takeOne()); }
  • 20. Example 3 | assertCount, assertContains and assertEmpty public function testStartsWithALetter() { $box = new Box(['toy', 'torch', 'ball', 'cat', 'tissue']); $results = $box->startsWith('t'); $this->assertCount(3, $results); $this->assertContains('toy', $results); $this->assertContains('torch', $results); $this->assertContains('tissue', $results); // Empty array if passed even $this->assertEmpty($box->startsWith('s')); }
  • 21. Laravel Test Helpers When you are building an application that includes complex views, navigation and forms, you will want to test these components too. This is where Laravel's test helpers make things just as easy as unit testing simple components.  Here’s some of the Laravel test helpers :- visit see dontsee click(‘link') seePageIs(‘link');
  • 22. Example 1 | Using Laravel Test Helpers Code : public function testBasicExample() { $this->visit('/') ->see('Laravel 5'); }  Without any prior knowledge of how the test helpers work, we can assume it means something like this: when I visit / (webroot) I should see 'Laravel 5‘  A simple test like this may prevent you from deploying a broken application if a change somewhere else causes the page to no longer display the right information.
  • 23. Example 2 | Using Laravel Test Helpers Test Page Content public function testDisplaysAlpha() { $this->visit('/alpha') ->see('Alpha') ->dontSee('Beta'); }  Another basic test, that visits page /alpha checks for text ‘Alpha’ and also ensures that pages does not contain text ‘Beta’
  • 24. Example 3 | Using Laravel Test Helpers Test Page Links public function testClickNextForBeta() { $this->visit('/alpha') ->click('Next') ->seePageIs('/beta'); }  Another great way to test is to visit a page, click on specific links and check loading of corresponding page !
  • 25. Example 4 | Using Laravel Test Helpers Test Database public function testDatabase() { // Make call to application... $this->seeInDatabase('users', ['name'=>'john','email' => 'john@clivern.com']); }  Laravel also provides a variety of helpful tools to make it easier to test your database driven applications.  As seen above, you may use the seeInDatabase helper to assert that data exists in the database matching a given set of criteria. Here data is being checked in user table with columns name and email
  • 26. Example 5 | Using Laravel Test Helpers Test Session public function testSession() { $this->withSession(['foo' => 'bar']); $this->assertSessionHas('foo','bar'); $this->assertSessionMissing('foo1'); }
  • 27. Example 6 | Using Laravel Test Helpers Test Model  Let write unit test for our Cat model ! class Cat extends Model { // // public function setAgeAttribute($age) { $this->attributes['age'] = $age * 7; } }
  • 28. Example 6 | Using Laravel Test Helpers Test Model (Cont’d) public function testAutomaticallyCompensatesForCatYears() { $cat = new Cat; $cat->age = 6; $this->assertEquals(42, $cat->age); // true }

Notas del editor

  1. The most important thing to notice here is the test prefix on the method name. Like the Test suffix for class names, this test prefix tells PHPUnit what methods to run when testing. If you forget the test prefix, then PHPUnit will ignore the method.