Code Monkey home page Code Monkey logo

contao-test-case's Introduction

Contao 4 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 4 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"

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

contao-test-case's People

Contributors

aschempp avatar bytehead avatar leofeyer avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.