Skip to content

Commit 4135735

Browse files
committed
Tests: refactor
1 parent a3a8ce9 commit 4135735

8 files changed

Lines changed: 47 additions & 102 deletions

File tree

‎tests/.coveralls.yml‎

Lines changed: 0 additions & 4 deletions
This file was deleted.

‎tests/.gitignore‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎tests/Cases/DI/EventExtension.phpt‎

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
<?php declare(strict_types = 1);
22

3-
/**
4-
* Test: DI\EventDispatcherExtensions
5-
*/
6-
73
use Contributte\EventDispatcher\DI\EventDispatcherExtension;
4+
use Contributte\Tester\Toolkit;
5+
use Contributte\Tester\Utils\ContainerBuilder;
86
use Nette\DI\Compiler;
9-
use Nette\DI\Container;
10-
use Nette\DI\ContainerLoader;
117
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
128
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
139
use Symfony\Contracts\EventDispatcher\Event;
@@ -20,18 +16,15 @@ use Tests\Fixtures\PrioritizedSubscriber;
2016
require_once __DIR__ . '/../../bootstrap.php';
2117

2218
// Dispatch event with NO defined subscriber for event
23-
test(function (): void {
24-
$loader = new ContainerLoader(TEMP_DIR, true);
25-
$class = $loader->load(function (Compiler $compiler): void {
26-
$compiler->addExtension('events', new EventDispatcherExtension());
27-
$compiler->loadConfig(FileMock::create('
28-
services:
29-
foo: Tests\Fixtures\FooSubscriber
30-
', 'neon'));
31-
}, 1);
32-
33-
/** @var Container $container */
34-
$container = new $class();
19+
Toolkit::test(function (): void {
20+
$container = ContainerBuilder::of()
21+
->withCompiler(function (Compiler $compiler): void {
22+
$compiler->addExtension('events', new EventDispatcherExtension());
23+
$compiler->loadConfig(FileMock::create('
24+
services:
25+
foo: Tests\Fixtures\FooSubscriber
26+
', 'neon'));
27+
})->build();
3528

3629
/** @var EventDispatcherInterface $em */
3730
$em = $container->getByType(EventDispatcherInterface::class);
@@ -57,18 +50,15 @@ test(function (): void {
5750
});
5851

5952
// Dispatch event with defined subscriber
60-
test(function (): void {
61-
$loader = new ContainerLoader(TEMP_DIR, true);
62-
$class = $loader->load(function (Compiler $compiler): void {
63-
$compiler->addExtension('events', new EventDispatcherExtension());
64-
$compiler->loadConfig(FileMock::create('
53+
Toolkit::test(function (): void {
54+
$container = ContainerBuilder::of()
55+
->withCompiler(function (Compiler $compiler): void {
56+
$compiler->addExtension('events', new EventDispatcherExtension());
57+
$compiler->loadConfig(FileMock::create('
6558
services:
6659
foo: Tests\Fixtures\FooSubscriber
6760
', 'neon'));
68-
}, 2);
69-
70-
/** @var Container $container */
71-
$container = new $class();
61+
})->build();
7262

7363
/** @var EventDispatcherInterface $em */
7464
$em = $container->getByType(EventDispatcherInterface::class);
@@ -101,36 +91,30 @@ test(function (): void {
10191
});
10292

10393
// Register multiple subscribers
104-
test(function (): void {
105-
$loader = new ContainerLoader(TEMP_DIR, true);
106-
$class = $loader->load(function (Compiler $compiler): void {
107-
$compiler->addExtension('events', new EventDispatcherExtension());
108-
$compiler->loadConfig(FileMock::create('
94+
Toolkit::test(function (): void {
95+
$container = ContainerBuilder::of()
96+
->withCompiler(function (Compiler $compiler): void {
97+
$compiler->addExtension('events', new EventDispatcherExtension());
98+
$compiler->loadConfig(FileMock::create('
10999
services:
110100
foo: Tests\Fixtures\FooSubscriber
111101
bar: Tests\Fixtures\BarSubscriber
112102
', 'neon'));
113-
}, 3);
114-
115-
/** @var Container $container */
116-
$container = new $class();
103+
})->build();
117104

118105
Assert::count(2, $container->findByType(EventSubscriberInterface::class));
119106
});
120107

121108
// Register subscriber with more events
122-
test(function (): void {
123-
$loader = new ContainerLoader(TEMP_DIR, true);
124-
$class = $loader->load(function (Compiler $compiler): void {
125-
$compiler->addExtension('events', new EventDispatcherExtension());
126-
$compiler->loadConfig(FileMock::create('
109+
Toolkit::test(function (): void {
110+
$container = ContainerBuilder::of()
111+
->withCompiler(function (Compiler $compiler): void {
112+
$compiler->addExtension('events', new EventDispatcherExtension());
113+
$compiler->loadConfig(FileMock::create('
127114
services:
128115
multi: Tests\Fixtures\MultiSubscriber
129116
', 'neon'));
130-
}, 4);
131-
132-
/** @var Container $container */
133-
$container = new $class();
117+
})->build();
134118

135119
/** @var EventDispatcherInterface $em */
136120
$em = $container->getByType(EventDispatcherInterface::class);
@@ -154,18 +138,15 @@ test(function (): void {
154138
});
155139

156140
// Register prioritized subscriber
157-
test(function (): void {
158-
$loader = new ContainerLoader(TEMP_DIR, true);
159-
$class = $loader->load(function (Compiler $compiler): void {
160-
$compiler->addExtension('events', new EventDispatcherExtension());
161-
$compiler->loadConfig(FileMock::create('
141+
Toolkit::test(function (): void {
142+
$container = ContainerBuilder::of()
143+
->withCompiler(function (Compiler $compiler): void {
144+
$compiler->addExtension('events', new EventDispatcherExtension());
145+
$compiler->loadConfig(FileMock::create('
162146
services:
163147
prioritized: Tests\Fixtures\PrioritizedSubscriber
164148
', 'neon'));
165-
}, 5);
166-
167-
/** @var Container $container */
168-
$container = new $class();
149+
})->build();
169150

170151
/** @var EventDispatcherInterface $em */
171152
$em = $container->getByType(EventDispatcherInterface::class);
@@ -186,15 +167,12 @@ test(function (): void {
186167
});
187168

188169
// Dispatch event with NO subscribers at all
189-
test(function (): void {
190-
$loader = new ContainerLoader(TEMP_DIR, true);
191-
$class = $loader->load(function (Compiler $compiler): void {
192-
$compiler->addExtension('events', new EventDispatcherExtension());
193-
$compiler->loadConfig(FileMock::create('', 'neon'));
194-
}, 6);
195-
196-
/** @var Container $container */
197-
$container = new $class();
170+
Toolkit::test(function (): void {
171+
$container = ContainerBuilder::of()
172+
->withCompiler(function (Compiler $compiler): void {
173+
$compiler->addExtension('events', new EventDispatcherExtension());
174+
$compiler->loadConfig(FileMock::create('', 'neon'));
175+
})->build();
198176

199177
/** @var EventDispatcherInterface $em */
200178
$em = $container->getByType(EventDispatcherInterface::class);

‎tests/Fixtures/BarSubscriber.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class BarSubscriber implements EventSubscriberInterface
99
{
1010

1111
/** @var Event[] */
12-
public $onCall = [];
12+
public array $onCall = [];
1313

1414
/**
1515
* @return mixed[] The event names to listen to

‎tests/Fixtures/FooSubscriber.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class FooSubscriber implements EventSubscriberInterface
99
{
1010

1111
/** @var Event[] */
12-
public $onCall = [];
12+
public array $onCall = [];
1313

1414
/**
1515
* @return mixed[] The event names to listen to

‎tests/Fixtures/MultiSubscriber.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class MultiSubscriber implements EventSubscriberInterface
99
{
1010

1111
/** @var Event[] */
12-
public $onCall = [];
12+
public array $onCall = [];
1313

1414
/**
1515
* @return mixed[] The event names to listen to

‎tests/Fixtures/PrioritizedSubscriber.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ final class PrioritizedSubscriber implements EventSubscriberInterface
99
{
1010

1111
/** @var Event[] */
12-
public $onCall = [];
12+
public array $onCall = [];
1313

1414
/**
1515
* @return mixed[] The event names to listen to

‎tests/bootstrap.php‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
<?php declare(strict_types = 1);
22

3-
use Tester\Environment;
4-
use Tester\Helpers;
3+
use Contributte\Tester\Environment;
54

65
if (@!include __DIR__ . '/../vendor/autoload.php') {
76
echo 'Install Nette Tester using `composer update --dev`';
87
exit(1);
98
}
109

11-
// Configure environment
12-
Environment::setup();
13-
date_default_timezone_set('Europe/Prague');
14-
15-
// Create temporary directory
16-
define('TMP_DIR', __DIR__ . '/tmp');
17-
@mkdir(TMP_DIR, 0777, true);
18-
define('CACHE_DIR', TMP_DIR . '/cache');
19-
@mkdir(CACHE_DIR, 0777, true);
20-
define('TEMP_DIR', TMP_DIR . '/cases/' . getmypid());
21-
@mkdir(TEMP_DIR, 0777, true);
22-
23-
// Purge temporary directory
24-
Helpers::purge(TEMP_DIR);
25-
26-
function test(Closure $function): void
27-
{
28-
$function();
29-
}
10+
Environment::setup(__DIR__);

0 commit comments

Comments
 (0)