Create a gist now

Instantly share code, notes, and snippets.

@twada /Card.php Secret
Created Sep 9, 2017

What would you like to do?
TDDBC Sendai 7th PHP Pair
<?php
namespace Tddbc;
class Card
{
/**
* @var Suit
*/
private $suit;
/**
* @var Rank
*/
private $rank;
/**
* Card constructor.
* @param Rank $rank
* @param Suit $suit
*/
public function __construct(Rank $rank, Suit $suit)
{
$this->rank = $rank;
$this->suit = $suit;
}
/**
* @return string
*/
public function getSuit(): string
{
return $this->suit->getValue();
}
/**
* @return string
*/
public function getRank(): string
{
return $this->rank->getValue();
}
/**
* @return string
*/
public function getNotation(): string
{
return $this->rank->getValue() . $this->suit->getValue();
}
/**
* 同じ Suit を持つかどうかを判定する
* @param Card $another
* @return bool
*/
public function hasSameSuit(Card $another): bool
{
return ($this->getSuit() === $another->getSuit());
}
/**
* 同じ Rank を持つかどうかを判定する
* @param Card $another
* @return bool
*/
public function hasSameRank(Card $another): bool
{
return ($this->getRank() === $another->getRank());
}
}
<?php
namespace Tddbc;
class Cards
{
/**
* @var Card
*/
private $card1;
/**
* @var Card
*/
private $card2;
/**
* @param Card $card1
* @param Card $card2
*/
public function __construct(Card $card1, Card $card2)
{
$this->card1 = $card1;
$this->card2 = $card2;
}
/**
* ペアかどうかを判定する
* @return bool
*/
public function isPair(): bool
{
return $this->card1->hasSameRank($this->card2);
}
}
<?php
namespace Tddbc;
use PHPUnit\Framework\TestCase;
class CardsTest extends TestCase
{
/**
* @test
*/
public functionK♣」と「K♦」はペアと判定される()
{
$kingOfClubs = new Card(new Rank('K'), Suit::CLUB());
$kingOfDiamonds = new Card(new Rank('K'), Suit::DIAMOND());
$cards = new Cards($kingOfClubs, $kingOfDiamonds);
$this->assertTrue($cards->isPair());
}
/**
* @test
*/
public functionK♣」と「Q♣」はペアではない()
{
$kingOfClubs = new Card(new Rank('K'), Suit::CLUB());
$queenOfClubs = new Card(new Rank('Q'), Suit::CLUB());
$cards = new Cards($kingOfClubs, $queenOfClubs);
$this->assertFalse($cards->isPair());
}
}
<?php
namespace Tddbc;
use PHPUnit\Framework\TestCase;
class CardTest extends TestCase
{
/**
* @var Card
*/
private $kingOfSpades;
/**
* @var Card
*/
private $tenOfHearts;
/**
* @before
*/
protected function setUp()
{
parent::setUp();
$this->kingOfSpades = new Card(new Rank('K'), Suit::SPADE());
$this->tenOfHearts = new Card(new Rank('10'), Suit::HEART());
}
// K♠の場合
/**
* @test
*/
public function suitが「♠」rankが「K」のカードのsuitは「♠」()
{
$this->assertEquals('', $this->kingOfSpades->getSuit());
}
/**
* @test
*/
public function suitが「♠」rankが「K」のカードのrankは「K」()
{
$this->assertEquals('K', $this->kingOfSpades->getRank());
}
/**
* @test
*/
public function suitが「♠」rankが「K」のカードの文字列表現は「K♠」()
{
$this->assertEquals('K♠', $this->kingOfSpades->getNotation());
}
// 10♥の場合
/**
* @test
*/
public function suitが「♥」rankが「10」のカードのsuitは「♥」()
{
$this->assertEquals('', $this->tenOfHearts->getSuit());
}
/**
* @test
*/
public function suitが「♥」rankが「10」のカードのrankは「10」()
{
$this->assertEquals('10', $this->tenOfHearts->getRank());
}
/**
* @test
*/
public function suitが「♥」rankが「10」のカードの文字列表現は「10♥」()
{
$this->assertEquals('10♥', $this->tenOfHearts->getNotation());
}
/**
* @test
*/
public function hasSameSuitメソッドは「K♠」と「10♥」の場合falseを返す()
{
$this->assertFalse($this->kingOfSpades->hasSameSuit($this->tenOfHearts));
$this->assertFalse($this->tenOfHearts->hasSameSuit($this->kingOfSpades));
}
/**
* @test
*/
public function hasSameSuitメソッドは「K♠」と「A♠」の場合trueを返す()
{
$aceOfSpades = new Card(new Rank('A'), Suit::SPADE());
$this->assertTrue($this->kingOfSpades->hasSameSuit($aceOfSpades));
$this->assertTrue($aceOfSpades->hasSameSuit($this->kingOfSpades));
}
/**
* @test
*/
public function hasSameRankメソッドは「K♠」と「10♥」の場合falseを返す()
{
$this->assertFalse($this->kingOfSpades->hasSameRank($this->tenOfHearts));
$this->assertFalse($this->tenOfHearts->hasSameRank($this->kingOfSpades));
}
/**
* @test
*/
public function hasSameRankメソッドは「K♠」と「K♣」の場合trueを返す()
{
$kingOfClubs = new Card(new Rank('K'), Suit::CLUB());
$this->assertTrue($this->kingOfSpades->hasSameRank($kingOfClubs));
$this->assertTrue($kingOfClubs->hasSameRank($this->kingOfSpades));
}
}
{
"name": "tddbc/sendai-7th",
"description": "skeleton for php users.",
"license": "BSD-3-Clause",
"authors": [
{
"name": "tddbc",
"email": "tddbc@googlegroups.com"
}
],
"minimum-stability": "stable",
"require": {
"php": ">= 7.0"
},
"require-dev": {
"phpunit/phpunit": "^6.3.0"
},
"autoload": {
"psr-0": {
"Tddbc": [
"src/",
"tests/"
]
}
}
}
<?php
namespace Tddbc;
// http://qiita.com/Hiraku/items/71e385b56dcaa37629fe
abstract class Enum
{
private $__value;
public function __construct($value)
{
$ref = new \ReflectionObject($this);
$constants = $ref->getConstants();
if (! in_array($value, $constants, true)) {
throw new \InvalidArgumentException("$value is not defined.");
}
$this->__value = $value;
}
final public static function __callStatic($label, $args)
{
$class = get_called_class();
$const = constant("$class::$label");
return new $class($const);
}
final public function getValue()
{
return $this->__value;
}
final public function __toString()
{
return (string)$this->__value;
}
}
<?php
namespace Tddbc;
class Rank
{
/**
* @var string
*/
private $value;
/**
* Rank constructor.
* @param string $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* @return string
*/
public function getValue(): string
{
return $this->value;
}
}
<?php
namespace Tddbc;
final class Suit extends Enum
{
const SPADE = '';
const HEART = '';
const CLUB = '';
const DIAMOND = '♦︎';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment