-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContainerAwareTest.php
More file actions
82 lines (66 loc) · 2.11 KB
/
ContainerAwareTest.php
File metadata and controls
82 lines (66 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace PhpSchool\PhpWorkshopTest;
use DI\ContainerBuilder;
use PHPUnit\Framework\ExpectationFailedException;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\Filesystem\Filesystem;
abstract class ContainerAwareTest extends BaseTest
{
/**
* @var ContainerInterface
*/
protected $container;
/**
* @var ?string
*/
private $currentWorkingDirectory;
public function setUp(): void
{
$containerConfig = __DIR__ . '/../app/config.php';
$containerBuilder = new ContainerBuilder();
$containerBuilder->addDefinitions(require $containerConfig);
$containerBuilder->useAutowiring(false);
$containerBuilder->useAnnotations(false);
$this->container = $containerBuilder->build();
}
public function mockLogger(): void
{
$this->container->set(LoggerInterface::class, new MockLogger());
}
public function mockCurrentWorkingDirectory(): void
{
$this->currentWorkingDirectory = $this->getTemporaryDirectory();
$this->container->set('currentWorkingDirectory', $this->currentWorkingDirectory);
}
public function getCurrentWorkingDirectory(): ?string
{
return $this->currentWorkingDirectory;
}
/**
* @param array<array{level: string, message: string, context: array<mixed>}> $messages
* @throws ExpectationFailedException
*/
public function assertLoggerHasMessages(array $messages): void
{
$logged = $this->container->get(LoggerInterface::class)->messages;
foreach ($messages as $message) {
$this->assertContains(
[
'level' => $message['level'],
'message' => $message['message'],
'context' => $message['context'],
],
$logged,
);
}
}
public function tearDown(): void
{
if ($this->currentWorkingDirectory) {
(new Filesystem())->remove($this->currentWorkingDirectory);
}
parent::tearDown();
}
}