Skip to content

Commit

Permalink
Tests: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Feb 5, 2024
1 parent 33b0e9d commit f72b954
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 159 deletions.
54 changes: 24 additions & 30 deletions tests/Cases/DI/EventApplicationBridgeExtension.phpt
@@ -1,54 +1,48 @@
<?php declare(strict_types = 1);

/**
* Test: DI\EventApplicationBridgeExtension
*/

use Contributte\EventDispatcher\DI\EventDispatcherExtension;
use Contributte\Events\Extra\DI\EventApplicationBridgeExtension;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Neonkit;
use Nette\Application\Application;
use Nette\Application\Responses\VoidResponse;
use Nette\Bridges\ApplicationDI\ApplicationExtension;
use Nette\Bridges\HttpDI\HttpExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\FakePresenter;
use Tests\Fixtures\FakePresenterShutdownSubscriber;
use Tests\Fixtures\FakePresenterStartupSubscriber;
use Tests\Fixtures\FakeStartupSubscriber;

require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
Assert::exception(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$loader->load(function (Compiler $compiler): void {
$compiler->addExtension('events2application', new EventApplicationBridgeExtension());
}, __FILE__ . '1');
ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('events2application', new EventApplicationBridgeExtension());
})->build();
}, LogicException::class, 'Service of type "Nette\Application\Application" is needed. Please register it.');
});

test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->loadConfig(FileMock::create('
services:
- Nette\Application\Routers\RouteList
fake.startup.startupSubscriber: Tests\Fixtures\FakeStartupSubscriber
fake.presenter.startup.startupSubscriber: Tests\Fixtures\FakePresenterStartupSubscriber
fake.presenter.startup.shutdownSubscriber: Tests\Fixtures\FakePresenterShutdownSubscriber
', 'neon'));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2application', new EventApplicationBridgeExtension());
}, __FILE__ . '2');

/** @var Container $container */
$container = new $class();
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Neonkit::load(<<<'NEON'
services:
- Nette\Application\Routers\RouteList
fake.startup.startupSubscriber: Tests\Fixtures\FakeStartupSubscriber
fake.presenter.startup.startupSubscriber: Tests\Fixtures\FakePresenterStartupSubscriber
fake.presenter.startup.shutdownSubscriber: Tests\Fixtures\FakePresenterShutdownSubscriber
NEON
));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2application', new EventApplicationBridgeExtension());
})->build();

// Subscriber is still not created
Assert::false($container->isCreated('fake.startup.startupSubscriber'));
Expand Down
85 changes: 39 additions & 46 deletions tests/Cases/DI/EventLatteBridgeExtension.phpt
@@ -1,53 +1,48 @@
<?php declare(strict_types = 1);

/**
* Test: DI\EventLatteBridgeExtension
*/

use Contributte\EventDispatcher\DI\EventDispatcherExtension;
use Contributte\Events\Extra\DI\EventLatteBridgeExtension;
use Contributte\Tester\Environment;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Neonkit;
use Nette\Application\Application;
use Nette\Bridges\ApplicationDI\ApplicationExtension;
use Nette\Bridges\ApplicationDI\LatteExtension;
use Nette\Bridges\ApplicationLatte\LatteFactory;
use Nette\Bridges\ApplicationLatte\TemplateFactory;
use Nette\Bridges\HttpDI\HttpExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\FakeLatteCompileSubscriber;
use Tests\Fixtures\FakeTemplateCreateSubscriber;

require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
Assert::exception(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$loader->load(function (Compiler $compiler): void {
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
}, __FILE__ . '1');
ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
})->build();
}, LogicException::class, 'Service of type "Nette\Bridges\ApplicationLatte\LatteFactory" is needed. Please register it.');
});

test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->loadConfig(FileMock::create('
services:
- Nette\Application\Routers\RouteList
fake.latte.compile.subscriber: Tests\Fixtures\FakeLatteCompileSubscriber
', 'neon'));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('latte', new LatteExtension(TEMP_DIR));
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
}, __FILE__ . '2');

/** @var Container $container */
$container = new $class();
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Neonkit::load(<<<'NEON'
services:
- Nette\Application\Routers\RouteList
fake.latte.compile.subscriber: Tests\Fixtures\FakeLatteCompileSubscriber
NEON
));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('latte', new LatteExtension(Environment::getTestDir()));
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
})->build();

// Subscriber is still not created
Assert::false($container->isCreated('fake.latte.compile.subscriber'));
Expand All @@ -68,23 +63,21 @@ test(function (): void {
Assert::equal($engine, $subscriber->onCall[0]->getEngine());
});

test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->loadConfig(FileMock::create('
services:
- Nette\Application\Routers\RouteList
fake.template.create.subscriber: Tests\Fixtures\FakeTemplateCreateSubscriber
', 'neon'));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('latte', new LatteExtension(TEMP_DIR));
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
}, __FILE__ . '3');

/** @var Container $container */
$container = new $class();
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Neonkit::load(<<<'NEON'
services:
- Nette\Application\Routers\RouteList
fake.template.create.subscriber: Tests\Fixtures\FakeTemplateCreateSubscriber
NEON
));
$compiler->addExtension('application', new ApplicationExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('latte', new LatteExtension(Environment::getTestDir()));
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2latte', new EventLatteBridgeExtension());
})->build();

// Subscriber is still not created
Assert::false($container->isCreated('fake.template.create.subscriber'));
Expand Down
54 changes: 24 additions & 30 deletions tests/Cases/DI/EventSecurityBridgeExtension.phpt
@@ -1,50 +1,44 @@
<?php declare(strict_types = 1);

/**
* Test: DI\EventSecurityBridgeExtension
*/

use Contributte\EventDispatcher\DI\EventDispatcherExtension;
use Contributte\Events\Extra\DI\EventSecurityBridgeExtension;
use Contributte\Tester\Toolkit;
use Contributte\Tester\Utils\ContainerBuilder;
use Contributte\Tester\Utils\Neonkit;
use Nette\Bridges\HttpDI\HttpExtension;
use Nette\Bridges\HttpDI\SessionExtension;
use Nette\Bridges\SecurityDI\SecurityExtension;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\Security\Identity;
use Nette\Security\SimpleIdentity;
use Nette\Security\User;
use Tester\Assert;
use Tester\FileMock;
use Tests\Fixtures\FakeLoggedInSubscriber;

require_once __DIR__ . '/../../bootstrap.php';

test(function (): void {
Toolkit::test(function (): void {
Assert::exception(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$loader->load(function (Compiler $compiler): void {
$compiler->addExtension('events2security', new EventSecurityBridgeExtension());
}, __FILE__ . '1');
ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addExtension('events2security', new EventSecurityBridgeExtension());
})->build();
}, LogicException::class, 'Service of type "Nette\Security\User" is needed. Please register it.');
});

test(function (): void {
$loader = new ContainerLoader(TEMP_DIR, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->loadConfig(FileMock::create('
services:
fake.loggedin.subscriber: Tests\Fixtures\FakeLoggedInSubscriber
', 'neon'));
$compiler->addExtension('security', new SecurityExtension());
$compiler->addExtension('session', new SessionExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2security', new EventSecurityBridgeExtension());
}, __FILE__ . '2');

/** @var Container $container */
$container = new $class();
Toolkit::test(function (): void {
$container = ContainerBuilder::of()
->withCompiler(function (Compiler $compiler): void {
$compiler->addConfig(Neonkit::load(<<<'NEON'
services:
fake.loggedin.subscriber: Tests\Fixtures\FakeLoggedInSubscriber
NEON
));
$compiler->addExtension('security', new SecurityExtension());
$compiler->addExtension('session', new SessionExtension());
$compiler->addExtension('http', new HttpExtension());
$compiler->addExtension('events', new EventDispatcherExtension());
$compiler->addExtension('events2security', new EventSecurityBridgeExtension());
})->build();

// Subscriber is still not created
Assert::false($container->isCreated('fake.loggedin.subscriber'));
Expand All @@ -55,7 +49,7 @@ test(function (): void {

/** @var FakeLoggedInSubscriber $subscriber */
$subscriber = $container->getByType(FakeLoggedInSubscriber::class);
$user->login(new Identity(1));
$user->login(new SimpleIdentity(1));

Assert::count(1, $subscriber->onCall);
Assert::equal($user, $subscriber->onCall[0]->getUser());
Expand Down
7 changes: 2 additions & 5 deletions tests/Cases/UI/UIEvent.phpt
@@ -1,16 +1,13 @@
<?php declare(strict_types = 1);

/**
* Test: UI/UIEvent
*/

use Contributte\Tester\Toolkit;
use Tester\Assert;
use Tests\Fixtures\FakeUiEvent;

require_once __DIR__ . '/../../bootstrap.php';

// Success message
test(function (): void {
Toolkit::test(function (): void {
$event = new FakeUiEvent();
$event->getUi()->addSuccessMessage('All clear');

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakeLatteCompileSubscriber.php
Expand Up @@ -9,7 +9,7 @@ final class FakeLatteCompileSubscriber implements EventSubscriberInterface
{

/** @var LatteCompileEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakeLoggedInSubscriber.php
Expand Up @@ -9,7 +9,7 @@ final class FakeLoggedInSubscriber implements EventSubscriberInterface
{

/** @var LoggedInEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakePresenterShutdownSubscriber.php
Expand Up @@ -9,7 +9,7 @@ class FakePresenterShutdownSubscriber implements EventSubscriberInterface
{

/** @var PresenterShutdownEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakePresenterStartupSubscriber.php
Expand Up @@ -9,7 +9,7 @@ class FakePresenterStartupSubscriber implements EventSubscriberInterface
{

/** @var PresenterStartupEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakeStartupSubscriber.php
Expand Up @@ -9,7 +9,7 @@ final class FakeStartupSubscriber implements EventSubscriberInterface
{

/** @var StartupEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/FakeTemplateCreateSubscriber.php
Expand Up @@ -9,7 +9,7 @@ final class FakeTemplateCreateSubscriber implements EventSubscriberInterface
{

/** @var TemplateCreateEvent[] */
public $onCall = [];
public array $onCall = [];

/**
* @return string[]
Expand Down

0 comments on commit f72b954

Please sign in to comment.