mirror of https://github.com/FriendsOfTYPO3/tea.git synced 2024-11-10 03:36:12 +01:00

[FEATURE] Add a DBUnit example

This is the functional test for the TeaBeverageRepository.
This commit is contained in:
Oliver Klee 2015-10-25 11:34:00 +01:00
parent 6dfa56f78e
commit a4721b0d1c
3 changed files with 108 additions and 1 deletions

View file

@ -4,7 +4,7 @@ This TYPO3 extension is an example for writing unit tests for extbase
extensions for TYPO3 CMS using PHPUnit. extensions for TYPO3 CMS using PHPUnit.
The functional tests for the Repositories provide an example of creating 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 The functional test for the Utility/FileUtility class provides examples
for working with [vfsStream](https://github.com/mikey179/vfsStream/). for working with [vfsStream](https://github.com/mikey179/vfsStream/).

View file

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<dataset>
<tx_tea_domain_model_teabeverage>
<uid>1</uid>
<size>3.141</size>
</tx_tea_domain_model_teabeverage>
</dataset>

View file

@ -0,0 +1,100 @@
<?php
namespace OliverKlee\Tea\Tests\Functional\Domain\Repository;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use OliverKlee\Tea\Domain\Model\TeaBeverage;
use OliverKlee\Tea\Domain\Repository\TeaBeverageRepository;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
/**
* Test case.
*
* @author Oliver Klee <typo3-coding@oliverklee.de>
*/
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
);
}
}