contao / test-case
Contao test case
Fund package maintenance!
to.contao.org/donate
Installs: 235 092
Dependents: 180
Suggesters: 0
Security: 0
Stars: 6
Watchers: 5
Forks: 4
Open Issues: 0
Requires
- php: ^8.1
- phpunit/phpunit: ^9.5
- symfony/yaml: ^6.4 || ^7.0
Requires (Dev)
- ext-pdo: *
- contao/core-bundle: 5.4.4
- doctrine/dbal: ^3.6
- doctrine/orm: ^2.17
- symfony/http-client: ^6.4 || ^7.0
- 5.x-dev
- 5.4.x-dev
- 5.4.4
- 5.4.3
- 5.4.2
- 5.4.1
- 5.4.0
- 5.4.0-RC4
- 5.4.0-RC3
- 5.4.0-RC2
- 5.4.0-RC1
- 5.3.x-dev
- 5.3.17
- 5.3.16
- 5.3.15
- 5.3.14
- 5.3.13
- 5.3.12
- 5.3.11
- 5.3.10
- 5.3.9
- 5.3.8
- 5.3.7
- 5.3.6
- 5.3.5
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.3.0-RC4
- 5.3.0-RC3
- 5.3.0-RC2
- 5.3.0-RC1
- 5.2.x-dev
- 5.2.10
- 5.2.9
- 5.2.8
- 5.2.7
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.2.0-RC6
- 5.2.0-RC5
- 5.2.0-RC4
- 5.2.0-RC3
- 5.2.0-RC2
- 5.2.0-RC1
- 5.1.x-dev
- 5.1.11
- 5.1.10
- 5.1.9
- 5.1.8
- 5.1.7
- 5.1.6
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.1.0-RC3
- 5.1.0-RC2
- 5.1.0-RC1
- 5.0.x-dev
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 5.0.0-RC4
- 5.0.0-RC3
- 5.0.0-RC2
- 5.0.0-RC1
- 4.13.x-dev
- 4.13.49
- 4.13.48
- 4.13.47
- 4.13.46
- 4.13.45
- 4.13.44
- 4.13.43
- 4.13.42
- 4.13.41
- 4.13.40
- 4.13.39
- 4.13.38
- 4.13.37
- 4.13.36
- 4.13.35
- 4.13.34
- 4.13.33
- 4.13.32
- 4.13.31
- 4.13.30
- 4.13.29
- 4.13.28
- 4.13.27
- 4.13.26
- 4.13.25
- 4.13.24
- 4.13.23
- 4.13.22
- 4.13.21
- 4.13.20
- 4.13.19
- 4.13.18
- 4.13.17
- 4.13.16
- 4.13.15
- 4.13.14
- 4.13.13
- 4.13.12
- 4.13.11
- 4.13.10
- 4.13.9
- 4.13.8
- 4.13.7
- 4.13.6
- 4.13.5
- 4.13.4
- 4.13.3
- 4.13.2
- 4.9.x-dev
- 4.9.42
- 4.9.41
- 4.9.40
- 4.9.39
- 4.9.38
- 4.9.37
- 4.9.36
- 4.9.35
- 4.9.34
- 4.9.33
- 4.9.32
- 4.9.31
- 4.9.30
- 4.9.29
- 4.9.28
- 4.6.1
- 4.6.0
- 4.5.0
- 4.4.0
- 4.3.1
- 4.3.0
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.2
- 4.1.1
- 4.1.0
- 4.0.0
- 3.1.0
- 3.0.0
- 2.1.0
- 2.0.0
- 1.6.0
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
This package is auto-updated.
Last update: 2024-10-23 10:18:18 UTC
README
Contao is an open source PHP content management system for people who want a professional website that is easy to maintain. Visit the project website for more information.
The Contao test case provides a PHPUnit test case with some useful methods for testing Contao. Run
php composer.phar require --dev contao/test-case
to install the package and then use it in your test classes:
use Contao\TestCase\ContaoTestCase; class MyTest extends ContaoTestCase { }
Mocking the Symfony container
The getContainerWithContaoConfiguration()
method mocks a Symfony container with the default configuration of the
Contao core extension.
$container = $this->getContainerWithContaoConfiguration(); echo $container->getParameter('contao.upload_path'); // will output "files"
You can also set a project directory:
$container = $this->getContainerWithContaoConfiguration('/tmp'); echo $container->getParameter('kernel.project_dir'); // will output "/tmp" echo $container->getParameter('kernel.root_dir'); // will output "/tmp/app" echo $container->getParameter('kernel.cache_dir'); // will output "/tmp/var/cache"
Mocking the Contao framework
The mockContaoFramework)
method mocks an initialized Contao framework.
$framework = $this->mockContaoFramework(); $framework ->expect($this->atLeastOnce()) ->method('initialize') ;
The method automatically adds a Config adapter with the Contao settings:
$framework = $this->mockContaoFramework(); $config = $framework->getAdapter(Contao\Config::class); echo $config->get('datimFormat'); // will output "'Y-m-d H:i'"
You can optionally add more adapters as argument:
$adapters = [ Contao\Config::class => $configAdapter, Contao\Encryption::class => $encryptionAdapter, ]; $framework = $this->mockContaoFramework($adapters);
The given Config adapter will overwrite the default Config adapter.
Mocking an adapter
The mockAdapter()
method will mock an adapter with the given methods.
$adapter = $this->mockAdapter(['findById']); $adapter ->method('findById') ->willReturn($model) ; $framework = $this->mockContaoFramework([Contao\FilesModel::class => $adapter]);
Adapters with a simple return value like above can be further simplified:
$adapter = $this->mockConfiguredAdapter(['findById' => $model]);
This code does exactly the same as the code above.
Mocking a class with magic properties
The mockClassWithProperties()
method mocks a class that uses magic __set()
and __get()
methods to manage
properties.
$mock = $this->mockClassWithProperties(Contao\PageModel::class); $mock->id = 2; $mock->title = 'Home'; echo $mock->title; // will output "Home"
If the class to be mocked is read-only, you can optionally pass the properties as constructor argument:
$properties = [ 'id' => 2, 'title' => 'Home', ]; $mock = $this->mockClassWithProperties(Contao\PageModel::class, $properties); echo $mock->title; // will output "Home"
If you need to call a method of the original class, you can pass the method name as third argument. The resulting mock object will be a partial mock object without the given method(s).
$mock = $this->mockClassWithProperties(Contao\PageModel::class, [], ['getTable']); $mock->id = 2; echo $mock->getTable(); // will call the original method
Mocking a token storage
The mockTokenStorage()
mocks a token storage with a token returning either a Contao back end or front end user.
$tokenStorage = $this->mockTokenStorage(Contao\BackendUser::class); $user = $tokenStorage->getToken()->getUser();
Using a temporary directory
The getTempDir()
method creates a temporary directory based on the test class name and returns its path.
$fs = new Filesystem(); $fs->mkdir($this->getTempDir().'/var/cache');
The directory will be removed automatically after the tests have been run. For this to work, please make sure to always
call the parent tearDownAfterClass()
method if you define the method in your test class!
use Contao\TestCase\ContaoTestCase; class MyTest extends ContaoTestCase { public static function tearDownAfterClass() { // The temporary directory would not be removed without this call! parent::tearDownAfterClass(); } }