From a4721b0d1ccd319b252d590e079f8721b75b098d Mon Sep 17 00:00:00 2001 From: Oliver Klee Date: Sun, 25 Oct 2015 11:34:00 +0100 Subject: [PATCH] [FEATURE] Add a DBUnit example This is the functional test for the TeaBeverageRepository. --- README.md | 2 +- .../Repository/Fixtures/TeaBeverages.xml | 7 ++ .../Repository/TeaBeverageRepositoryTest.php | 100 ++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 Tests/Functional/Domain/Repository/Fixtures/TeaBeverages.xml create mode 100644 Tests/Functional/Domain/Repository/TeaBeverageRepositoryTest.php diff --git a/README.md b/README.md index 8e3fba5..5d75057 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ This TYPO3 extension is an example for writing unit tests for extbase extensions for TYPO3 CMS using PHPUnit. The functional tests for the Repositories provide an example of creating -records using the PHPUnit extension testing framework. +records both using the PHPUnit extension testing framework and DBUnit. The functional test for the Utility/FileUtility class provides examples for working with [vfsStream](https://github.com/mikey179/vfsStream/). diff --git a/Tests/Functional/Domain/Repository/Fixtures/TeaBeverages.xml b/Tests/Functional/Domain/Repository/Fixtures/TeaBeverages.xml new file mode 100644 index 0000000..09c9f80 --- /dev/null +++ b/Tests/Functional/Domain/Repository/Fixtures/TeaBeverages.xml @@ -0,0 +1,7 @@ + + + + 1 + 3.141 + + \ No newline at end of file diff --git a/Tests/Functional/Domain/Repository/TeaBeverageRepositoryTest.php b/Tests/Functional/Domain/Repository/TeaBeverageRepositoryTest.php new file mode 100644 index 0000000..83903be --- /dev/null +++ b/Tests/Functional/Domain/Repository/TeaBeverageRepositoryTest.php @@ -0,0 +1,100 @@ + + */ +class TeaBeverageRepositoryTest extends \Tx_Phpunit_Database_TestCase { + /** + * @var TeaBeverageRepository|\PHPUnit_Framework_MockObject_MockObject + */ + protected $subject = null; + + protected function setUp() { + if (!$this->createDatabase()) { + self::markTestSkipped('Test database could not be created.'); + } + $this->importExtensions(array('tea')); + + /** @var ObjectManager $objectManager */ + $objectManager = GeneralUtility::makeInstance(ObjectManager::class); + // We are using the object manager instead of new so that the dependencies get injected. + // In a unit test, we would inject the mocked dependencies instead. + $this->subject = $objectManager->get(TeaBeverageRepository::class); + } + + protected function tearDown() { + $this->dropDatabase(); + $this->switchToTypo3Database(); + } + + /** + * @test + */ + public function findAllForNoRecordsReturnsEmptyContainer() { + $container = $this->subject->findAll(); + + self::assertSame( + 0, + $container->count() + ); + } + + /** + * @test + */ + public function findAllWithOneRecordFindsThisRecord() { + $this->importDataSet(__DIR__ . '/Fixtures/TeaBeverages.xml'); + + $container = $this->subject->findAll(); + /** @var TeaBeverage $first */ + $first = $container->getFirst(); + + self::assertSame( + 1, + $container->count() + ); + self::assertSame( + 1, + $first->getUid() + ); + } + + /** + * @test + */ + public function findByUidForExistingRecordReturnsModelWithData() { + $this->importDataSet(__DIR__ . '/Fixtures/TeaBeverages.xml'); + + /** @var TeaBeverage $model */ + $model = $this->subject->findByUid(1); + + self::assertNotNull($model); + self::assertEquals( + 3.141, + $model->getSize(), + '', + 0.001 + ); + } +}