Skip to content

Instantly share code, notes, and snippets.

@mlebkowski
Last active December 17, 2021 08:57
Show Gist options
  • Save mlebkowski/c22ba474b9b6fd06a95419e1164851ed to your computer and use it in GitHub Desktop.
Save mlebkowski/c22ba474b9b6fd06a95419e1164851ed to your computer and use it in GitHub Desktop.
Unit tests code style
<?php
// region $sut = new WaitingScriptFactory()
$sut = new WaitingScriptFactory(
new CallerContextFactory(
new StaticWaitingMusicRepository(self::MUSIC_URL),
new StaticLinePositionAnnouncementConfiguration(),
new StaticAnnouncementFactory($this->announcement),
),
new StaticResponseBuilderFactory($this->responseBuilder),
new AnnouncementScript(
new StaticVoiceSettingsRepository(),
new TestLogger(),
),
new TrimmedAudioFileFactory(
new SimpleMusicTrimmer(),
new StaticLinePositionAnnouncementConfiguration(),
new TestLogger(),
),
new StaticWaitingUrlGenerator(),
);
// endregion
<?php
private function and a redirect to next phase(WaitPhase $phase): void
{
$instruction = array_shift($this->responseBuilder->instructions);
self::assertNotNull($instruction, 'Missing expected redirect instruction');
self::assertSame('post', $instruction[0], 'Oops, there is no redirect to the next phase');
self::assertStringEndsWith($phase->getValue(), $instruction[1], 'Invalid phase detected for the next iteration');
}
private function i expect an announcement(): void
{
$instruction = array_shift($this->responseBuilder->instructions);
self::assertNotNull($instruction, 'Missing expected announce instruction');
self::assertSame('announce', $instruction[0], 'Expected an announce item in script');
self::assertSame(self::ANNOUNCEMENT_MESSAGE, $instruction[3], 'The announce message does not match');
$instruction = array_shift($this->responseBuilder->instructions);
self::assertNotNull($instruction, 'Missing expected pause instruction');
self::assertSame('pause', $instruction[0], 'The announcement should end in a pause');
}
<?php
public function test no announcement during the initial phase(): void
{
$this->given it’s only the initial phase();
$this->when i build();
$this->then i expect the waiting music to be cut to n seconds(3);
$this->and a redirect to next phase(WaitPhase::ANNOUNCEMENT());
}
public function test i can hear the announcement during the announcement phase(): void
{
$this->given it’s the announcement phase();
$this->when i build();
$this->then i expect an announcement();
$this->and the waiting music to be cut to n seconds(10);
$this->and a redirect to next phase(WaitPhase::ANNOUNCEMENT());
}
<?php
protected function setUp(): void
{
$this->responseBuilder = new StaticResponseBuilder();
$this->phase = WaitPhase::INITIAL();
$this->facilityId = FacilityIdMother::enabled();
$this->announcement = Announcement::of(MessageText::ofWords(self::ANNOUNCEMENT_MESSAGE));
}
private function given feature is disabled(): void
{
$this->facilityId = FacilityIdMother::disabled();
}
private function given phase does not allow announcements(): void
{
$this->phase = WaitPhase::NO_ANNOUNCEMENTS();
}
private function given caller has no announcement during phase(WaitPhase $phase)
{
$this->phase = $phase;
$this->announcement = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment