PHP code example of contao / test-case

1. Go to this page and download the library: Download contao/test-case library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

contao / test-case example snippets


use Contao\TestCase\ContaoTestCase;

class MyTest extends ContaoTestCase
{
}

$container = $this->getContainerWithContaoConfiguration();

echo $container->getParameter('contao.upload_path'); // will output "files"

$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"

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

$framework = $this->mockContaoFramework();
$config = $framework->getAdapter(Contao\Config::class);

echo $config->get('datimFormat'); // will output "'Y-m-d H:i'"

$adapters = [
    Contao\Config::class => $configAdapter,
    Contao\Encryption::class => $encryptionAdapter,
];

$framework = $this->mockContaoFramework($adapters);

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

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

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

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

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

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

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

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

$mock = $this->mockClassWithProperties(Contao\PageModel::class, [], ['getTable']);
$mock->id = 2;

echo $mock->getTable(); // will call the original method

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

$fs = new Filesystem();
$fs->mkdir($this->getTempDir().'/var/cache');

use Contao\TestCase\ContaoTestCase;

class MyTest extends ContaoTestCase
{
    public static function tearDownAfterClass()
    {
        // The temporary directory would not be removed without this call!
        parent::tearDownAfterClass();
    }
}