Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Test Case Laravel Package

contao/test-case

View on GitHub
Deep Wiki
Context7

Contao test case

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
{
}

Symfony container

The getContainerWithContaoConfiguration() method creates a Symfony container builder object 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"

Contao framework

The createContaoFrameworkMock() and createContaoFrameworkStub() methods create a mock or stub object of an initialized Contao framework.

$framework = $this->createContaoFrameworkMock();
$framework
    ->expect($this->atLeastOnce())
    ->method('initialize')
;

The method automatically adds a Config adapter with the Contao settings:

$framework = $this->createContaoFrameworkStub();
$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->createContaoFrameworkStub($adapters);

A given Config adapter will overwrite the default Config adapter.

Adapters

The createAdapterMock() and createAdapterStub() methods will create an adapter mock or stub object with the given methods.

$adapter = $this->createAdapterStub(['findById']);
$adapter
    ->method('findById')
    ->willReturn($model)
;

$framework = $this->createContaoFrameworkStub([Contao\FilesModel::class => $adapter]);

Adapters with a simple return value like above can be further simplified:

$adapter = $this->createConfiguredAdapterStub(['findById' => $model]);

This code does exactly the same as the code above.

Classes with magic properties

The createClassWithPropertiesMock() and createClassWithPropertiesStub() methods will create a mock or stub object of a class that uses magic __set() and __get() methods to manage properties.

$mock = $this->createClassWithPropertiesStub(Contao\PageModel::class);
$mock->id = 2;
$mock->title = 'Home';

echo $mock->title; // will output "Home"

If the class is read-only, you can optionally pass the properties as constructor argument:

$properties = [
    'id' => 2,
    'title' => 'Home',
];

$mock = $this->createClassWithPropertiesStub(Contao\PageModel::class, $properties);

echo $mock->title; // will output "Home"

Token storage

The createTokenStorageStub() creates a token storage stub object with a token returning either a Contao back end or front end user.

$tokenStorage = $this->createTokenStorageStub(Contao\BackendUser::class);
$user = $tokenStorage->getToken()->getUser();

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();
    }
}
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
htinlynn/genius-helper
maco-studios/hephaestus
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator