19
Unit Testing PHP code Sudar Muthu http://sudarmuthu.com https://github.com/sudar

Unit testing in php

Embed Size (px)

Citation preview

Page 1: Unit testing in php

Unit Testing PHP codeSudar Muthu

http://sudarmuthu.com https://github.com/sudar

Page 2: Unit testing in php

Me

• Programming in PHP for more than a decade

• Programming in WordPress for 8 years

• Big fan of automating process and workflows

• Occasionally contribute to open source projects

2

Page 3: Unit testing in php

What about you?

• What is your typical development environment looks like?

• What is your experience with PHP?

• What is your experience with Unit Testing?

• What are your expectations out of this talk?

3

Page 4: Unit testing in php

Is there something wrong?class Sample { protected $value;

public function initialize($value) { $this->value = $value; }

public function execute() { if (!$this->value) { throw new Exception("value not set"); } return $value * 10; // business logic }}

$sample = new Sample;$sample->execute();

4

Page 5: Unit testing in php

Finding bugs is not easy

5

Page 6: Unit testing in php

Write Tests

6

Page 7: Unit testing in php

Write Tests

7

It sounds obvious but getting started is the hardest part!

Page 8: Unit testing in php

Different types of Testing

• Functionality testing

• Integration testing

• Unit testing

8

Page 9: Unit testing in php

Different types of Testing

• Functionality testing

• Integration testing

• Unit testing

9

Page 10: Unit testing in php

Briefly

• What is PHPUnit?

• Installing PHPUnit

• Setting up folder structure

• phpunit.xml

10

Page 11: Unit testing in php

Test classes, not methods

• Unit testing, is about testing the observable behaviours of a class!

• Observable from the outside! Nobody cares about the internal state of a class if it never changes the outcome of a method call.

• Don’t test getters and setters, unless they have custom validation

Page 12: Unit testing in php

Three steps in test cases

• setup

• act

• verify

Page 13: Unit testing in php

Demo

13

Page 14: Unit testing in php

Writing our first test case public function test_execute_works_with_initialize() { // setup $sample = new Sample();

// act $sample->initialize(10); $return = $sample->execute();

// verify $this->assertEquals(100, $return); }

Page 15: Unit testing in php

Testing Exception /** * @expectedException Exception */ public function test_execute_needs_initialize() { // setup $sample = new Sample();

// act $sample->execute();

// verify // that it throws and exception }

Page 16: Unit testing in php

Let’s add more tests

• Testing decimals

• Testing with negative values

• Testing it work with zero

Page 17: Unit testing in php

Why use mocks?

• Isolate the test class from its dependencies.

• Test functionality that calls external services.

• Test functionality that depends on class internals.

Page 18: Unit testing in php

Some PHPUnit Tips• Have a fast test suite

• Use Continuous Integration

• Use Composer

• Enable code coverage in reports

• phpunit.xml.dist vs phpunit.xml

• Use specific assertions

Page 19: Unit testing in php

Thank You

19

@sudarmuthu http://sudarmuthu.com

https://github.com/sudar