HandFinderTest::testFindHands()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 68
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
rs 9.2447
cc 1
eloc 39
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Bourdeau\Bundle\HandEvaluatorBundle\Test\Services;
3
4
use Bourdeau\Bundle\HandEvaluatorBundle\HandEvaluator\HandFinder;
5
6
/**
7
 * Class HandEvaluatorTest
8
 */
9
class HandFinderTest extends \PHPUnit_Framework_TestCase
10
{
11
    private $handFinder;
12
13
    /**
14
     * {@inheritDoc}
15
     */
16
    protected function setUp()
17
    {
18
        $cardValidator = $this->getMock('Bourdeau\Bundle\HandEvaluatorBundle\HandEvaluator\CardValidator');
19
20
        $cardValidator->expects($this->any())
21
                      ->method('areValid')
22
                      ->willReturn(true);
23
24
        $this->handFinder = new HandFinder($cardValidator);
25
    }
26
27
    public function testFindHands()
28
    {
29
        // Test Royal Flush
30
        $cards = [
31
            '13' => ['AS', 'KS', 'QS', 'JS', '10S', '9S', '8S'],
32
            '13' => ['AD', 'KD', 'QD', 'JD', '10D', '9D', '8D'],
33
            '13' => ['AC', 'KC', 'QC', 'JC', '10C', '9C', '8C'],
34
            '13' => ['AH', 'KH', 'QH', 'JH', '10H', '9H', '8H'],
35
        ];
36
        $this->runValidTest("Royal Flush", 5, $cards);
37
38
        // Test Straight Flush
39
        $cards = [
40
            '12' => ['KS', 'QS', 'JS', '10S', '9S', '8H', '7D'],
41
            '11' => ['QS', 'JS', '10S', '9S', '8S', '7C', '2C'],
42
        ];
43
        $this->runValidTest("Straight Flush", 5, $cards);
44
45
        // Test Four of a kind
46
        $cards = [
47
            '12' => ['KS', 'KD', 'KC', 'KH', '9S', '8S', '7D'],
48
        ];
49
        $this->runValidTest("Four of a kind", 4, $cards);
50
51
        // Test Full House
52
        $cards = [
53
            '12' => ['KS', 'KD', 'KC', 'QD', 'QC', '8S', '7D'],
54
            '13' => ['AD', 'AC', 'AH', '9D', '9H', '9C', 'JS'],
55
        ];
56
        $this->runValidTest("Full House", 5, $cards);
57
58
        // Test Flush
59
        $cards = [
60
            '9' => ['10H', '8H', '7H', '3H', '5H', '8C', '7D'],
61
            '8' => ['9D', '8D', '4D', '3D', '5D', '8C', '2H'],
62
        ];
63
        $this->runValidTest("Flush", 5, $cards);
64
65
        // Test Straight
66
        $cards = [
67
            '6'  => ['7H', '6S', '5H', '4C', '3H', 'JC', 'AD'],
68
            '9'  => ['10S', '9C', '8H', '7D', '6S', 'QC', '2D'],
69
            '10' => ['JC', '10H', '9D', '8C', '7S', '5H', '3H'],
70
            '12' => ['KD', 'QC', 'JS', '10S', '9S', '6H', '3D'],
71
            '7'  => ['8S', '7C', '6C', '5H', '4H', 'JC', '10H'],
72
            '6'  => ['AC', '2D', '3H', '4H', '5S', 'KS', '10D'], // straight from bottom
73
        ];
74
        $this->runValidTest("Straight", 5, $cards);
75
76
        // Test Three of a kind
77
        $cards = [
78
            '11'  => ['QS', 'QC', 'QD', 'JS', '6S', '7D', '9S'],
79
        ];
80
        $this->runValidTest("Three of a kind", 3, $cards);
81
82
        // Test Two Pairs
83
        $cards = [
84
            '9'  => ['10C', '10H', '2H', '2S', 'JC', '6C', '9D'],
85
        ];
86
        $this->runValidTest("Two Pairs", 4, $cards);
87
88
        // Test One Pair
89
        $cards = [
90
            '9'  => ['10C', '10H', 'KH', '2S', 'JC', '6C', '9D'],
91
        ];
92
        $this->runValidTest("One Pair", 2, $cards);
93
94
    }
95
96
    private function runValidTest($handName, $nbCard, array $arrayCards)
97
    {
98
        foreach ($arrayCards as $rank => $cards) {
99
            shuffle($cards);
100
101
            $test = $this->handFinder->findHand($cards);
102
103
            $this->assertInternalType("array", $test);
104
            $this->assertEquals($handName, $test["hand_name"]);
105
            $this->assertEquals($rank, $test["card_rank"]);
106
            $this->assertInternalType("array", $test["cards"]);
107
            $this->assertEquals($nbCard, count($test["cards"]));
108
        }
109
    }
110
}
111