Skip to content

Commit 10f5f1e

Browse files
committed
Tests: modernize
1 parent 88fcc1b commit 10f5f1e

8 files changed

+50
-107
lines changed

tests/cases/DI/AnnotationsExtensionTest.php tests/Cases/DI/AnnotationsExtensionTest.php

+31-41
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
namespace Tests\Cases\DI;
44

5+
use Contributte\Tester\Environment;
6+
use Contributte\Tester\Utils\ContainerBuilder;
7+
use Contributte\Tester\Utils\Neonkit;
58
use Doctrine\Common\Annotations\CachedReader;
69
use Doctrine\Common\Annotations\Reader;
710
use Doctrine\Common\Cache\ApcuCache;
811
use Doctrine\Common\Cache\Cache;
912
use Doctrine\Common\Cache\PhpFileCache;
10-
use Exception;
1113
use Nette\DI\Compiler;
12-
use Nette\DI\Container;
13-
use Nette\DI\ContainerLoader;
1414
use Nette\InvalidStateException;
1515
use Nettrine\Annotations\DI\AnnotationsExtension;
1616
use Nettrine\Cache\DI\CacheExtension;
1717
use Tester\Assert;
1818
use Tester\TestCase;
19-
use Tests\Toolkit\NeonLoader;
20-
use Tests\Toolkit\Tests;
2119

2220
require __DIR__ . '/../../bootstrap.php';
2321

@@ -26,39 +24,35 @@ final class AnnotationsExtensionTest extends TestCase
2624

2725
public function testAutowiredCache(): void
2826
{
29-
$loader = new ContainerLoader(Tests::TEMP_PATH, true);
30-
$class = $loader->load(static function (Compiler $compiler): void {
31-
$compiler->addExtension('annotations', new AnnotationsExtension());
32-
$compiler->addExtension('cache', new CacheExtension());
33-
$compiler->addConfig([
34-
'parameters' => [
35-
'tempDir' => Tests::TEMP_PATH,
36-
],
37-
]);
38-
$compiler->addDependencies([__FILE__]);
39-
}, __METHOD__);
40-
41-
$container = new $class();
42-
assert($container instanceof Container);
27+
$container = ContainerBuilder::of()
28+
->withCompiler(function (Compiler $compiler): void {
29+
$compiler->addExtension('annotations', new AnnotationsExtension());
30+
$compiler->addExtension('cache', new CacheExtension());
31+
$compiler->addConfig([
32+
'parameters' => [
33+
'tempDir' => Environment::getTestDir(),
34+
],
35+
]);
36+
$compiler->addDependencies([__FILE__]);
37+
})
38+
->build();
4339

4440
Assert::type(CachedReader::class, $container->getByType(Reader::class));
4541
Assert::type(PhpFileCache::class, $container->getService('cache.driver'));
4642
}
4743

4844
public function testProvidedCache(): void
4945
{
50-
$loader = new ContainerLoader(Tests::TEMP_PATH, true);
51-
$class = $loader->load(static function (Compiler $compiler): void {
52-
$compiler->addExtension('annotations', new AnnotationsExtension());
53-
$compiler->addConfig(NeonLoader::load('
54-
annotations:
55-
cache: Doctrine\Common\Cache\ApcuCache
56-
'));
57-
$compiler->addDependencies([__FILE__]);
58-
}, __METHOD__);
59-
60-
$container = new $class();
61-
assert($container instanceof Container);
46+
$container = ContainerBuilder::of()
47+
->withCompiler(function (Compiler $compiler): void {
48+
$compiler->addExtension('annotations', new AnnotationsExtension());
49+
$compiler->addConfig(Neonkit::load('
50+
annotations:
51+
cache: Doctrine\Common\Cache\ApcuCache
52+
'));
53+
$compiler->addDependencies([__FILE__]);
54+
})
55+
->build();
6256

6357
Assert::type(CachedReader::class, $container->getByType(Reader::class));
6458
Assert::type(ApcuCache::class, $container->getService('annotations.cache'));
@@ -67,17 +61,13 @@ public function testProvidedCache(): void
6761

6862
public function testNoCache(): void
6963
{
70-
Assert::type(new Exception(), new InvalidStateException());
71-
7264
Assert::exception(function (): void {
73-
$loader = new ContainerLoader(Tests::TEMP_PATH, true);
74-
$class = $loader->load(static function (Compiler $compiler): void {
75-
$compiler->addExtension('annotations', new AnnotationsExtension());
76-
$compiler->addDependencies([__FILE__]);
77-
}, __METHOD__);
78-
79-
$container = new $class();
80-
Assert::true($container instanceof Container);
65+
ContainerBuilder::of()
66+
->withCompiler(function (Compiler $compiler): void {
67+
$compiler->addExtension('annotations', new AnnotationsExtension());
68+
$compiler->addDependencies([__FILE__]);
69+
})
70+
->build();
8171
}, InvalidStateException::class, "Service 'annotations.reader' (type of Doctrine\Common\Annotations\Reader): Service of type Doctrine\Common\Cache\Cache not found. Did you add it to configuration file?");
8272
}
8373

tests/cases/Reader/ReaderTest.php tests/Cases/Reader/ReaderTest.php

+17-20
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace Tests\Cases\Reader;
44

5+
use Contributte\Tester\Environment;
6+
use Contributte\Tester\Utils\ContainerBuilder;
7+
use Contributte\Tester\Utils\Neonkit;
58
use Doctrine\Common\Annotations\Reader;
69
use Nette\DI\Compiler;
7-
use Nette\DI\Container;
8-
use Nette\DI\ContainerLoader;
910
use Nettrine\Annotations\DI\AnnotationsExtension;
1011
use ReflectionClass;
1112
use Tester\Assert;
1213
use Tester\TestCase;
1314
use Tests\Fixtures\SampleAnnotation;
1415
use Tests\Fixtures\SampleClass;
15-
use Tests\Toolkit\NeonLoader;
16-
use Tests\Toolkit\Tests;
1716

1817
require __DIR__ . '/../../bootstrap.php';
1918

@@ -22,24 +21,22 @@ final class ReaderTest extends TestCase
2221

2322
public function testIgnoreAnnotations(): void
2423
{
25-
$loader = new ContainerLoader(Tests::TEMP_PATH, true);
26-
$class = $loader->load(function (Compiler $compiler): void {
27-
$compiler->addExtension('annotations', new AnnotationsExtension());
28-
$compiler->addConfig(['parameters' => ['tempDir' => Tests::TEMP_PATH]]);
29-
$compiler->addConfig(NeonLoader::load('
30-
annotations:
31-
cache: Doctrine\Common\Cache\FilesystemCache(%tempDir%/nettrine.annotations)
32-
ignore:
33-
- ignoredAnnotation
34-
'));
35-
$compiler->addDependencies([__FILE__]);
36-
}, __METHOD__);
37-
38-
$container = new $class();
39-
Assert::true($container instanceof Container);
24+
$container = ContainerBuilder::of()
25+
->withCompiler(function (Compiler $compiler): void {
26+
$compiler->addExtension('annotations', new AnnotationsExtension());
27+
$compiler->addConfig(['parameters' => ['tempDir' => Environment::getTestDir()]]);
28+
$compiler->addConfig(Neonkit::load('
29+
annotations:
30+
cache: Doctrine\Common\Cache\FilesystemCache(%tempDir%/nettrine.annotations)
31+
ignore:
32+
- ignoredAnnotation
33+
'));
34+
$compiler->addDependencies([__FILE__]);
35+
})
36+
->build();
4037

4138
$reader = $container->getByType(Reader::class);
42-
Assert::true($reader instanceof Reader);
39+
Assert::type(Reader::class, $reader);
4340

4441
$annotations = $reader->getClassAnnotations(new ReflectionClass(SampleClass::class));
4542

tests/fixtures/SampleAnnotation.php tests/Fixtures/SampleAnnotation.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
class SampleAnnotation
1212
{
1313

14-
/** @var string|NULL */
15-
private $value;
14+
private string|null $value;
1615

1716
/**
1817
* @param string[] $values
File renamed without changes.

tests/bootstrap.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<?php declare(strict_types = 1);
22

3-
use Ninjify\Nunjuck\Environment;
3+
use Contributte\Tester\Environment;
44

55
if (@!include __DIR__ . '/../vendor/autoload.php') {
66
echo 'Install Nette Tester using `composer update --dev`';
77
exit(1);
88
}
99

10-
// Configure environment
1110
Environment::setup(__DIR__);

tests/coverage.xml

-11
This file was deleted.

tests/toolkit/NeonLoader.php

-19
This file was deleted.

tests/toolkit/Tests.php

-12
This file was deleted.

0 commit comments

Comments
 (0)