mirror of
https://github.com/ysoftdevs/Theatrical-Players-Refactoring-Kata.git
synced 2026-07-05 20:41:50 +02:00
PHP7.2+ close to the JS code in refactoring ch.1
This commit is contained in:
@@ -1,63 +1,62 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Theatrical;
|
namespace Theatrical;
|
||||||
use Theatrical\Play;
|
|
||||||
use Theatrical\Invoice;
|
use Error;
|
||||||
use Theatrical\Performance;
|
use NumberFormatter;
|
||||||
|
|
||||||
class StatementPrinter
|
class StatementPrinter
|
||||||
{
|
{
|
||||||
public function print(Invoice $invoice, array $plays)
|
public function print(Invoice $invoice, array $plays): string
|
||||||
{
|
{
|
||||||
$totalAmount = 0;
|
$totalAmount = 0;
|
||||||
$volumeCredits = 0;
|
$volumeCredits = 0;
|
||||||
|
|
||||||
$result = 'Statement for ' . $invoice->customer . '\n';
|
$result = "Statement for {$invoice->customer}\n";
|
||||||
|
$format = new NumberFormatter('en_US', NumberFormatter::CURRENCY);
|
||||||
|
|
||||||
foreach($invoice->performances as $performance)
|
foreach ($invoice->performances as $performance) {
|
||||||
{
|
|
||||||
$play = $plays[$performance->play_id];
|
$play = $plays[$performance->play_id];
|
||||||
|
|
||||||
$thisAmount = 0;
|
$thisAmount = 0;
|
||||||
|
|
||||||
switch($play->type)
|
switch ($play->type) {
|
||||||
{
|
case 'tragedy':
|
||||||
case "tragedy":
|
|
||||||
$thisAmount = 40000;
|
$thisAmount = 40000;
|
||||||
if($performance->audience > 30)
|
if ($performance->audience > 30) {
|
||||||
{
|
|
||||||
$thisAmount += 1000 * ($performance->audience - 30);
|
$thisAmount += 1000 * ($performance->audience - 30);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "comedy":
|
case 'comedy':
|
||||||
$thisAmount = 30000;
|
$thisAmount = 30000;
|
||||||
if($performance->audience > 20)
|
if ($performance->audience > 20) {
|
||||||
{
|
|
||||||
$thisAmount += 10000 + 500 * ($performance->audience - 20);
|
$thisAmount += 10000 + 500 * ($performance->audience - 20);
|
||||||
}
|
}
|
||||||
$thisAmount += 300 * $performance->audience;
|
$thisAmount += 300 * $performance->audience;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
throw new \Error("Unknow type: $play->type");
|
throw new Error("Unknown type: {$play->type}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add volume credits
|
||||||
$volumeCredits += max($performance->audience - 30, 0);
|
$volumeCredits += max($performance->audience - 30, 0);
|
||||||
if($play->type == 'comedy')
|
// add extra credit for every ten comedy attendees
|
||||||
{
|
if ($play->type === 'comedy') {
|
||||||
$volumeCredits += floor($performance->audience / 5);
|
$volumeCredits += floor($performance->audience / 5);
|
||||||
}
|
}
|
||||||
$thisFinalAmount = $thisAmount / 100;
|
|
||||||
$result = "$play->name: $thisFinalAmount ($performance->audience seats)\n";
|
// print line for this order
|
||||||
|
$result .= " {$play->name}: {$format->formatCurrency($thisAmount / 100, 'USD')} ";
|
||||||
|
$result .= "({$performance->audience} seats)\n";
|
||||||
|
|
||||||
$totalAmount += $thisAmount;
|
$totalAmount += $thisAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
$finalTotal = ($totalAmount / 100);
|
$result .= "Amount owed is {$format ->formatCurrency($totalAmount / 100, 'USD')}\n";
|
||||||
$result .= "Amount owed is $finalTotal\n";
|
$result .= "You earned {$volumeCredits} credits";
|
||||||
$result .= "You earned $volumeCredits credits\n";
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,50 +1,51 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
declare(strict_types=1);
|
||||||
use ApprovalTests\Approvals;
|
|
||||||
|
|
||||||
use Theatrical\StatementPrinter;
|
namespace Tests;
|
||||||
use Theatrical\Play;
|
|
||||||
use Theatrical\Performance;
|
use ApprovalTests\Approvals;
|
||||||
|
use Error;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
use Theatrical\Invoice;
|
use Theatrical\Invoice;
|
||||||
|
use Theatrical\Performance;
|
||||||
|
use Theatrical\Play;
|
||||||
|
use Theatrical\StatementPrinter;
|
||||||
|
|
||||||
final class StatementPrinterTest extends TestCase
|
final class StatementPrinterTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testCanPrintInvoice() : void
|
public function testCanPrintInvoice(): void
|
||||||
{
|
{
|
||||||
$plays = [
|
$plays = [
|
||||||
"hamlet" => new Play("Hamlet", "tragedy"),
|
'hamlet' => new Play('Hamlet', 'tragedy'),
|
||||||
"as-like" => new Play("As You Like It", "comedy"),
|
'as-like' => new Play('As You Like It', 'comedy'),
|
||||||
"othello" => new Play("Othello", "tragedy")
|
'othello' => new Play('Othello', 'tragedy'),
|
||||||
];
|
];
|
||||||
|
|
||||||
$performances = [
|
$performances = [
|
||||||
new Performance("hamlet", 55),
|
new Performance('hamlet', 55),
|
||||||
new Performance("as-like", 35),
|
new Performance('as-like', 35),
|
||||||
new Performance("othello", 40)
|
new Performance('othello', 40),
|
||||||
];
|
];
|
||||||
$invoice = new Invoice("BigCo", $performances);
|
$invoice = new Invoice('BigCo', $performances);
|
||||||
$statementPrinter = new StatementPrinter();
|
$statementPrinter = new StatementPrinter();
|
||||||
$result = $statementPrinter->print($invoice, $plays);
|
$result = $statementPrinter->print($invoice, $plays);
|
||||||
|
|
||||||
Approvals::verifyString($result);
|
Approvals::verifyString($result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testNewPlayTypes() : void
|
public function testNewPlayTypes(): void
|
||||||
{
|
{
|
||||||
$plays = [
|
$plays = [
|
||||||
"henry-v" => new Play("Henry V", "history"),
|
'henry-v' => new Play('Henry V', 'history'),
|
||||||
"as-like" => new Play("As You Like It", "comedy"),
|
'as-like' => new Play('As You Like It', 'comedy'),
|
||||||
];
|
];
|
||||||
|
|
||||||
$performances = [
|
$performances = [new Performance('henry-v', 53), new Performance('as-like', 55)];
|
||||||
new Performance("henry-v", 53),
|
|
||||||
new Performance("as-like", 55)
|
|
||||||
];
|
|
||||||
|
|
||||||
$invoice = new Invoice("BigCo", $performances);
|
$invoice = new Invoice('BigCo', $performances);
|
||||||
$statementPrinter = new StatementPrinter();
|
$statementPrinter = new StatementPrinter();
|
||||||
$this->expectException(\Error::class);
|
$this->expectException(Error::class);
|
||||||
$statementPrinter->print($invoice, $plays);
|
$statementPrinter->print($invoice, $plays);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user