Skip to content

Commit 9bbcaee

Browse files
committed
Add logging to initial code listener
1 parent 9eeca6d commit 9bbcaee

File tree

4 files changed

+78
-28
lines changed

4 files changed

+78
-28
lines changed

app/config.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@
8787
return [
8888
'appName' => basename($_SERVER['argv'][0]),
8989
'phpschoolGlobalDir' => sprintf('%s/.php-school', getenv('HOME')),
90+
'currentWorkingDirectory' => function (ContainerInterface $c) {
91+
return getcwd();
92+
},
9093
WorkshopType::class => WorkshopType::STANDARD(),
9194
Psr\Log\LoggerInterface::class => function (ContainerInterface $c) {
9295
$appName = $c->get('appName');
@@ -212,7 +215,7 @@
212215

213216
//Listeners
214217
InitialCodeListener::class => function (ContainerInterface $c) {
215-
return new InitialCodeListener(getcwd());
218+
return new InitialCodeListener($c->get('currentWorkingDirectory'), $c->get(LoggerInterface::class));
216219
},
217220
PrepareSolutionListener::class => create(),
218221
CodePatchListener::class => function (ContainerInterface $c) {

src/Listener/InitialCodeListener.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace PhpSchool\PhpWorkshop\Listener;
66

77
use PhpSchool\PhpWorkshop\Event\Event;
8+
use PhpSchool\PhpWorkshop\Exercise\ExerciseInterface;
89
use PhpSchool\PhpWorkshop\Exercise\ProvidesInitialCode;
910
use PhpSchool\PhpWorkshop\Solution\SolutionFile;
11+
use Psr\Log\LoggerInterface;
1012

1113
/**
1214
* Copy over any initial files for this exercise when
@@ -22,16 +24,23 @@ class InitialCodeListener
2224
*/
2325
private $workingDirectory;
2426

25-
public function __construct(string $workingDirectory)
27+
/**
28+
* @var LoggerInterface
29+
*/
30+
private $logger;
31+
32+
public function __construct(string $workingDirectory, LoggerInterface $logger)
2633
{
2734
$this->workingDirectory = $workingDirectory;
35+
$this->logger = $logger;
2836
}
2937

3038
/**
3139
* @param Event $event
3240
*/
3341
public function __invoke(Event $event): void
3442
{
43+
/** @var ExerciseInterface $exercise */
3544
$exercise = $event->getParameter('exercise');
3645

3746
if (!$exercise instanceof ProvidesInitialCode) {
@@ -42,7 +51,19 @@ public function __invoke(Event $event): void
4251
/** @var SolutionFile $file */
4352
if (!file_exists($this->workingDirectory . '/' . $file->getRelativePath())) {
4453
copy($file->getAbsolutePath(), $this->workingDirectory . '/' . $file->getRelativePath());
54+
$message = 'File successfully copied to working directory';
55+
} else {
56+
$message = 'File not copied. File with same name already exists in working directory';
4557
}
58+
59+
$this->logger->debug(
60+
$message,
61+
[
62+
'exercise' => $exercise->getName(),
63+
'workingDir' => $this->workingDirectory,
64+
'file' => $file->getAbsolutePath()
65+
]
66+
);
4667
}
4768
}
4869
}

test/Asset/ExerciseWithInitialCode.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ExerciseWithInitialCode implements ExerciseInterface, ProvidesInitialCode
1313
{
1414
public function getName(): string
1515
{
16-
// TODO: Implement getName() method.
16+
return 'exercise-with-initial-code';
1717
}
1818

1919
public function getDescription(): string

test/Listener/InitialCodeListenerTest.php

+51-25
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,16 @@
88
use PhpSchool\PhpWorkshop\Listener\InitialCodeListener;
99
use PhpSchool\PhpWorkshopTest\Asset\CliExerciseImpl;
1010
use PhpSchool\PhpWorkshopTest\Asset\ExerciseWithInitialCode;
11-
use PHPUnit\Framework\TestCase;
12-
use Symfony\Component\Filesystem\Filesystem;
11+
use PhpSchool\PhpWorkshopTest\ContainerAwareTest;
1312

14-
class InitialCodeListenerTest extends TestCase
13+
class InitialCodeListenerTest extends ContainerAwareTest
1514
{
16-
/**
17-
* @var Filesystem
18-
*/
19-
private $filesystem;
20-
21-
/**
22-
* @var string
23-
*/
24-
private $cwd;
25-
2615
public function setUp(): void
2716
{
28-
$this->filesystem = new Filesystem();
17+
parent::setUp();
2918

30-
$this->cwd = sprintf('%s/%s', str_replace('\\', '/', sys_get_temp_dir()), $this->getName());
31-
mkdir($this->cwd, 0775, true);
19+
$this->mockCurrentWorkingDirectory();
20+
$this->mockLogger();
3221
}
3322

3423
public function testExerciseCodeIsCopiedIfExerciseProvidesInitialCode(): void
@@ -37,30 +26,67 @@ public function testExerciseCodeIsCopiedIfExerciseProvidesInitialCode(): void
3726

3827
$event = new Event('exercise.selected', ['exercise' => $exercise]);
3928

40-
$listener = new InitialCodeListener($this->cwd);
29+
$listener = $this->container->get(InitialCodeListener::class);
4130
$listener->__invoke($event);
4231

43-
$this->assertFileExists($this->cwd . '/init-solution.php');
32+
$this->assertFileExists($this->getCurrentWorkingDirectory() . '/init-solution.php');
4433
$this->assertFileEquals(
4534
$exercise->getInitialCode()->getFiles()[0]->getAbsolutePath(),
46-
$this->cwd . '/init-solution.php'
35+
$this->getCurrentWorkingDirectory() . '/init-solution.php'
36+
);
37+
38+
$this->assertLoggerHasMessages(
39+
[
40+
[
41+
'level' => 'debug',
42+
'message' => 'File successfully copied to working directory',
43+
'context' => [
44+
'exercise' => 'exercise-with-initial-code',
45+
'workingDir' => $this->getCurrentWorkingDirectory(),
46+
'file' => $exercise->getInitialCode()->getFiles()[0]->getAbsolutePath()
47+
]
48+
]
49+
]
4750
);
4851
}
4952

50-
public function testExerciseCodeIsNotCopiedIfExerciseDoesNotProvideInitialCode(): void
53+
public function testExerciseCodeIsNotCopiedIfFileWithSameNameExistsInWorkingDirectory(): void
5154
{
52-
$exercise = new CliExerciseImpl();
55+
$exercise = new ExerciseWithInitialCode();
5356

5457
$event = new Event('exercise.selected', ['exercise' => $exercise]);
5558

56-
$listener = new InitialCodeListener($this->cwd);
59+
touch($this->getCurrentWorkingDirectory() . '/init-solution.php');
60+
61+
$listener = $this->container->get(InitialCodeListener::class);
5762
$listener->__invoke($event);
5863

59-
$this->assertEmpty(array_diff(scandir($this->cwd), ['.', '..']));
64+
$this->assertFileExists($this->getCurrentWorkingDirectory() . '/init-solution.php');
65+
66+
$this->assertLoggerHasMessages(
67+
[
68+
[
69+
'level' => 'debug',
70+
'message' => 'File not copied. File with same name already exists in working directory',
71+
'context' => [
72+
'exercise' => 'exercise-with-initial-code',
73+
'workingDir' => $this->getCurrentWorkingDirectory(),
74+
'file' => $exercise->getInitialCode()->getFiles()[0]->getAbsolutePath()
75+
]
76+
]
77+
]
78+
);
6079
}
6180

62-
public function tearDown(): void
81+
public function testExerciseCodeIsNotCopiedIfExerciseDoesNotProvideInitialCode(): void
6382
{
64-
$this->filesystem->remove($this->cwd);
83+
$exercise = new CliExerciseImpl();
84+
85+
$event = new Event('exercise.selected', ['exercise' => $exercise]);
86+
87+
$listener = $this->container->get(InitialCodeListener::class);
88+
$listener->__invoke($event);
89+
90+
$this->assertEmpty(array_diff(scandir($this->getCurrentWorkingDirectory()), ['.', '..']));
6591
}
6692
}

0 commit comments

Comments
 (0)