Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
matej21 committed Aug 26, 2017
0 parents commit 168447a
Show file tree
Hide file tree
Showing 23 changed files with 1,443 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/composer.lock
/tests/temp
/vendor
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "mangoweb/presenter-tester",
"license": "MIT",
"require": {
"php": "~7.1",
"nette/di": "~2.4",
"nette/bootstrap": "~2.4",
"nette/tester": "~2.0"
},
"require-dev": {
"nette/utils": "~2.4",
"nette/http": "~2.4",
"nette/reflection": "^2.4"
},
"autoload": {
"classmap": [
"src"
]
}
}
46 changes: 46 additions & 0 deletions src/Bridges/Database/DatabaseCreatorHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\Database;

use Mangoweb\Tester\DatabaseCreator\DatabaseCreator;
use Mangoweb\Tester\Infrastructure\Container\IAppContainerHook;
use Nette\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;


class DatabaseCreatorHook implements IAppContainerHook
{

/** @var DatabaseCreator */
private $databaseCreator;


public function __construct(DatabaseCreator $databaseCreator)
{
$this->databaseCreator = $databaseCreator;
}


public function onConfigure(Configurator $configurator): void
{

}


public function onCompile(ContainerBuilder $builder): void
{
$builder->addDefinition('databaseCreator')
->setClass(DatabaseCreator::class)
->setDynamic()
->setAutowired(FALSE);
$builder->prepareClassList();
}


public function onCreate(Container $applicationContainer): void
{
$applicationContainer->addService('databaseCreator', $this->databaseCreator);
}

}
34 changes: 34 additions & 0 deletions src/Bridges/MailTester/MailTesterContainerHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\MailTester;

use Mangoweb\MailTester\TestMailer;
use Mangoweb\Tester\Infrastructure\Container\IAppContainerHook;
use Nette\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;


class MailTesterContainerHook implements IAppContainerHook
{

public function onConfigure(Configurator $configurator): void
{

}


public function onCompile(ContainerBuilder $builder): void
{
$builder->getDefinition('mail.mailer')
->setClass(TestMailer::class)
->setFactory(TestMailer::class);
}


public function onCreate(Container $applicationContainer): void
{

}

}
33 changes: 33 additions & 0 deletions src/Bridges/MailTester/MailTesterTestCaseListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\MailTester;

use Mangoweb\MailTester\MailTester;
use Mangoweb\Tester\Infrastructure\ITestCaseListener;
use Mangoweb\Tester\Infrastructure\TestCase;


class MailTesterTestCaseListener implements ITestCaseListener
{

/** @var MailTester */
private $mailTester;


public function __construct(MailTester $mailTester)
{
$this->mailTester = $mailTester;
}


public function setUp(TestCase $testCase): void
{
}


public function tearDown(TestCase $testCase): void
{
$this->mailTester->assertNone();
}

}
60 changes: 60 additions & 0 deletions src/Bridges/Mockery/MockeryContainerHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\Mockery;

use Mangoweb\Tester\Infrastructure\Container\IAppContainerHook;
use Mangoweb\Tester\Infrastructure\TestContext;
use Mockery\MockInterface;
use Nette\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;
use Nette\Reflection\AnnotationsParser;
use Nette\Utils\Strings;

class MockeryContainerHook implements IAppContainerHook
{

/** @var TestContext */
private $testContext;


public function __construct(TestContext $testContext)
{
$this->testContext = $testContext;
}


public function onConfigure(Configurator $configurator): void
{

}


public function onCompile(ContainerBuilder $builder): void
{
}


public function onCreate(Container $applicationContainer): void
{
$rc = new \ReflectionClass($this->testContext->getTestCaseClass());
$rm = $rc->getMethod($this->testContext->getTestMethod());
$doc = $rm->getDocComment();
$params = Strings::matchAll($doc, '~\*\s+@param\s+([\w_\\\\|]+)\s+(\$[\w_]+)(?:\s+.*)?$~m');
foreach ($params as [, $types, $paramName]) {
$types = explode('|', $types);
if (count($types) !== 2) {
continue;
}
[$requiredType, $mockeryType] = $types;
$requiredType = AnnotationsParser::expandClassName($requiredType, $rc);
$mockeryType = AnnotationsParser::expandClassName($mockeryType, $rc);
if ($mockeryType !== MockInterface::class) {
continue;
}
$applicationContainer->addService($applicationContainer->findByType($requiredType)[0], \Mockery::mock($requiredType));
}

}

}
44 changes: 44 additions & 0 deletions src/Bridges/NextrasDbal/NextrasDbalHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\NextrasDbal;

use Mangoweb\Tester\Infrastructure\Container\IAppContainerHook;
use Nette\Configurator;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;
use Nette\DI\Statement;
use Nextras\Dbal\Connection;


class NextrasDbalHook implements IAppContainerHook
{

public function onConfigure(Configurator $configurator): void
{

}


public function onCompile(ContainerBuilder $builder): void
{
$def = $builder->getDefinitionByType(Connection::class);
$factory = $def->getFactory();
assert($factory !== NULL);
$args = $factory->arguments;
$args['config'] = new Statement('array_merge(?, ?)', [
$args['config'],
[
'database' => new Statement('@databaseCreator::getDatabaseName'),
],
]);
$def->setArguments($args);
$def->addSetup(['@databaseCreator', 'createTestDatabase']);
}


public function onCreate(Container $applicationContainer): void
{

}

}
35 changes: 35 additions & 0 deletions src/Bridges/PresenterTester/PresenterTesterTestCaseListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Bridges\PresenterTester;

use Mangoweb\Tester\Infrastructure\ITestCaseListener;
use Mangoweb\Tester\Infrastructure\TestCase;
use Mangoweb\Tester\PresenterTester\PresenterTester;
use Tester\Assert;


class PresenterTesterTestCaseListener implements ITestCaseListener
{

/** @var PresenterTester|NULL */
public $presenterTester;


public function setUp(TestCase $testCase): void
{
}


public function tearDown(TestCase $testCase): void
{
if (!$this->presenterTester) {
return;
}
foreach ($this->presenterTester->getResults() as $i => $result) {
if (!$result->wasResponseInspected()) {
Assert::fail(sprintf('Request #%d to %s presenter was not asserted', $i + 1, $result->getRequest()->getPresenterName()));
}
}
}

}
74 changes: 74 additions & 0 deletions src/Container/AppContainerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php declare(strict_types = 1);

namespace Mangoweb\Tester\Infrastructure\Container;

use Mangoweb\Tester\Infrastructure\MangoTesterExtension;
use Nette\Configurator;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerBuilder;


class AppContainerFactory
{

/** @var IAppConfiguratorFactory */
private $appConfiguratorFactory;


public function __construct(IAppConfiguratorFactory $appConfiguratorFactory)
{
$this->appConfiguratorFactory = $appConfiguratorFactory;
}


public function create(Container $testContainer, ?IAppContainerHook $testCaseHook): Container
{
$configurator = $this->appConfiguratorFactory->create($testContainer);

$hook = $this->getHook($testContainer);
if ($testCaseHook) {
$hook = new AppContainerHookList([$hook, $testCaseHook]);
}
$this->setupConfigurator($testContainer, $configurator, $hook);

$appContainer = $configurator->createContainer();
$hook->onCreate($appContainer);
assert($appContainer instanceof Container);

return $appContainer;
}


protected function setupConfigurator(Container $testContainer, Configurator $configurator, IAppContainerHook $hook): void
{
$configurator->addParameters([
'hooksHash' => md5(serialize(get_class($hook))),
]);
$hook->onConfigure($configurator);
$configurator->onCompile[] = function ($configurator, Compiler $compiler) use ($hook) {
$compilerExtension = new CompilerHookExtension();
$compilerExtension->onBeforeCompile[] = function (ContainerBuilder $builder) use ($hook) {
$hook->onCompile($builder);
};
$compiler->addExtension('tests.beforeCompile', $compilerExtension);
};

$configurator->addParameters([
'testContainerParameters' => $testContainer->getParameters(),
]);
}


protected function getHook(Container $testContainer): IAppContainerHook
{
$hooks = [];
foreach ($testContainer->findByTag(MangoTesterExtension::TAG_HOOK) as $hookName => $_) {
$hook = $testContainer->getService($hookName);
assert($hook instanceof IAppContainerHook);
$hooks[] = $hook;
}
return new AppContainerHookList($hooks);
}

}
Loading

0 comments on commit 168447a

Please sign in to comment.