2017 © Pedro Peláez
 

library phpunit-contao-database

MetaModels phpunit contao database classes

image

metamodels/phpunit-contao-database

MetaModels phpunit contao database classes

  • Wednesday, July 20, 2016
  • by xtra
  • Repository
  • 5 Watchers
  • 2 Stars
  • 8,727 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 3 Versions
  • 4 % Grown

The README.md

MetaModels database objects for phpunit

What you can expect here.

This repository holds a small subset of classes that can be used to fake a Contao database connection., (*1)

To do so, you define the queries and their result sets., (*2)

Installation

Add "metamodels/phpunit-contao-database": "~1.0" to your composer.json in the require-dev section and you are set., (*3)

How to use it.

In your unit test you simply replace the original Contao database classes by calling the following code (before the original Contao database classes get loaded via autoloading!). A good place might be the setUp()-method of your test case., (*4)

class MyTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Register the database.
     *
     * @return void
     */
    public function setUp()
    {
        \MetaModels\Test\Contao\Database::register();
    }
}

Using it in unit tests

You should use dependency injection for proper unit testing, then you can use something like this:, (*5)

$objectToTest = new ClassToTest();
$database     = \MetaModels\Test\Contao\Database::getNewTestInstance();

// Inject the database into the instance.
$objectToTest->setDatabase($database);

$database
    ->getQueryCollection()
    ->theQuery('SELECT * FROM test WHERE id=?')
    ->with(1)
    ->result()
        ->addRow(
            array(
              'id'     => 1,
              'tstamp' => 343094400,
              'title'  => 'test'
            ),
        );

// This method will call the above query internally and will receive the given result.
$result = $objectToTest->testMethod();

$this->assertEquals(1, $result->getId());
$this->assertEquals(343094400, $result->getModificationTime());
$this->assertEquals('test', $result->getTitle());

If you should be in the unfortunate position that you can not use dependency injection, as the underlying code is using Database::getInstance(), you are not out of luck. You can use the method $database = \MetaModels\Test\Contao\Database::getNewTestInstance(); which will return the "default" database instance then., (*6)

However using this approach is not suggested as the instance will be shared over all unit tests., (*7)

The Versions

20/07 2016

dev-master

9999999-dev http://now.metamodel.me/

MetaModels phpunit contao database classes

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

phpunit metamodels

20/07 2016

1.0.1

1.0.1.0 http://now.metamodel.me/

MetaModels phpunit contao database classes

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

phpunit metamodels

22/07 2015

1.0.0

1.0.0.0 http://now.metamodel.me/

MetaModels phpunit contao database classes

  Sources   Download

LGPL-3.0+

The Requires

 

The Development Requires

phpunit metamodels