![Page 1: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/1.jpg)
TEST YOUR LARAVEL 4APPLICATIONS
Presented by / Milan Popović @komita1981
![Page 2: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/2.jpg)
MEPHP developer
7 years in PHP development
Work for 12 Mnkys
I like to learn and share knowledge
Active member of PHP Srbija
![Page 3: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/3.jpg)
YOU?Who are you?
What's your experience in programming?
What's your experience in testing?
What are your expectations?
![Page 4: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/4.jpg)
WHAT IS TESTING?Testing is the activity of finding out whether a piece of code
produces the intended behavior
![Page 5: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/5.jpg)
WHAT DO PEOPLE THINK ABOUTTESTING?
Who has time to write tests???
Testing is hard
Steep learning curve
Time consuming
Don’t make anybody any money.
![Page 6: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/6.jpg)
![Page 7: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/7.jpg)
![Page 8: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/8.jpg)
![Page 9: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/9.jpg)
Prove you've done your work
Help you check much faster if you're work is done
Protect you from breaking things - regression bugs
Help you make better code design - easier to maintain
Let you apply changes with less worries - refactoring willimprove things without breaking anything
Ensures stable, long lasting application
Enhance security
Free documentation
![Page 10: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/10.jpg)
IBM & Microsoft - TDD - 20-40% longer to complete but 40-90% fewer bugs in production
![Page 11: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/11.jpg)
By 2022 it will be not be possible to get a professionalprogramming job if you do not practice TDD routinely
![Page 12: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/12.jpg)
TEST TYPESAcceptance (end-to-end)
Test as if end user would use the whole system/feature
IntegrationTest how different parts of system work together
UnitTest single unit of code - mock all dependencies
![Page 13: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/13.jpg)
WHAT WE NEED TO DEFINEWhat is unit testing?
What is to be tested?
What does not need to be tested?
Unit testing FIRST principles
Writing unit testing before or after code
![Page 14: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/14.jpg)
WHAT IS UNIT TESTING?A software testing method to test individual unit of source
code - a method in class or a piece of code
![Page 15: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/15.jpg)
What is to be tested?
EVERYTHING
![Page 16: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/16.jpg)
WHAT DOES NOT NEED TO BETESTED?Getters/Setters
Framework
Third party packages
![Page 17: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/17.jpg)
UNIT TESTING FIRST PRINCIPLESFast - Be fast or be dead
Isolation - Run without dependency
Repeatable - Should be idempotent
Self-verifyng - Just pass or fail
Timely - Code change requires new test
![Page 18: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/18.jpg)
WRITE UNIT TESTS BEFORE ORAFTER CODE?
After - You test what you code - Test Last - DDT
Before - You code what you test - Test First - TDD
![Page 19: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/19.jpg)
![Page 20: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/20.jpg)
![Page 21: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/21.jpg)
![Page 22: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/22.jpg)
PHPUNITMember of xUnit family
Created by Sebastian Bergmann
Integrated/Supported by all modern frameworks
Integrated in most IDE (PHPStorm, Netbeans, Eclipse, ZendStudio)
![Page 23: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/23.jpg)
Written for PHP 5.x
Install using composer or phar"require-dev": { "phpunit/phpunit": "4.2.*"},
PEAR install - not supported from 1.1.2015
![Page 24: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/24.jpg)
PHPUNIT'S TEST GOALSEasy to learn to write
Easy to write
Easy to read
Easy to execute
Quick to execute
Isolated
Composable
![Page 25: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/25.jpg)
TEST CONFIG & STRUCTUREConfigured in phpunit.xml file
The tests for a class Class go into a class ClassTest
ClassTest inherits (most of the time) fromPHPUnit_Framework_TestCase
The tests are public methods that are named test*
Inside the test methods assertion methods are used - assert*
![Page 26: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/26.jpg)
<phpunit backupglobals="false" backupstaticattributes="false" bootstrap="bootstrap/autoload.php" <testsuites> <testsuite name="Libraries"> <directory>./app/tests/Unit/Libraries/</directory> </testsuite> <testsuite name="Unit"> <directory>./app/tests/Unit/</directory> </testsuite> <testsuite name="Integration"> <directory>./app/tests/Integration/</directory> </testsuite> <testsuite name="RunAll"> <directory>./app/tests/</directory> </testsuite> </testsuites></phpunit>
![Page 27: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/27.jpg)
ASSERTION METHODS EXAMPLESAssertTrue - Check the input to verify it equals true
AssertFalse - Check the input to verify it equals false
AssertEquals - Check the result against another input for amatch
AssertContains - Check that the input contains a certainvalue
Many more asserts - official documentation...
![Page 28: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/28.jpg)
class Calculator{ public function add($a, $b) { return $a + $b; }}
![Page 29: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/29.jpg)
class CalculatorTest extends PHPUnit_Framework_TestCase{ protected $calculator;
public function setUp() { $this->calculator = new Calculator(); parent::setUp(); }
public function testSuccessfulAdd() { $this->assertEquals(9, $this->calculator->add(3, 6)); }
public function tearDown() { parent::tearDown(); }}
![Page 30: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/30.jpg)
![Page 31: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/31.jpg)
/*** @dataProvider getSuccessfulAddData*/public function testSuccessfulAdd($data){ $this->assertEquals($data['result'], $this->calculator->add($data['a'], $data['b']));}
public function getSuccessfulAddData(){ return array( array( array('a' => 1, 'b' => 2, 'result' => 3), array('a' => 2, 'b' => 1, 'result' => 3), array('a' => 0, 'b' => 1, 'result' => 1), array('a' => 1, 'b' => 0, 'result' => 1), ) );}
![Page 32: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/32.jpg)
/*** @dataProvider getUnsuccessfulAddData*/public function testUnsuccessfulAdd($data){ $this->setExpectedException($data['expectedException']); $this->calculator->add($data['a'], $data['b']);}
public function getUnsuccessfulAddData(){ return array( array( array('a' => 'string', 'b' => 2, 'result' => 2, 'expectedException' array('a' => 2, 'b' => 'string', 'expectedException' => 'Exception' array('a' => 'string', 'b' => 'string', 'expectedException' => 'Exception' ) );}
![Page 33: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/33.jpg)
![Page 34: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/34.jpg)
class Calculator{ public function add($a, $b) { if (! is_int($a) or ! is_int($b)){ throw new Exception('Only integers allowed'); }
return $a + $b; }}
![Page 35: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/35.jpg)
![Page 36: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/36.jpg)
class ScientificCalculator{ public function complex($a) { return "Too complex $a"; }}
class Calculator{ public function complex($a) { $scientificCalculator = new ScientificCalculator();
return $scientificCalculator->complex($a); }}
![Page 37: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/37.jpg)
![Page 38: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/38.jpg)
class ScientificCalculator{ public function complex($a) { return "Too complex $a"; }}
class Calculator{ protected $scientificCalculator;
public function __construct(ScientificCalculator $scientificCalculator) { $this->scientificCalculator = $scientificCalculator; }
public function complex($a) { return $this->scientificCalculator->complex($a); }}
![Page 39: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/39.jpg)
class CalculatorTest extends PHPUnit_Framework_TestCase{ protected $calculator;
public function setUp() { $this->calculator = new Calculator(new ScientificCalculator()); parent::setUp(); } ...
![Page 40: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/40.jpg)
![Page 41: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/41.jpg)
class CalculatorTest extends PHPUnit_Framework_TestCase{ protected $calculator; protected $scientificCalculatorMock;
public function setUp() { $this->scientificCalculatorMock = $this->getMockBuilder('ScientificCalculator' $this->calculator = new Calculator($this->scientificCalculatorMock); parent::setUp(); }
public function testSuccessfulComplex(){ $a = 1; $returnValue = "Something";
$this->scientificCalculatorMock ->expects($this->once()) ->method('complex') ->with($this->equalTo($a)) ->will($this->returnValue($returnValue));
![Page 42: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/42.jpg)
![Page 43: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/43.jpg)
MOCKERYSimple, powerful framework for creating Mock Objects
Install through Composer"require-dev": { "mockery/mockery": "0.9.*@dev"},
![Page 44: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/44.jpg)
MOCK METHOD$mockedObject = Mockery::mock('\Show\ExampleClass');
Partial mocks$mockedObject = Mockery::mock('\Show\ExampleClass[save, send]');
$mockedObject = Mockery::mock('\Show\ExampleClass')->makePartial();
![Page 45: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/45.jpg)
SHOULD RECEIVE$this->mock->shouldReceive('methodName');
![Page 46: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/46.jpg)
ONCE, TWICE, TIMES(N), NEVER$this->mock ->shouldReceive('methodName') ->once();
$this->mock ->shouldReceive('methodName') ->never();
![Page 47: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/47.jpg)
WITH$this->mock ->shouldReceive('methodName') ->once() ->with($params);
![Page 48: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/48.jpg)
AND RETURN$mockedObject = Mockery::mock('\Show\ExampleClass');$mockedObject ->shouldReceive('all') ->once() ->with($param) ->andReturn('foo');
![Page 49: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/49.jpg)
TESTING IN LARAVEL 4Built with unit testing in mind
Utilizes the Symfony HttpKernel, DomCrawler, andBrowserKit components
All tests inherit app/tests/TestCase.php file
![Page 50: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/50.jpg)
class TestCase extends Illuminate\Foundation\Testing\TestCase { /** * Creates the application. * * @return \Symfony\Component\HttpKernel\HttpKernelInterface */ public function createApplication() { $unitTesting = true; $testEnvironment = 'testing'; return require __DIR__.'/../../bootstrap/start.php'; }}
![Page 51: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/51.jpg)
abstract class TestCase extends \PHPUnit_Framework_TestCase {
use ApplicationTrait, AssertionsTrait; ...
![Page 52: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/52.jpg)
ApplicationTrait
cal(...) - Call the given URI and return the Response
action(...) - Call a controller action and return the Response
route(...) - Call a named route and return the Response
seed($class = 'DatabaseSeeder') - Seed a given databaseconnection
be(...) - Set the currently logged in user for the application
![Page 53: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/53.jpg)
AssertionsTrait
assertResponseOk() - Assert that the client response has anOK status code
assertResponseStatus($code) - Assert that the clientresponse has a given code
assertViewHas($key, $value = null) - Assert that the responseview has a given piece of bound data
assertRedirectedTo($uri, $with = array()) - Assert whetherthe client was redirected to a given URI
![Page 54: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/54.jpg)
MOCKING FACADESpublic function crypt(){ Crypt::setKey('someKey');}
public function testCrypt(){ Crypt::shouldReceive('setKey')->once()->with($key);}
shouldReceive method called on the facade return aninstance of a Mockery mock
![Page 55: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/55.jpg)
public function __construct(User $userModel){ $this->userModel = $userModel;}public function store(){ $userData = Input::all();
if(! $this->userModel->validate($userData)){ throw new StoreResourceFailedException($this->userModel->getErrors()); }
try{ $userModel = $this->userModel->create($userData);
if (! $userModel instanceof User){ throw new StoreResourceFailedException(); }
return $userModel; }catch (Exception $e){ throw new StoreResourceFailedException('Failed'); }
![Page 56: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/56.jpg)
use Mockery as m;
class UserControllerTest extends TestCase{ public function setUp() { parent::setUp(); $this->userModel = m::mock('Eloquent', 'Api\Models\User'); }
public function tearDown() { parent::tearDown(); m::close(); } ...
![Page 57: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/57.jpg)
public function testSuccessfulStore(){ $data = ['first_name' => 'John', 'last_name' => 'Doe', 'email' => '[email protected]'
$this->userModel ->shouldReceive('validate') ->once() ->with($data) ->andReturn(true);
$mockedUserModel = m::mock('Eloquent', 'Api\Models\User');
$this->userModel ->shouldReceive('create') ->once() ->andReturn($mockedUserModel);
$this->app->instance('Models\User', $this->userModel);
$response = $this->call('POST', $this->createUri('users'));
$this->assertResponseOk(); $this->assertContains(json_encode($data), $response->getContent());
![Page 58: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/58.jpg)
public function testUnsuccessfulStore(){ $data = ['first_name' => 'John', 'last_name' => 'Doe']; $errorMessage = 'Error';
$this->userModel ->shouldReceive('validate') ->once() ->with($data) ->andReturn(false);
$this->userModel ->shouldReceive('getErrors') ->once() ->andReturn($errorMessage);
$this->userModel ->shouldReceive('create') ->never();
$this->app->instance('Models\User', $this->userModel);
$response = $this->call('POST', $this->createUri('users'));
![Page 59: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/59.jpg)
PROGRAMMING SINSNew Operators - new ClassName()
Using statics - SomeClass::someMethod()
Endless "anding" - Break SRP
![Page 60: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/60.jpg)
PROGRAMMING SINSLogic in constructor - only assign variables
Using switch-case often - use design patterns instead
Too many dependencies - max 4 dependencies
![Page 61: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/61.jpg)
ADVICESTry with writing tests after writing the code
Do not make tests complex
Do not duplicate test code
Treat test like your code
When you get confidence try to write tests before the code
![Page 62: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/62.jpg)
ADVICESRun tests often - after every change and before any commit
Test before refactoring
Use continuous integration server
![Page 63: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/63.jpg)
![Page 64: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/64.jpg)
![Page 65: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/65.jpg)
http://images.sodahead.com/polls/001599249/4445136897_Question_Mark_000007651615XSmall_answer_101_xlarge.jpeghttps://lh4.ggpht.com/W3DVtNTVIAvZfJ99kDT2hP5cxklxZfLMGehfox8WFCzVk49LOUW-exgVXifFxDKkPts=w300http://www.redbubble.com/people/fitbys/works/10613559-results-or-excuses-fitness-slogan-retro?p=art-printhttp://www.slideshare.net/damiansromek/php-tests-tipshttp://www.slideshare.net/ChonlasithJucksripor/unit-test-39809217http://blog.typemock.com/2009/03/the-cost-of-test-driven-development.htmlhttp://lh3.ggpht.com/-X8LPVvE5BYE/UHaLknLmMmI/AAAAAAAABg0/HDu3iZAs2yg/s1600-h/image7.pnghttp://lh5.ggpht.com/-jDpF-eS-6TE/UQo_mozEkkI/AAAAAAAAHUg/uu9F08Atq9I/wrong_thumb%25255B39%25255D.jpg?imgmax=800
![Page 66: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/66.jpg)
THANK YOU
![Page 67: TEST YOUR LARAVEL 4 APPLICATIONS - Razvoj · PDF fileTEST YOUR LARAVEL 4 APPLICATIONS Presented by Milan Popović / @komita1981. ME PHP developer 7 years in PHP development Work for](https://reader030.vdocument.in/reader030/viewer/2022021504/5aaf9b537f8b9a07498d92ce/html5/thumbnails/67.jpg)
QUESTIONS?