Here are some better behavior tests:Scenario: Checked in passenger can print boarding pass Given I have a flight booked And I have already checked in When I visit the boarding pass page Then I should see the "Print boarding pass" buttonScenario: Not checked in yet Given I have a flight booked But I have not checked in yet When I visit the boarding pass page Then I should see a message to check in firstScenario: Technical issue prevents printing Given I have a flight booked And I am checked in But the printer is unavailable When I try to print my boarding pass Then I should see an error message And information on collecting at the airportThese
The Best Practices for Crafting Quality PHP Applications workshop is an interactive exploration into some conventions, processes, and habits that help make better quality software. Whilst there is no single silver bullet, we will spend time discussing, supported by some practical exercises, what improvements we can make. We’ll focus on three core areas; planning, development, and testing; using a variety of practical explorations, group discussion and showing you discoveries from my own experience.
Similar to Here are some better behavior tests:Scenario: Checked in passenger can print boarding pass Given I have a flight booked And I have already checked in When I visit the boarding pass page Then I should see the "Print boarding pass" buttonScenario: Not checked in yet Given I have a flight booked But I have not checked in yet When I visit the boarding pass page Then I should see a message to check in firstScenario: Technical issue prevents printing Given I have a flight booked And I am checked in But the printer is unavailable When I try to print my boarding pass Then I should see an error message And information on collecting at the airportThese
James Thomas - Serverless Machine Learning With TensorFlow - Codemotion Berli...Codemotion
Similar to Here are some better behavior tests:Scenario: Checked in passenger can print boarding pass Given I have a flight booked And I have already checked in When I visit the boarding pass page Then I should see the "Print boarding pass" buttonScenario: Not checked in yet Given I have a flight booked But I have not checked in yet When I visit the boarding pass page Then I should see a message to check in firstScenario: Technical issue prevents printing Given I have a flight booked And I am checked in But the printer is unavailable When I try to print my boarding pass Then I should see an error message And information on collecting at the airportThese (20)
Here are some better behavior tests:Scenario: Checked in passenger can print boarding pass Given I have a flight booked And I have already checked in When I visit the boarding pass page Then I should see the "Print boarding pass" buttonScenario: Not checked in yet Given I have a flight booked But I have not checked in yet When I visit the boarding pass page Then I should see a message to check in firstScenario: Technical issue prevents printing Given I have a flight booked And I am checked in But the printer is unavailable When I try to print my boarding pass Then I should see an error message And information on collecting at the airportThese
1. @asgrim
Please clone & composer install:
https://github.com/asgrim/quality-tutorial
WiFi: php2019 / php2019
Best practices for crafting high quality PHP apps
2. @asgrim
Best practices for crafting high
quality PHP apps
James Titcumb
php[world] 2019
Please clone & composer install:
https://github.com/asgrim/quality-tutorial
WiFi: php2019 / php2019
16. @asgrim
“A freelancer at $25 an hour for 100 hours still
costs more than a freelancer at $150 an hour that
takes 10 hours to do the same task”
-- Brandon Savage
source: http://www.brandonsavage.net/earning-more-money-as-a-php-freelancer/
28. @asgrim
Event Storming - Practical Exercise
● Booking a hotel room
● Customer must select dates
● Rooms must be paid for when booking
● Stripe is the chosen payment provider
● Payment must only be taken once room is reserved
● Room availability is checked & reserved via 3rd party API
● All rooms & dates cost the same (e.g. $100 per night)
● Notify the customer (e.g. email) on a successful reservation
● Customer can cancel a booking
● On cancellation, full refund if more than 48 hours before the stay
32. @asgrim
Feature Planning
Feature: Booking a room for a hotel
Scenario: Making a successful booking
Given ...
When ...
Then ...
Scenario: Cancelling an existing booking
Given ...
When ...
Then ...
Scenario: If a room is booked by the time payment is made,
do not charge the card or reserve the room
Given ...
When ...
Then ...
36. @asgrim
“There are only two hard things in Computer
Science: cache invalidation and naming things.”
-- Phil Karlton
source: https://martinfowler.com/bliki/TwoHardThings.html
42. @asgrim
SOLID Violation Example
interface Thing {
public function doStuff() : void;
}
final class ImplementationOfThing implements Thing {
public function doStuff() : void { ... }
public function moreStuff() : void;
}
class ConsumerOfThing {
/** @var Thing */
private $thing;
public function woo() {
$this->thing->moreStuff();
}
}
44. @asgrim
Object Calisthenics - Practical
● One level of indentation in a method
● Avoid “else” keyword
● Wrap primitive types (e.g. strings, int)
● First class collections (instead of Thing[])
● One object operator (Law of Demeter)
● Avoid abbreviations (naming!)
● Keep classes small
● Avoid more than 2 instance variables
● No getters/setters
50. @asgrim
Immutable value objects
declare(strict_types=1);
use AssertAssertion; // beberlei/assert library !
final class PostalCode
{
private const VALIDATION_EXPRESSION = '(GIR 0AA)|((([A-Z-[QVX]][0-9][0-...
/** @var string */
private $value;
public function __construct(string $value) {
$this->assertValidPostalCode($value);
$this->value = $value;
}
private function assertValidPostalCode(string $value) : string {
Assertion::regex($value, self::VALIDATION_EXPRESSION);
}
public function __toString() : string {
return $this->value;
}
}
51. @asgrim
Value objects are valid!
declare(strict_types=1);
final class Thing {
public function assignPostalCode(PostalCode $postalCode) : void
{
// ... we can trust $postalCode is a valid postal code
}
}
// 12345 is not valid - EXCEPTION!
$myThing->assignPostalCode(new PostalCode('12345'));
// assignPostalCode is happy
$myThing->assignPostalCode(new PostalCode('PO1 1AA'));
// With STRICT types, this will also FAIL
$myThing->assignPostalCode(new PostalCode(12345));
62. @asgrim
Reduce complexity
public function process(Stuff a, string b, int c) : MoreStuff
{
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
// lots of complicated code
63. @asgrim
Reduce complexity
public function meaningfulThing(Stuff a, string b, int c) : More
{
// call nicer, meaningful methods below
}
private function throwStuffIntoFire(Stuff a) : Fire
{
// smaller, meaningful chunk of code
}
private function combineStringWithFire(Fire a, string b) : string
{
// simple, lovely code!
}
private function postFlowersToSpain(int c) : void
78. @asgrim
Example of a test not testing…
public function testPurchaseTickets()
{
$event = Event::create(uniq('event', true)
$customer = Customer::create(uniq('name', true));
$shop->purchaseTicket(1, $event, [$customer]);
}
@asgrim
84. @asgrim
Mutation testing
function add(int $a, int $b) : int {
return $a - $b;
}
function testAdd() {
$result = add(2, 3);
self::assertSame(5, $result);
// /// test will now fail with mutation
}
88. @asgrim
BAD! Do not do this.
Feature: Ability to print my boarding pass
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked
And I have checked in
When I visit the home page
And I click the ".manage-booking" button
And I enter "CJM23L" in the ".bref-ipt-fld" field
And I click the "Find Booking" button
Then I should see the ".booking-ref.print" button
When I click the ".booking-ref.print" button
Then I should see the print dialogue
89. @asgrim
Better Behaviour test
Feature: Ability to print my boarding pass
Policies:
- Boarding passes are only available when already checked in
- If customer cannot print boarding pass, they can collect at
The airport
- more business rules etc...
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked for LHR-MIA on 24th May
And I have previously checked in for the flight
When I display my booking reference "CJM23L"
Then I should be able to print my boarding pass
And the boarding pass should display flight details correctly
94. @asgrim
Testing at domain layer
Application (UI, API, etc.)
Domain / Business Logic
Infrastructure (DB, APIs, etc.)
// Testing via the UI
public function iDisplayMyBookingReference(string $reference)
{
$page = $this->getSession()->getPage();
$page->click(".manage-booking");
$page->findField(".bref-ipt-fld")->setValue($reference);
$page->click("Find Booking");
}
// Using the domain layer directly in tests
public function iDisplayMyBookingReference(string $reference)
{
$this->booking = $this->retrieveBooking($reference);
}
95. @asgrim
Some UI testing is okay!!!
Feature: Ability to print my boarding pass
Policies:
- Boarding passes are only available when already checked in
- If customer cannot print boarding pass, they can collect at
The airport
- more business rules etc...
@ui
Scenario: A checked in passenger can print their boarding pass
Given I have a flight booked for LHR-OTP on 24th May
And I have previously checked in for the flight
When I display my booking reference "CJM23L"
Then I should be able to print my boarding pass
And the boarding pass should display flight details correctly