2018-09-25 16:46:37 +02:00
|
|
|
<?php
|
|
|
|
|
2023-02-03 20:41:59 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-25 16:46:37 +02:00
|
|
|
namespace Engelsystem\Test\Unit;
|
|
|
|
|
2019-10-08 13:57:50 +02:00
|
|
|
use Engelsystem\Application;
|
2022-07-11 23:22:22 +02:00
|
|
|
use Engelsystem\Helpers\Translation\Translator;
|
2022-06-07 22:59:49 +02:00
|
|
|
use Engelsystem\Renderer\Renderer;
|
2021-06-29 00:27:57 +02:00
|
|
|
use Faker\Factory as FakerFactory;
|
|
|
|
use Faker\Generator;
|
2019-04-24 10:45:00 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-10-06 21:03:20 +02:00
|
|
|
use PHPUnit\Framework\MockObject\Rule\InvocationOrder;
|
2018-09-25 16:46:37 +02:00
|
|
|
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
|
|
|
|
|
|
|
|
abstract class TestCase extends PHPUnitTestCase
|
|
|
|
{
|
2022-12-15 19:50:56 +01:00
|
|
|
protected Application $app;
|
2019-10-08 13:57:50 +02:00
|
|
|
|
2022-12-14 00:28:34 +01:00
|
|
|
protected function setExpects(
|
|
|
|
MockObject $object,
|
|
|
|
string $method,
|
|
|
|
array $arguments = null,
|
|
|
|
mixed $return = null,
|
|
|
|
InvocationOrder|int $times = null
|
2022-12-14 19:15:20 +01:00
|
|
|
): void {
|
2018-09-25 16:46:37 +02:00
|
|
|
if (is_null($times)) {
|
|
|
|
$times = $this->once();
|
|
|
|
}
|
|
|
|
|
2021-11-23 11:59:53 +01:00
|
|
|
if (is_int($times)) {
|
|
|
|
$times = $this->exactly($times);
|
|
|
|
}
|
|
|
|
|
2018-09-25 16:46:37 +02:00
|
|
|
$invocation = $object->expects($times)
|
|
|
|
->method($method);
|
|
|
|
|
|
|
|
if (is_null($arguments)) {
|
|
|
|
$invocation->withAnyParameters();
|
|
|
|
} else {
|
|
|
|
call_user_func_array([$invocation, 'with'], $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_null($return)) {
|
|
|
|
$invocation->willReturn($return);
|
|
|
|
}
|
|
|
|
}
|
2019-10-08 13:57:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Called before each test run
|
|
|
|
*/
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
$this->app = new Application(__DIR__ . '/../../');
|
2021-06-29 00:27:57 +02:00
|
|
|
|
|
|
|
$faker = FakerFactory::create();
|
|
|
|
$faker->addProvider(new FakerProvider($faker));
|
|
|
|
$this->app->instance(Generator::class, $faker);
|
2019-10-08 13:57:50 +02:00
|
|
|
}
|
2022-06-07 22:59:49 +02:00
|
|
|
|
2022-07-11 23:22:22 +02:00
|
|
|
/**
|
|
|
|
* @return Translator&MockObject
|
|
|
|
*/
|
|
|
|
protected function mockTranslator(bool $mockImplementation = true): Translator
|
2022-06-07 22:59:49 +02:00
|
|
|
{
|
|
|
|
$translator = $this->getMockBuilder(Translator::class)
|
|
|
|
->disableOriginalConstructor()
|
2022-07-11 23:22:22 +02:00
|
|
|
->onlyMethods(['translate'])
|
2022-06-07 22:59:49 +02:00
|
|
|
->getMock();
|
2022-07-11 23:22:22 +02:00
|
|
|
|
|
|
|
if ($mockImplementation) {
|
|
|
|
$translator->method('translate')
|
|
|
|
->willReturnCallback(fn(string $key, array $replace = []) => $key);
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:59:49 +02:00
|
|
|
$this->app->instance('translator', $translator);
|
2022-07-11 23:22:22 +02:00
|
|
|
|
|
|
|
return $translator;
|
2022-06-07 22:59:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param bool $mockImplementation Whether to mock the Renderer methods
|
|
|
|
* @return Renderer&MockObject
|
|
|
|
*/
|
|
|
|
protected function mockRenderer(bool $mockImplementation = true): Renderer
|
|
|
|
{
|
|
|
|
$renderer = $this->getMockBuilder(Renderer::class)
|
|
|
|
->disableOriginalConstructor()
|
2022-07-11 23:22:22 +02:00
|
|
|
->onlyMethods(['render'])
|
2022-06-07 22:59:49 +02:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
if ($mockImplementation) {
|
|
|
|
$renderer->method('render')
|
|
|
|
->willReturnCallback(fn(string $template, array $data = []) => $template . json_encode($data));
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->app->instance('renderer', $renderer);
|
2022-07-11 23:22:22 +02:00
|
|
|
|
2022-06-07 22:59:49 +02:00
|
|
|
return $renderer;
|
|
|
|
}
|
2018-09-25 16:46:37 +02:00
|
|
|
}
|