PHPUnit your bug exterminator

26
Follow this topic: @rjsmelo, #phpunit PHPUnit your bug exterminator RICARDO MELO PHPLX – 07 December 2013

description

Software should be perfect, but it isn't! Bugs in the software ranges from small "inoffensive ones" to the ones that makes the news when a bunch of credit card data is stole. On this talk we will review different testing methodologies and tools, and we will focus on using PHPUnit to assert how your software works and how to exterminate your bugs.

Transcript of PHPUnit your bug exterminator

Page 1: PHPUnit your bug exterminator

Follow this topic:

@rjsmelo, #phpunit

PHPUnit your bug exterminator

RICARDO MELO

PHPLX – 07 December 2013

Page 2: PHPUnit your bug exterminator

@rjsmelo 2

RICARDO MELO

● CTO @ DRI● PHP, Mysql, Linux and lots of other OSS

● ZCE, RHCE, LPI 3, ITIL, etc● +10 years building (and breaking) things

Page 3: PHPUnit your bug exterminator

@rjsmelo 3

About

● 14 Year old academic spin-off● Pragmatic OSS Orientation● PHP, Mysql, SugarCRM, Drupal, JavaScript, Linux, etc.

● Crafters, Integrators

● Always looking for software developers– Yes, right now!

Page 4: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

4

Outline

● Testing Methodologies● Testing Tools Ecosystem● What's PHPUnit● The "Hello World" test● How to run your tests● Other assertions● Other PHPUnit topics● The bug hunting work flow

Page 5: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

5

Testing and Development Methodologies

● Unit, Integration, System and Acceptance Testing

● CMMI/Waterfall, Agile and XP● TDD and BDD

Page 6: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

6

Testing Tools Ecosystem

● PHPUnit● Behat● Selenium● Codeception● And many more

Page 7: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

7

What's PHPUnit

● xUnit family for PHP● Uses assertions to check the behavior of the unit

● But it can handle other tests like integration and system

Page 8: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

8

Installing PHPUnit

● Pear

pear config-set auto_discover 1pear install pear.phpunit.de/PHPUnit

phpunit --version

Page 9: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

9

Installing PHPUnit

● Composer

{ "require-dev": { "phpunit/phpunit": "3.7.*" }}

composer install

./vendor/bin/phpunit --version

Page 10: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

10

The "Hello World" test

<?phpnamespace Phplx;

class HelloWorld { public function say(){ return "Hello World"; }}

Page 11: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

11

The "Hello World" test

<?phpnamespace Phplx\Tests\HelloWorld;use \PHPUnit_Framework_TestCaseuse \Phplx\HelloWorld;

class HelloWorldTest extends PHPUnit_Framework_TestCase{ public function testHelloWorld(){ $obj = new HelloWorld(); $this->assertEquals("Hello World", $obj->say()); }}

Page 12: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

12

How to run your tests

# using PEAR / PHAR phpunit --bootstrap vendor/autoload.php tests/

# using composer./vendor/bin/phpunit tests/

Page 13: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

13

How to run your tests – bootstrap.php

<?phpuse \Phplx\App;

require_once dirname(__DIR__) . "/vendor/autoload.php";

$app = new App();$app->initHelloWorldApplication();

Page 14: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

14

How to run your tests – phpunit.xml

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/3.7/phpunit.xsd" bootstrap="tests/bootstrap.php" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false"> <testsuites> <testsuite name="Hello World Test Suite"> <directory suffix="Test.php">tests</directory> </testsuite> </testsuites></phpunit>

Page 15: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

15

How to run your tests – phpunit

$ ./vendor/bin/phpunit PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from /home/rjsmelo/phplx/phpunit.xml

...

Time: 42 ms, Memory: 3.25Mb

OK (3 tests, 3 assertions)

Page 16: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

16

Other assertions

● There is a HUGE list of assertions

Page 17: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

17

Use specific assertions - assertInstanceOf

<?phpuse \Phplx\HelloWorld;

class InstanceOfTest extends PHPUnit_Framework_TestCase{ public function testTrue(){ $obj = new stdClass(); $this->assertTrue( $obj instanceof \Phplx\HelloWorld); }

public function testInstanceOf(){ $obj = new stdClass(); $this->assertInstanceOf( '\Phplx\HelloWorld', $obj); }}

Page 18: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

18

Use specific assertions - assertInstanceOf

PHPUnit 3.7.28 by Sebastian Bergmann.[...]

1) InstanceOfTest::testTrueFailed asserting that false is true.

2) InstanceOfTest::testInstanceOfFailed asserting that stdClass Object () is an instance of class "\Phplx\HelloWorld".

Page 19: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

19

Use specific assertions - JsonString

<?phpuse \Phplx\Object;class JsonStringTest extends PHPUnit_Framework_TestCase{ protected $jsonString = '{"someKey":"some thing","otherKey":"other thing"}';

public function testEquals(){ $obj = new Object(); $this->assertEquals( $this->jsonString, $obj->myPreciousJson()); }

public function testJsonString(){ $obj = new Object(); $this->assertJsonStringEqualsJsonString( $this->jsonString, $obj->myPreciousJson()); }}

Page 20: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

20

Use specific assertions - JsonString

PHPUnit 3.7.28 by Sebastian Bergmann.[...]

1) JsonStringTest::testEqualsFailed asserting that two strings are equal.--- Expected+++ Actual@@ @@-'{"someKey":"some thing","otherKey":"other thing"}'+'{"someKey":"some value","otherKey":"other thing"}'

2) JsonStringTest::testJsonStringFailed asserting that two objects are equal.--- Expected+++ Actual@@ @@ stdClass Object (- 'someKey' => 'some thing'+ 'someKey' => 'some value' 'otherKey' => 'other thing' )

Page 21: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

21

Other PHPUnit topics

● Setup / Teardown● Data providers● Test doubles

Page 22: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

22

The bug hunting work flow

● Gather information on the problem● Reproduce the problem● Capture that as a test● Fix it (ok... this sometimes is hard)● All green... mission accomplished

Page 23: PHPUnit your bug exterminator

1999 - 2013 DRI. Some Rights Reserved.

23

The community challenge

● Go to your favorite PHP project● Or go to https://github.com/trending?l=php and pick one

● Fix Some Bugs!

Page 24: PHPUnit your bug exterminator

Thank you

Page 25: PHPUnit your bug exterminator

Follow this topic:

@rjsmelo, #phpunit

QA

Code: https://github.com/rjsmelo/talk-phpunit

Feedback: https://joind.in/talk/view/10305

Page 26: PHPUnit your bug exterminator

www.dri-global.com

@rjsmelo

[email protected]