unit testing in php

25
Unit testing in PHP by Silviu Butnariu ([email protected])

Upload: radu-murzea

Post on 17-May-2015

956 views

Category:

Technology


0 download

DESCRIPTION

PHP Unit Presentation by Silviu Butnariu held at Pentalog's headquarters in Cluj-Napoca on 26th September 2013.

TRANSCRIPT

Page 1: Unit Testing in PHP

Unit testing in PHP

by Silviu Butnariu ([email protected])

Page 2: Unit Testing in PHP

What’s that?Unit testing = testing bunches of code, not the

whole application

PhpUnit was created by Sebastian Bergamann

It’s part of xUnit family – most used unit testers

Used for automatic tests – makes the machine do the work

Page 3: Unit Testing in PHP

What are the benefits of unit testing?

Uncover bugs easier – good tests go trough all possible program paths

Assure high code coverage – measurable code quality indicator

Faster than manual testing

Suitable for teamwork

Page 4: Unit Testing in PHP

Installing PhpUnitWith PEAR: pear config-set auto_discover 1pear install phpunit/PHPUnit

With composer (dependency manager for php); just add to composer.json:

{ "require-dev": { "phpunit/phpunit": "3.7.*" } }and update the composer

Page 5: Unit Testing in PHP

Basic test

Page 6: Unit Testing in PHP

Test result notations

. – success

F – failure

E – error

I – incomplete

S - skipped

Page 7: Unit Testing in PHP

AssertionsHelper functions that compare an expected result

with the actual one:- assertEquals()- assertFalse()- assertArrayHasKey()- assertInstanceOf()- assertNotNull()- assertRegExp()- etc.

Page 8: Unit Testing in PHP

Dependency injection

DI is a software pattern that allows the removal of hard-coded dependencies

Highly coupled dependencies – bad

Loose coupled dependencies - awesome

Improves code readability, reusability

Page 9: Unit Testing in PHP

DI guidelinesDon’t use ‘new’

Pass dependencies as method parameters

DI allows real unit testing, by separating modules

Read the DI best practices

Page 10: Unit Testing in PHP

Mocking objectsCreating fake objects that act in a

predefined and predictable way

Page 11: Unit Testing in PHP

Stubbing methodsIn close relation to mockingStubbing implies faking a method from the

mock object

Page 12: Unit Testing in PHP

Static methodsAvoid static methods

Can’t mock static calls to outside classes!

Page 13: Unit Testing in PHP

AnnotationsThese syntactic metadata are used for

specifying some special case behaviors- @covers- @test- @dataProvider- @expectedException- @group- etc.

Page 14: Unit Testing in PHP

Data ProviderAllows to bunch up more test cases into

one, thus reducing code duplicity

Page 15: Unit Testing in PHP

Testing exceptions

Page 16: Unit Testing in PHP

setUp & tearDownsetUp(), tearDown() – executed once for each test methodsetUpBeforeClass(), tearDownAfterClass() - called before

the first test of the test case and after the last test

Page 17: Unit Testing in PHP

DB testingNeed extension: pear install phpunit/DbUnitSupported dbs: mysql, postgre, oracle, sqliteThere are 4 stages of db testing:

- set up fixture- exercise SUT- verify outcome- teardown

Important methods: - getConnection() - connection data (host,db,pass)- getDataSet() – defines state of DB before each test

Page 18: Unit Testing in PHP

DB testingOnce setting up the ‘fake’ db connection tests can be

executed to check queries

The db changes that occur during the tests don’t persist

Can verify number of rows after insertion, check query results, etc.

This is the safest way of fully testing an application (versus mocking db connection)

Page 19: Unit Testing in PHP

Skeleton GeneratorAutomatically generates test classes based

on assertions in the code

Page 20: Unit Testing in PHP

IDE integrationIntegrated in most ides: Eclipse, NetBeans,

PhpStorm, etc

Page 21: Unit Testing in PHP

Code coverageGenerating statistics with naked PhpUnitphpunit -c app/ --coverage-

html={foldername}

Page 22: Unit Testing in PHP

Code coverageFFFFFFF

Page 23: Unit Testing in PHP

Documentationhttp://phpunit.de/manual/3.7/en/

Page 24: Unit Testing in PHP

Q & A

Page 25: Unit Testing in PHP

THANKS !!!