Skip to content

Commit 9eeca6d

Browse files
committed
Test logger
1 parent 581419b commit 9eeca6d

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

test/Logger/LoggerTest.php

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\PhpWorkshopTest\Logger;
6+
7+
use PhpSchool\PhpWorkshopTest\ContainerAwareTest;
8+
use Psr\Log\LoggerInterface;
9+
10+
class LoggerTest extends ContainerAwareTest
11+
{
12+
public function setUp(): void
13+
{
14+
parent::setUp();
15+
16+
$this->container->set('phpschoolGlobalDir', $this->getTemporaryDirectory());
17+
$this->container->set('appName', 'my-workshop');
18+
}
19+
20+
public function testLoggerDoesNotCreateFileIfNoMessageIsLogged(): void
21+
{
22+
$expectedFileName = sprintf("%s/logs/my-workshop.log", $this->getTemporaryDirectory());
23+
24+
$logger = $this->container->get(LoggerInterface::class);
25+
26+
$this->assertFileDoesNotExist($expectedFileName);
27+
}
28+
29+
public function testLoggerCreatesFileWhenMessageIsLogged(): void
30+
{
31+
$expectedFileName = sprintf("%s/logs/my-workshop.log", $this->getTemporaryDirectory());
32+
33+
$logger = $this->container->get(LoggerInterface::class);
34+
$logger->critical('Failed to copy file');
35+
36+
$this->assertFileExists($expectedFileName);
37+
$this->assertMatchesRegularExpression(
38+
'/Time\: \d{2}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}, Level\: critical, Message\: Failed to copy file, Context\: \[\]/',
39+
file_get_contents($expectedFileName)
40+
);
41+
}
42+
43+
public function testLoggerAppendsToFileWhenSecondMessageIsLogged(): void
44+
{
45+
$expectedFileName = sprintf("%s/logs/my-workshop.log", $this->getTemporaryDirectory());
46+
47+
$logger = $this->container->get(LoggerInterface::class);
48+
$logger->critical('Failed to copy file');
49+
$logger->emergency('Second error');
50+
51+
$this->assertFileExists($expectedFileName);
52+
$match = '/Time\: \d{2}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}, Level\: critical, Message\: Failed to copy file,';
53+
$match .= ' Context\: \[\]' . "\n\n";
54+
$match .= 'Time\: \d{2}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}, Level\: emergency, Message\: Second error,';
55+
$match .= ' Context\: \[\]' . "\n\n/";
56+
57+
$this->assertMatchesRegularExpression(
58+
$match,
59+
file_get_contents($expectedFileName)
60+
);
61+
}
62+
63+
public function testLoggerAppendsToFileWhenItAlreadyExists(): void
64+
{
65+
$expectedFileName = sprintf("%s/logs/my-workshop.log", $this->getTemporaryDirectory());
66+
67+
mkdir(dirname($expectedFileName), 0777, true);
68+
file_put_contents($expectedFileName, "Please do not overwrite me\n\n");
69+
70+
$logger = $this->container->get(LoggerInterface::class);
71+
$logger->emergency('Second error');
72+
73+
$this->assertFileExists($expectedFileName);
74+
$match = '/Please do not overwrite me' . "\n\n";
75+
$match .= 'Time\: \d{2}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}, Level\: emergency, Message\: Second error,';
76+
$match .= ' Context\: \[\]' . "\n\n/";
77+
78+
$this->assertMatchesRegularExpression(
79+
$match,
80+
file_get_contents($expectedFileName)
81+
);
82+
}
83+
84+
public function testLoggerWithContextIsEncoded(): void
85+
{
86+
$expectedFileName = sprintf("%s/logs/my-workshop.log", $this->getTemporaryDirectory());
87+
88+
$logger = $this->container->get(LoggerInterface::class);
89+
$logger->critical('Failed to copy file', ['exercise' => 'my-exercise']);
90+
91+
$this->assertFileExists($expectedFileName);
92+
93+
$match = '/Time\: \d{2}\-\d{2}\-\d{2} \d{2}\:\d{2}\:\d{2}, Level\: critical, Message\: Failed to copy file, ';
94+
$match .= 'Context\: {"exercise":"my-exercise"}/';
95+
$this->assertMatchesRegularExpression(
96+
$match,
97+
file_get_contents($expectedFileName)
98+
);
99+
}
100+
}

0 commit comments

Comments
 (0)