Skip to content

Instantly share code, notes, and snippets.

@ShawnMcCool
Last active April 23, 2021 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShawnMcCool/391ea137c70ae65978be96424985d8ce to your computer and use it in GitHub Desktop.
Save ShawnMcCool/391ea137c70ae65978be96424985d8ce to your computer and use it in GitHub Desktop.
Are named parameters a risk to all that we hold near and dear? News at 11.

This tests behavior on the process "CreateConferenceTicketsForWebinarCustomers".

It's possible to create a working test that looks like the "minimum possible" example. But the "actual" example has additional information that supplements the test. The null parameter with default values pattern is not new.

In this case, the named parameters improve the comprehensibility of the tests.

This is one of hundreds of tests that I've used this technique for. When testing various paths through the same process, being able to swap values in then given/when/then steps can be really beautiful.

<?php
function testCreatesTicketsForWebinarPurchase()
{
$this->stub(CreateHiddenHopinTickets::class, new CreateHiddenHopinTicketsStub('code', 'ticket url'));
$this->process(
CreateConferenceTicketsForWebinarCustomers::class
)->when(
$this->hopinRegistrationWasCreated(
eventName: 'Laracon EU Online Webinar - Style Beautiful Sites with Tailwind CSS'
)
)->then(
$this->freeConferenceTicketWasCreatedForAWebinarCustomer(
eventName: 'Laracon EU Online Webinar - Style Beautiful Sites with Tailwind CSS',
ticketReference: $this->ticketReference(
ticketName: 'Conference Ticket - ' . 'user name',
ticketDescription: 'Attend the conference sessions on Jan. 18th and 19th.',
ticketCount: 1,
ticketCode: 'code'
)
)
);
}
<?php
function testCreatesTicketsForWebinarPurchase()
{
$this->stub(CreateHiddenHopinTickets::class, new CreateHiddenHopinTicketsStub('code', 'ticket url'));
$this->process(
CreateConferenceTicketsForWebinarCustomers::class
)->when(
$this->hopinRegistrationWasCreated()
)->then(
$this->freeConferenceTicketWasCreatedForAWebinarCustomer()
);
}
<?php
# This is all from a different file, reused.
function hopinRegistrationWasCreated(
?string $id = null,
?string $chargeId = null,
?string $userName = null,
?string $userEmail = null,
?string $userHeadline = null,
?string $userTown = null,
?string $eventName = null,
?string $personaLabel = null,
?string $persona = null,
?Timestamp $createdAt = null
): HopinRegistrationWasCreated {
return new HopinRegistrationWasCreated(
$id ?? 'registration id',
$chargeId ?? 'charge id',
$userName ?? 'user name',
$userEmail ?? 'test@obfuscated.com',
$userHeadline ?? 'user headline',
$userTown ?? 'user town',
$eventName ?? 'event name',
$personaLabel ?? 'ticket name',
$persona ?? '60.50',
$createdAt ?? $this->timestamp()
);
}
function freeConferenceTicketWasCreatedForAWebinarCustomer(
?string $hopinRegistrationId = null,
?string $attendeeName = null,
?string $attendeeEmail = null,
?string $eventName = null,
?string $ticketName = null,
?string $ticketPrice = null,
?HopinTicketReference $ticketReference = null,
?Timestamp $createdAt = null,
): FreeConferenceTicketWasCreatedForAWebinarCustomer {
return new FreeConferenceTicketWasCreatedForAWebinarCustomer(
$hopinRegistrationId ?? 'registration id',
$attendeeName ?? 'user name',
$attendeeEmail ?? 'test@obfuscated.com',
$eventName ?? 'event name',
$ticketName ?? 'ticket name',
$ticketPrice ?? '60.50',
$ticketReference ?? $this->ticketReference(),
$createdAt ?? $this->timestamp(),
);
}
function ticketReference(
?string $eventSlug = null,
?string $ticketName = null,
?string $ticketDescription = null,
?int $ticketCount = null,
?string $ticketCode = null,
?string $ticketUrl = null
): HopinTicketReference {
return new HopinTicketReference(
$eventSlug ?? 'laracon-eu-online-conference',
$ticketName ?? 'Conference Ticket - invoice company name',
$ticketDescription ?? 'Attend the conference sessions on Jan. 18th and 19th.',
$ticketCount ?? 3,
$ticketCode ?? 'code',
$ticketUrl ?? 'ticket url'
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment