2017 © Pedro Peláez
 

library phpunit_helper

PHPUnit helper to manage tested resource, mocked objects and test values.

image

serendipity_hq/phpunit_helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  • Monday, August 1, 2016
  • by Aerendir
  • Repository
  • 1 Watchers
  • 1 Stars
  • 2,441 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 3 Open issues
  • 34 Versions
  • 74 % Grown

The README.md

Latest Stable Version Build Status Total Downloads License Code Climate Test Coverage Issue Count StyleCI SensioLabsInsight Dependency Status Coverage Status, (*1)

SHQ_PHPUnit_Helper

! DOCUMENTATION NOT YET UPDATED !, (*2)

This helper permits you to tear down your tests in an easy way., (*3)

It can also print some info about memory usage before and after tear down., (*4)

How to install PHPUnit_Helper

To install PHPUnit_Helper use Composer:, (*5)

composer require serendipity_hq/phpunit_helper

Quick use

To quickly use PHPUnit_Helper, create a TestCase.php file and extend all your tests from this., (*6)

The helper is built as a trait, so you can use it in conjuction with other test cases (for example, the WebTestCase provided by LiipFunctionalTestBundle or the Symfony's WebTestCase., (*7)

<?php

/**
 * @package     PHPUnit_Helper
 *
 * @author      Adamo Crespi <hello@aerendir.me>
 * @copyright   Copyright (C) 2016.
 * @license     MIT
 */

namespace AppBundle\Tests;

use Liip\FunctionalTestBundle\Test\WebTestCase as BaseWebTestCase;
use SerendipityHQ\Library\PHPUnit_Helper\PHPUnit_Helper;

/**
 * Class WebTestCase
 *
 * @package AppBundle\Tests
 */
class WebTestCase extends BaseWebTestCase
{
    /** Here you include the trait in the test case */
    use PHPUnit_Helper;

    public function tearDown()
    {
        /** Measure the memory usage before tear down */
        $this->measureMemoryBeforeTearDown();

        /** Call the parent tear down method */
        parent::tearDown();

        /** CALL THIS AFTER the parent tear down method */
        $this->helpTearDown();

        /** Print in the console the information about memory usage */
        $this->printMemoryUsageInfo();
    }
}

Using the helper

During unit testing you need to:, (*8)

  1. Instantiate the class to test;
  2. Create some mocks to emulate original classes used by the tested class;
  3. Set some expected values.

All those operations can be handled by the helper in a simple and elegant way., (*9)

This is a sample code to test an hypothetical Order entity:, (*10)

/**
 * Tests all get and set methods
 */
public function testOrder()
{
    $this->setResourceToTest(new Order());

    $this->addHelpMocksCollection('purchase',
        $this->generateMocksCollection($this->getMock('\AppBundle\Entity\Purchase'), 3),
        true
    )
        ->addExpectedValue('id',          1)
        ->addHelpMock('channel',          $this->getMock('\AppBundle\Entity\Store'), true)
        ->addHelpMock('placedBy',         $this->getMock('\AppBundle\Entity\Customer'), true)
        ->addExpectedValue('placededOn',  new \DateTime('2015-04-12 00:08'))
        ->addExpectedValue('modifiedOn',  new \DateTime('2015-04-12 00:08'))
        ->addExpectedValue('completedOn', new \DateTime('2015-04-12 00:08'))
        ->bindExpectedValuesToResource();

    $this->assertEquals($this->getExpectedValue('id'),          $this->getTestingResource()->getId());
    $this->assertEquals($this->getExpectedValue('channel'),     $this->getTestingResource()->getChannel());
    $this->assertEquals($this->getExpectedValue('placedBy'),    $this->getTestingResource()->getPlacedBy());
    $this->assertEquals($this->getExpectedValue('placedOn'),    $this->getTestingResource()->getPlacedOn());
    $this->assertEquals($this->getExpectedValue('modifiedOn'),  $this->getTestingResource()->getModifiedOn());
    $this->assertEquals($this->getExpectedValue('completedOn'), $this->getTestingResource()->getCompletedOn());
    $this->assertEquals($this->getExpectedValue('id'),          $this->getTestingResource()->__toString());
    $this->assertEquals($this->getExpectedCount('purchase'),    count($this->getTestingResource()->getPurchases()));

    $this->getTestingResource()->removePurchase($this->removeMockFromMocksCollection(1, 'purchase'));
    $this->assertEquals($this->getExpectedCount('purchase'), count($this->getTestingResource()->getPurchases()));
}

Using $this->setResourceToTest()

Using $this->setResourceToTest() you can tell the helper which is the tested resource. Knowing this, the helper can help you to populate it after you've added some expected values., (*11)

Using $this->addHelpMocksCollection() and $this->generateMocksCollection()

To test the add*() and remove*() methods you have to create a lot of mocks of the same kind of objects., (*12)

Maybe your add*() methods appear to be very similar to this:, (*13)

/**
 * Add a Product
 *
 * @param \AppBundle\Entity\Purchase $purchase
 * @return $this
 */
public function addPurchase(Purchase $purchase)
{
    $this->purchases[] = $purchase;
    $purchase->setInOrder($this);

    return $this;
}

The old way to test this method is something like this:, (*14)

$resource = new Order();

$testPurchases = [
    $this->getMock('\AppBundle\Entity\Purchase'),
    $this->getMock('\AppBundle\Entity\Purchase'),
    $this->getMock('\AppBundle\Entity\Purchase')
];

foreach ($testPurchases as $purchase)
    $resource->addPurchase($purchase);

Using the PHPUnit_Helper::generateMocksCollection() method, you only need to write one line of code:, (*15)

$this->addHelpMocksCollection('purchase',
    $this->generateMocksCollection($this->getMock('\AppBundle\Entity\Purchase'), 3),
    true
)

As name of the collection use the name of the add*() method without the add part (in the sample Order entity, the method to add a Purchase is called Order::addPurchase())., (*16)

Using a collection makes you able to use the methods PHPUnit_Helper::getExpectedCount({name_of_the_collection}) and PHPUnit_Helper::removeMockFromMocksCollection({key}, {name_of_the_collection})., (*17)

By default PHPUnit_Helper::removeMockFromMocksCollection() also removes the mock from the expected values, but you can disable this behavior passing the optional third parameter and setting it to false., (*18)

Using PHPUnit_Helper::bindExpectedValuesToResource()

Adding expected values and calling them as the property of your class, makes you able to use the method PHPUnit_Helper::->bindExpectedValuesToResource(). This can automatically populate your set tested resource (that you set through PHPUnit_Helper::setResource()) with expected values, so you don't have to populate it manually., (*19)

Use of the reflection to tear down the tests

The PHPUnit_Helper::helpTearDown() method by default sets to null the internal properties, without taking care of properties of the tests., (*20)

If you are using the method with an existent test class, you may find helpful to use the method PHPUnit_Helper::useReflectionToTearDown()., (*21)

Using this method you set to true an internal flag that tells to PHPUnit_Helper to use the reflection to set to null all the found properties of the test class., (*22)

This is useful during the implementation to taking advantage of the tear down functionality, also if you don't still use all the methods of the helper to manage your tests., (*23)

Measuring the memory usage during tests

You can measure the memory usage before and after the tear down using the methods $this->measureMemoryBeforeTearDown() and $this->printMemoryUsageInfo(). This last method automatically calls $this->measureMemoryAfterTearDown() if it is not yet called., (*24)

So you can explicitly call it in you tear down method to measure the memory usage when you best like., (*25)

The Versions

01/08 2016

dev-master

9999999-dev https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

01/08 2016

7.1.0

7.1.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

31/01 2016

7.0.3

7.0.3.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

20/01 2016

7.0.2

7.0.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

20/01 2016

7.0.1

7.0.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

20/01 2016

7.0.0

7.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

20/01 2016

6.0.2

6.0.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

6.0.1

6.0.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

6.0.0

6.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.2.2

5.2.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.2.1

5.2.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.2.0

5.2.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.1.3

5.1.3.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.1.2

5.1.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.1.1

5.1.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

19/01 2016

5.1.0

5.1.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

15/01 2016

5.0.1

5.0.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

15/01 2016

5.0.0

5.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

15/01 2016

4.0.2

4.0.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

4.0.1

4.0.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

4.0.0

4.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

3.4

3.4.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

3.0.2

3.0.2.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

3.0.1

3.0.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

3.0.0

3.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

2.0.0

2.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

13/01 2016

1.0.0

1.0.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.5.1

0.5.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.5

0.5.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.4

0.4.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.3

0.3.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.2

0.2.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.1.1

0.1.1.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Development Requires

by Adamo crespi

phpunit

12/01 2016

0.1

0.1.0.0 https://github.com/SerendipityHQ/SHQ_PHPUnit_Helper

PHPUnit helper to manage tested resource, mocked objects and test values.

  Sources   Download

MIT

The Development Requires

by Adamo crespi

phpunit