testing persistence in php with dbunit

29
Testing persistence (in PHP) PHPUnit & DbUnit

Upload: peter-wilcsinszky

Post on 17-May-2015

4.044 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Testing persistence in PHP with DbUnit

Testing persistence (in PHP)

PHPUnit & DbUnit

Page 2: Testing persistence in PHP with DbUnit

Our story

by unit testing classes

Started from inside to the outside

Page 3: Testing persistence in PHP with DbUnit

Tools and principles

Using PHPUnit with Mock objects to isolate the SUT

• Design for testability (SOLID)

• Use the front door first

• One condition per test

http://xunitpatterns.com/Principles of Test Automation.htmlhttp://en.wikipedia.org/wiki/SOLID_(object-oriented_design)

Page 4: Testing persistence in PHP with DbUnit

PHPUnit

class TestClass     extends PHPUnit_Framework_TestCase{    public function setUp() {}    public function testMethod() {}    public function tearDown() {}}

Page 5: Testing persistence in PHP with DbUnit

Problems

No integration guarantees

Not effective for refactoring legacy code

Does not ensure external quality

Page 6: Testing persistence in PHP with DbUnit

Need for integration tests

To ensure external quality from top to bottom

Page 7: Testing persistence in PHP with DbUnit

Unit testing DAOs with DI and mocks

$connection = createMockExpecting(    " INSERT INTO table SET field = 'value' ");

$dao = new Dao($connection);$dao->insert('value');

Tells nothing about the database interaction

Page 8: Testing persistence in PHP with DbUnit

Testing DAOs - injected connection

$connection = createLocalTestConnection();

$dao = new Dao($connection);$dao->insert('value');

assertInsertedValueMatches('value');

Hard to replace in end to end tests

Page 9: Testing persistence in PHP with DbUnit

Dependency lookup

Page 10: Testing persistence in PHP with DbUnit

Testing DAOs - dependency lookup

$dao = new Dao();$dao->insert('value');

assertInsertedValueMatches('value');

http://xunitpatterns.com/Dependency Lookup.html

Page 11: Testing persistence in PHP with DbUnit

DbUnit + Etsy extensions

Page 12: Testing persistence in PHP with DbUnit

Test case initclass RemoverDaoTest extends DatabaseTestCaseBase{ /** * @return PHPUnit_Extensions_MultipleDatabase_Database[] */ protected function getDatabaseConfigs() { return array( $this->getDbConfigBuilder() ->connection($this->getCentralDbConnection()) ->dataSet($this->createXmlDataSet('init.xml')) ->build() ); }

Page 13: Testing persistence in PHP with DbUnit

Datasets

user: -   id: 1   name: Ingrid

<dataset>   <table name="user">       <column>id</column>       <column>name</column>       <row>           <value>1</value>           <value>Ingrid</value>       </row>   </table></dataset>

MySQL XML YAML

Page 14: Testing persistence in PHP with DbUnit

Assertions

public function testRemoveNoProblem(){   $remover = new RemoverDao();   $remover->removeUser(1);

   $this->assertDataSetsEqual( $this->createYamlDataSet('empty.yml'), $this->getCentralDbConnection() ->createDataSet(array('user'))   );}

Page 15: Testing persistence in PHP with DbUnit

Datasets

user: -   id: 1   name: Ingrid   created_at: 2012-01-16

<dataset>   <table name="user">       <column>id</column>       <column>name</column>       <column>created_at</column>       <row>           <value>1</value>           <value>Ingrid</value>           <value>2012-01-16</value>       </row>   </table></dataset>

MySQL XML YAML

Page 16: Testing persistence in PHP with DbUnit

Dataset filter

$datasetFilter = new PHPUnit_Extensions_Database_DataSet_DataSetFilter( $dataset);

$datasetFilter->addIncludeTables( array('user'));$datasetFilter->setExcludeColumnsForTable( 'user', array('created_at'));

Page 17: Testing persistence in PHP with DbUnit

Datasets

user: -   id: 1   name: Ingrid   created_at: ##TIME##

<dataset>   <table name="user">       <column>id</column>       <column>name</column>       <column>created_at</column>       <row>           <value>1</value>           <value>Ingrid</value>           <value>##TIME##</value>       </row>   </table></dataset>

MySQL XML YAML

Page 18: Testing persistence in PHP with DbUnit

Replacement dataset

$replacementDataset = new PHPUnit_Extensions_Database_DataSet_ReplacementDataSet(  $dataset, array('##TIME##' => TimeService::time()));

Page 19: Testing persistence in PHP with DbUnit

Implicit setup

Common initial state, extended test-by-test

Hard to share

Page 20: Testing persistence in PHP with DbUnit

Inline setup

• Define only the tables being used in common setup

• Insert all the necessary data inline in the test method

Still messy to share common datasets with xml or yml

Minimal fixture! 

Page 21: Testing persistence in PHP with DbUnit

Inline datasets with PHP

Value objects + builders

Convert to dataset for: • inserting into database• using in assertions

Page 22: Testing persistence in PHP with DbUnit

Setup

$user  = UserBuilder::create()->build();$video = VideoBuilder::create()  ->owner($user)  ->private()  ->build();

TestDataInserter::create(getCentralDbConnection())  ->add('user', $user)  ->add('video', $video);

Page 23: Testing persistence in PHP with DbUnit

Excercise

VideoPublisherDao::create()->publish($video->id);

Http service

DAO

HttpClient::create()->call('Video::publish', $video->id);

Page 24: Testing persistence in PHP with DbUnit

Verify

assertDataSetsEqual(  $this->createDataSet('video', $video),  $this->getCentralDbConnection() ->createDataSet(array('video')));

Page 25: Testing persistence in PHP with DbUnit

Layer crossing tests - where we use it

• Http services

• Daemons

• Cron jobs

• Ajax calls

Page 26: Testing persistence in PHP with DbUnit

The power of layer crossing tests

Refactoring (legacy) code

Increasing code coverage

Page 27: Testing persistence in PHP with DbUnit

There are downsides too

We use the back door => risk of overspecification

Empty initial state • hides concurrency problems• parallel testing more difficult

Page 28: Testing persistence in PHP with DbUnit

Concurrent end-to-end black box tests

Uses only the front door (public api)

Isolation with unique identifiers & Δ assertions

Tests real concurrency

Harder to track bugs & intermittent testshttp://engineering.imvu.com/2011/01/19/buildbot-and-intermittent-tests/

Page 29: Testing persistence in PHP with DbUnit

[email protected]/pepov