Skip to content

Commit 91d8e92

Browse files
authored
Introduce exercise scenario objects (#284)
Introduce DTO's which describe the exercise scenario. Will be used by exercises in the future instead of `getArgs()`, `getRequests()` etc. We also can use it to define files which should be created in the execution directory.
2 parents 807b3a6 + 5b124b4 commit 91d8e92

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

Diff for: src/Exercise/Scenario/CgiScenario.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise\Scenario;
4+
5+
use Psr\Http\Message\RequestInterface;
6+
7+
class CgiScenario extends ExerciseScenario
8+
{
9+
/**
10+
* @var array<RequestInterface>
11+
*/
12+
private array $executions = [];
13+
14+
public function withExecution(RequestInterface $request): self
15+
{
16+
$this->executions[] = $request;
17+
18+
return $this;
19+
}
20+
21+
/**
22+
* @return array<RequestInterface>
23+
*/
24+
public function getExecutions(): array
25+
{
26+
return $this->executions;
27+
}
28+
}

Diff for: src/Exercise/Scenario/CliScenario.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise\Scenario;
4+
5+
use PhpSchool\PhpWorkshop\Utils\Collection;
6+
7+
class CliScenario extends ExerciseScenario
8+
{
9+
/**
10+
* @var array<Collection<int, string>>
11+
*/
12+
private array $executions = [];
13+
14+
/**
15+
* @param array<string> $args
16+
*/
17+
public function withExecution(array $args = []): static
18+
{
19+
$this->executions[] = new Collection($args);
20+
21+
return $this;
22+
}
23+
24+
/**
25+
* @return array<Collection<int, string>>
26+
*/
27+
public function getExecutions(): array
28+
{
29+
return $this->executions;
30+
}
31+
}

Diff for: src/Exercise/Scenario/ExerciseScenario.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshop\Exercise\Scenario;
4+
5+
abstract class ExerciseScenario
6+
{
7+
/**
8+
* @var array<string, string>
9+
*/
10+
private array $files = [];
11+
12+
public function withFile(string $relativeFileName, string $content): static
13+
{
14+
$this->files[$relativeFileName] = $content;
15+
16+
return $this;
17+
}
18+
19+
/**
20+
* @return array<string, string>
21+
*/
22+
public function getFiles(): array
23+
{
24+
return $this->files;
25+
}
26+
}

Diff for: test/Exercise/Scenario/CgiScenarioTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Exercise\Scenario;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CgiScenario;
6+
use PhpSchool\PhpWorkshop\Utils\Collection;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\RequestInterface;
9+
10+
class CgiScenarioTest extends TestCase
11+
{
12+
public function testScenario(): void
13+
{
14+
$requestOne = $this->createMock(RequestInterface::class);
15+
$requestTwo = $this->createMock(RequestInterface::class);
16+
17+
$scenario = (new CgiScenario())
18+
->withFile('file1.txt', 'content1')
19+
->withFile('file2.txt', 'content2')
20+
->withExecution($requestOne)
21+
->withExecution($requestTwo);
22+
23+
static::assertEquals(
24+
[
25+
'file1.txt' => 'content1',
26+
'file2.txt' => 'content2',
27+
],
28+
$scenario->getFiles()
29+
);
30+
31+
static::assertEquals(
32+
[
33+
$requestOne,
34+
$requestTwo
35+
],
36+
$scenario->getExecutions()
37+
);
38+
}
39+
}

Diff for: test/Exercise/Scenario/CliScenarioTest.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace PhpSchool\PhpWorkshopTest\Exercise\Scenario;
4+
5+
use PhpSchool\PhpWorkshop\Exercise\Scenario\CliScenario;
6+
use PhpSchool\PhpWorkshop\Utils\Collection;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class CliScenarioTest extends TestCase
10+
{
11+
public function testScenario(): void
12+
{
13+
$scenario = (new CliScenario())
14+
->withFile('file1.txt', 'content1')
15+
->withFile('file2.txt', 'content2')
16+
->withExecution(['arg1', 'arg2'])
17+
->withExecution(['arg3', 'arg4']);
18+
19+
static::assertEquals(
20+
[
21+
'file1.txt' => 'content1',
22+
'file2.txt' => 'content2',
23+
],
24+
$scenario->getFiles()
25+
);
26+
27+
static::assertEquals(
28+
[
29+
['arg1', 'arg2'],
30+
['arg3', 'arg4'],
31+
],
32+
array_map(
33+
fn (Collection $collection) => $collection->getArrayCopy(),
34+
$scenario->getExecutions()
35+
)
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)