27
loading...
This website collects cookies to deliver better user experience
/** @test */
public function calculatesOrderCost(): void
{
$itemA = Item::withPrice(20);
$itemB = Item::withPrice(60);
$shippingCost = ShippingCost::asPercentage(10);
$order = new Order();
$order->addItems($itemA, $itemB);
$order->applyShippingCost($shippingCost);
self::assertEquals(88, $order->totalCost());
}
/** @test */
public function calculatesOrderCost(): void
{
$this->givenAnItemWithPrice(20);
$this->andAnItemWithPrice(60);
$this->andAShippingCostOfPercent(10);
$this->whenOrderIsCreated();
$this->thenTotalOrderCostIs(88);
}
Item
or Order
would require a lot more parameters to be instantiated while we only care about the ones involving the cost in this case.Given '1' items with price '80' euro*
will be matched with a method givenItemsWithPrice(int $amount, int $price)
. xUnit frameworks are not prepared for this type of pattern matching and the methods in GWT tests do not read as good as the pure BDD counterparts. In our xUnit tests we will have just the method implementation, not the more readable feature file./** @test **/
public function shipingToAddress(): void
{
$this->givenAnAddress();
$this->whenOrderIsShipped();
$this->thenPackageIsSent();
}
private function givenAnAddress(): void
{
// We need an address property
$this->address = new Address('sesame street');
}
private function whenOrderIsShipped(): void
{
$shippingService = new ShippingService();
// This method relies on an address previously set, producing a confusing error when it is not
$shippingService->shipTo($this->address);
}
/** @test **/
public function sanitizeTextGWT(): void
{
$this->givenATextWithAccents();
$this->whenSanitizingTheText();
$this->thenAccentsAreRemoved();
}
/** @test **/
public function sanitizeText(): void
{
// Maybe GWT does not provide much value compared to this
self::assertEquals('aa', Sanitizer::sanitize('áà'));
}
/** @test */
public function carReactsToAdverseWeather(): void
{
$this->givenThereIsMist(); // In this test rain is relevant
$this->andItIsRaining(); // So this reads nice
$this->whenWeatherIsScanned();
$this->thenMistLightsAreTurnOn();
$this->andDriveAssitanceOnWetRoadIsTurnOn();
}
/** @test */
public function carReactsToNotSoAdverseWeather(): void
{
$this->andItIsRaining(); // We don't care about the mist ant it reads weird
$this->whenWeatherIsScanned();
$this->andDriveAssitanceOnWetRoadIsTurnOn(); // Same here
}
/** @test */
public function carReactsToAdverseWeather(): void
{
$this->givenThereIsMist();
$this->givenItIsRaining();
$this->whenWeatherIsScanned();
$this->thenMistLightsAreTurnOn();
$this->thenDriveAssitanceOnWetRoadIsTurnOn();
}
/** @test **/
public function shipingToAddress(): void
{
$this->givenAnAddress();
$this->givenClientHasNoFunds();
$this->whenOrderIsShipped();
$this->thenOrderFailedDuetoNoPayment();
}
private function whenOrderIsShipped(): void
{
try {
$this->shippingService->ship();
} catch (Exception $ex) {
$this->exceptionWhenShippingOrder = $ex;
}
}
private function thenOrderFailedDuetoNoPayment(): void
{
self::assertInstanceOf(NoPaymentException::class, $this->exceptionWhenShippingOrder)
}
/** @test */
public function calculatesOrderCost(): void
{
$this->givenAnItemWithPrice(20); // Requires storing a property for the item
$this->givenAnItemWithPrice(60); // Another property
$this->andAShippingCostOfPercent(10); // Another property
$this->whenOrderIsCreated(); // Will use the previously set properties
$this->thenTotalOrderCostIs(88);
}
/** @test */
public function calculatesOrderCost(): void
{
$oneItem = $this->givenAnItemWithPrice(20);
$anotherItem = $this->givenAnItemWithPrice(60);
$shippingCost = $this->andAShippingCostOfPercent(10);
$this->whenOrderIsCreated($shippingCost, $oneItem, $anotherItem);
$this->thenTotalOrderCostIs(88);
}