-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScenarioShowcaseTest.php
More file actions
87 lines (78 loc) · 3.33 KB
/
ScenarioShowcaseTest.php
File metadata and controls
87 lines (78 loc) · 3.33 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
83
84
85
86
87
<?php
declare(strict_types=1);
namespace Demo;
use Backslash\Scenario\Play;
use Backslash\Scenario\PublishedEvents;
use Backslash\Scenario\UpdatedProjections;
use Demo\Feature\CourseDefinition\Event\CourseDefinedEvent;
use Demo\Feature\CourseSubscription\Command\SubscribeStudentToCourseCommand;
use Demo\Feature\CourseSubscription\Command\UnsubscribeStudentFromCourseCommand;
use Demo\Feature\CourseSubscription\Event\StudentSubscribedToCourseEvent;
use Demo\Feature\CourseSubscription\Event\StudentUnsubscribedFromCourseEvent;
use Demo\Feature\CourseSubscription\Exception\StudentNotSubscribedToCourseException;
use Demo\Feature\CourseView\Projection\CourseProjection;
use Demo\Feature\StudentRegistration\Command\RegisterStudentCommand;
use Demo\Feature\StudentView\Projection\StudentProjection;
use Demo\Infrastructure\TestCase;
use PHPUnit\Framework\Attributes\Test;
/**
* This test showcases most commonly used assertions provided by Scenario
*/
class ScenarioShowcaseTest extends TestCase
{
#[Test]
public function test_demo(): void
{
$studentId = '1';
$courseId = '2';
$subscribe = new Play()
->given(
new CourseDefinedEvent($courseId, 'Maths', 30),
new RegisterStudentCommand($studentId, 'John'),
)
->when(
new SubscribeStudentToCourseCommand($studentId, $courseId),
)
->when(function (): void {
define('MY_CONSTANT', 'some-value');
})
->then(function (PublishedEvents $events) use ($studentId, $courseId): void {
$this->assertPublishedEventsCount(1, $events);
$this->assertPublishedEventsContainOnly(StudentSubscribedToCourseEvent::class, $events);
$this->assertPublishedEventsDoNotContain(StudentUnsubscribedFromCourseEvent::class, $events);
/** @var StudentSubscribedToCourseEvent $event */
$event = $events->getAllOf(StudentSubscribedToCourseEvent::class)[0]->getEvent();
$this->assertEquals($studentId, $event->studentId);
$this->assertEquals($courseId, $event->courseId);
})
->then(function (UpdatedProjections $projections): void {
$this->assertUpdatedProjectionsCount(2, $projections);
$this->assertUpdatedProjectionsContainExactly([
CourseProjection::class => 1,
StudentProjection::class => 1,
], $projections);
})
->then(function (): void {
defined('MY_CONSTANT');
});
$unsubscribe = new Play()
->when(
new UnsubscribeStudentFromCourseCommand($studentId, $courseId),
)
->then(function (PublishedEvents $events) use ($studentId, $courseId): void {
$this->assertPublishedEventsContainExactly([
StudentUnsubscribedFromCourseEvent::class => 1,
], $events);
});
$oops = new Play()
->when(
new UnsubscribeStudentFromCourseCommand($studentId, $courseId),
)
->thenExpectException(StudentNotSubscribedToCourseException::class);
$this->scenario->play(
$subscribe,
$unsubscribe,
$oops,
);
}
}