Passed
Push — main ( c4a240...6a0053 )
by Karl
05:42
created

FrenchSuitedDeckTest::testDealCards()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A FrenchSuitedDeckTest::testDrawCards() 0 5 1
1
<?php
2
3
namespace App\Model;
4
5
use App\Observer\SessionSavingObserver;
6
use PHPUnit\Framework\TestCase;
7
use Random\Randomizer;
0 ignored issues
show
Bug introduced by
The type Random\Randomizer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Symfony\Component\HttpFoundation\Request;
9
10
/**
11
 * Test cases for class FrenchSuitedDeck.
12
 */
13
class FrenchSuitedDeckTest extends TestCase
14
{
15
    /**
16
     * Construct object and verify that the object is of the expected
17
     * type.
18
     */
19
    public function testCreateDeckOfCards(): void
20
    {
21
        $observer = new SessionSavingObserver(new Request(), "data");
22
        $deck = new FrenchSuitedDeck([$observer]);
23
        $this->assertInstanceOf("\App\Model\DeckOfCards", $deck);
24
    }
25
26
    /** 
27
     * Assert that create method returns a FrenchSuitedDeck object.
28
     */
29
30
    public function testCreate(): void
31
    {
32
        $deck = FrenchSuitedDeck::create();
33
        $this->assertInstanceOf("\App\Model\FrenchSuitedDeck", $deck);
34
    }
35
36
    /** 
37
     * Assert that getNumberOfCards() return 52 for a french deck
38
     */
39
40
    public function testGetNumberOfCards(): void
41
    {
42
        $deck = FrenchSuitedDeck::create();
43
        $this->assertEquals($deck->getNumberOfCards(), 52);
44
    }
45
46
    /**
47
     * Assert that drawCards throws an exception when 
48
     * trying to draw more cards than available.
49
     */
50
51
    public function testDrawCards(): void
52
    {
53
        $deck = FrenchSuitedDeck::create();
54
        $this->expectException(\Exception::class);
55
        $deck->drawCards(53);
56
    }
57
58
    /**
59
     * Assert that observers are attached to the deck.
60
     */
61
    public function testAttach(): void
62
    {
63
        $deck = FrenchSuitedDeck::create();
64
        $observer = new SessionSavingObserver(new Request(), "data");
65
        $deck->attach($observer);
66
        $this->assertEquals($deck->getObservers()->count(), 1);
67
68
        $deck->detach($observer);
69
        $this->assertEquals($deck->getObservers()->count(), 0);
70
    }
71
72
    /**
73
     * Assert that sort method returns a sorted deck.
74
     */
75
76
    public function testSort(): void
77
    {
78
        $randomizer = new Randomizer();
79
        $deck = FrenchSuitedDeck::create();
80
        $deckShuffler = new DeckShuffler($randomizer);
81
        $deckShuffler->shuffle($deck);
82
        $deck->sort();
83
        $this->assertEquals($deck->cardCollection->getCards()[0]->getSuit(), "Spade");
84
        $this->assertEquals($deck->cardCollection->getCards()[0]->getValue(), "2");
85
        $this->assertEquals($deck->cardCollection->getCards()[1]->getSuit(), "Spade");
86
        $this->assertEquals($deck->cardCollection->getCards()[1]->getValue(), "3");
87
        $this->assertEquals($deck->cardCollection->getCards()[51]->getSuit(), "Club");
88
        $this->assertEquals($deck->cardCollection->getCards()[51]->getValue(), "1");
89
        $this->assertEquals($deck->cardCollection->getCards()[51]->getAlternativeValue(), "14");
90
    }
91
92
93
    /**
94
     * Assert that sortBySuit method returns a sorted deck by suit.
95
     */
96
97
    public function testSortBySuit(): void
98
    {
99
        $randomizer = new Randomizer();
100
        $deck = FrenchSuitedDeck::create();
101
        $deckShuffler = new DeckShuffler($randomizer);
102
        $deckShuffler->shuffle($deck);
103
        $deck->sort();
104
        $deck->sortBySuit($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
105
        $this->assertEquals($deck->cardCollection->getCards()[0]->getSuit(), "Spade");
106
        $this->assertEquals($deck->cardCollection->getCards()[0]->getValue(), "2");
107
        $this->assertEquals($deck->cardCollection->getCards()[1]->getSuit(), "Spade");
108
        $this->assertEquals($deck->cardCollection->getCards()[1]->getValue(), "3");
109
        $this->assertEquals($deck->cardCollection->getCards()[51]->getSuit(), "Club");
110
    }
111
112
    /**
113
     * Assert that sortByValue method returns a sorted deck by value.
114
     */
115
116
    public function testSortByValue(): void
117
    {
118
        $deck = FrenchSuitedDeck::create();
119
        $deck->sort();
120
        $res = $deck->sortByValue($deck->cardCollection->getCards()[10], $deck->cardCollection->getCards()[1]);
121
        $this->assertEquals($res, 1);
122
123
        // equal
124
        $res = $deck->sortByValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[13]);
125
        $this->assertEquals($res, 0);
126
127
        $res = $deck->sortByValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
128
129
        $this->assertEquals($res, -1);
130
131
        $res = $deck->sortByValue($deck->cardCollection->getCards()[12], $deck->cardCollection->getCards()[25]);
132
        $this->assertEquals($res, 0);
133
        // var_dump($deck->cards[12]->getAlternativeValue(), $deck->cards[25]->getAlternativeValue());
134
        // ob_flush();
135
    }
136
137
    /**
138
     * Assert that sortBySuitAndValue method returns a sorted deck by suit and value.
139
     */
140
141
    public function testSortBySuitAndValue(): void
142
    {
143
        $deck = FrenchSuitedDeck::create();
144
        $deck->sort();
145
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
146
        $this->assertEquals($res, -1);
147
148
        // equal
149
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[13]);
150
        $this->assertEquals($res, -1);
151
152
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[1], $deck->cardCollection->getCards()[0]);
153
        $this->assertEquals($res, 1);
154
    }
155
}
156