Merge pull request #12 from nostradamnit/php-version

Php version
This commit is contained in:
Emily Bache
2020-02-13 07:23:29 +01:00
committed by GitHub
12 changed files with 1898 additions and 0 deletions

1
php/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
vendor/

9
php/README.md Normal file
View File

@@ -0,0 +1,9 @@
Theatrical Players Refactoring Kata - PHP version
========================================================
See the [top level readme](https://github.com/emilybache/Theatrical-Players-Refactoring-Kata) for general information about this exercise.
This project uses [PHPUnit](https://github.com/sebastianbergmann/phpunit) and [approvaltests](https://github.com/approvals/ApprovalTests.php).
Run ```composer install``` then ```./vendor/bin/phpunit``` to run the tests

22
php/composer.json Normal file
View File

@@ -0,0 +1,22 @@
{
"name": "sam/theatrical",
"type": "project",
"require-dev": {
"phpunit/phpunit": "^9",
"approvals/approval-tests": "^1.4"
},
"authors": [
{
"name": "Sam Cranford",
"email": "sam@cranford.fr"
}
],
"require": {
"php": "^7.4"
},
"autoload": {
"psr-4": {
"Theatrical\\": "src/"
}
}
}

1677
php/composer.lock generated Normal file

File diff suppressed because it is too large Load Diff

4
php/index.php Normal file
View File

@@ -0,0 +1,4 @@
<?php
require __DIR__ . '/vendor/autoload.php';

18
php/phpunit.xml Normal file
View File

@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
verbose="true">
<testsuites>
<testsuite name="unit">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>

16
php/src/Invoice.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
namespace Theatrical;
class Invoice
{
public string $customer;
public array $performances;
public function __construct($customer, $performances)
{
$this->customer = $customer;
$this->performances = $performances;
}
}

15
php/src/Performance.php Normal file
View File

@@ -0,0 +1,15 @@
<?php
namespace Theatrical;
class Performance
{
public string $play_id;
public int $audience;
public function __construct($play_id, $audience)
{
$this->play_id = $play_id;
$this->audience = $audience;
}
}

20
php/src/Play.php Normal file
View File

@@ -0,0 +1,20 @@
<?php
namespace Theatrical;
class Play
{
public string $name;
public string $type;
public function __construct($name, $type)
{
$this->name = $name;
$this->type = $type;
}
public function __toString()
{
return (string) $this->name . ' : ' . $this->type;
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Theatrical;
use Theatrical\Play;
use Theatrical\Invoice;
use Theatrical\Performance;
class StatementPrinter
{
public function print(Invoice $invoice, array $plays)
{
$totalAmount = 0;
$volumeCredits = 0;
$result = 'Statement for ' . $invoice->customer . '\n';
foreach($invoice->performances as $performance)
{
$play = $plays[$performance->play_id];
$thisAmount = 0;
switch($play->type)
{
case "tragedy":
$thisAmount = 40000;
if($performance->audience > 30)
{
$thisAmount += 1000 * ($performance->audience - 30);
}
break;
case "comedy":
$thisAmount = 30000;
if($performance->audience > 20)
{
$thisAmount += 10000 + 500 * ($performance->audience - 20);
}
$thisAmount += 300 * $performance->audience;
break;
default:
throw new \Error("Unknow type: $play->type");
}
$volumeCredits += max($performance->audience - 30, 0);
if($play->type == 'comedy')
{
$volumeCredits += floor($performance->audience / 5);
}
$thisFinalAmount = $thisAmount / 100;
$result = "$play->name: $thisFinalAmount ($performance->audience seats)\n";
$totalAmount += $thisAmount;
}
$finalTotal = ($totalAmount / 100);
$result .= "Amount owed is $finalTotal\n";
$result .= "You earned $volumeCredits credits\n";
return $result;
}
}

View File

@@ -0,0 +1,50 @@
<?php
use PHPUnit\Framework\TestCase;
use ApprovalTests\Approvals;
use Theatrical\StatementPrinter;
use Theatrical\Play;
use Theatrical\Performance;
use Theatrical\Invoice;
final class StatementPrinterTest extends TestCase
{
public function testCanPrintInvoice() : void
{
$plays = [
"hamlet" => new Play("Hamlet", "tragedy"),
"as-like" => new Play("As You Like It", "comedy"),
"othello" => new Play("Othello", "tragedy")
];
$performances = [
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40)
];
$invoice = new Invoice("BigCo", $performances);
$statementPrinter = new StatementPrinter();
$result = $statementPrinter->print($invoice, $plays);
Approvals::verifyString($result);
}
public function testNewPlayTypes() : void
{
$plays = [
"henry-v" => new Play("Henry V", "history"),
"as-like" => new Play("As You Like It", "comedy"),
];
$performances = [
new Performance("henry-v", 53),
new Performance("as-like", 55)
];
$invoice = new Invoice("BigCo", $performances);
$statementPrinter = new StatementPrinter();
$this->expectException(\Error::class);
$statementPrinter->print($invoice, $plays);
}
}

View File

@@ -0,0 +1,3 @@
Othello: 500 (40 seats)
Amount owed is 1730
You earned 47 credits