FrenchSuitedDeckTest::testSortByValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 10
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 17
rs 9.9332
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
    // /**
38
    //  * Assert that drawCards throws an exception when 
39
    //  * trying to draw more cards than available.
40
    //  */
41
42
    // public function testDrawCards(): void
43
    // {
44
    //     $deck = FrenchSuitedDeck::create();
45
    //     $this->expectException(\Exception::class);
46
    //     $deck->drawCards(53);
47
    // }
48
49
    /**
50
     * Assert that observers are attached to the deck.
51
     */
52
    public function testAttach(): void
53
    {
54
        $deck = FrenchSuitedDeck::create();
55
        $observer = new SessionSavingObserver(new Request(), "data");
56
        $deck->attach($observer);
57
        $this->assertEquals($deck->getObservers()->count(), 1);
58
59
        $deck->detach($observer);
60
        $this->assertEquals($deck->getObservers()->count(), 0);
61
    }
62
63
    /**
64
     * Assert that sort method returns a sorted deck.
65
     */
66
67
    public function testSort(): void
68
    {
69
        $randomizer = new Randomizer();
70
        $deck = FrenchSuitedDeck::create();
71
        $deckShuffler = new DeckShuffler($randomizer);
72
        $deckShuffler->shuffle($deck);
73
        $deck->sort();
74
        $this->assertEquals($deck->cardCollection->getCards()[0]->getSuit(), "Spade");
75
        $this->assertEquals($deck->cardCollection->getCards()[0]->getValue(), "2");
76
        $this->assertEquals($deck->cardCollection->getCards()[1]->getSuit(), "Spade");
77
        $this->assertEquals($deck->cardCollection->getCards()[1]->getValue(), "3");
78
        $this->assertEquals($deck->cardCollection->getCards()[51]->getSuit(), "Club");
79
        $this->assertEquals($deck->cardCollection->getCards()[51]->getValue(), "1");
80
        $this->assertEquals($deck->cardCollection->getCards()[51]->getAlternativeValue(), "14");
81
    }
82
83
84
    /**
85
     * Assert that sortBySuit method returns a sorted deck by suit.
86
     */
87
88
    public function testSortBySuit(): void
89
    {
90
        $randomizer = new Randomizer();
91
        $deck = FrenchSuitedDeck::create();
92
        $deckShuffler = new DeckShuffler($randomizer);
93
        $deckShuffler->shuffle($deck);
94
        $deck->sort();
95
        $deck->sortBySuit($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
96
        $this->assertEquals($deck->cardCollection->getCards()[0]->getSuit(), "Spade");
97
        $this->assertEquals($deck->cardCollection->getCards()[0]->getValue(), "2");
98
        $this->assertEquals($deck->cardCollection->getCards()[1]->getSuit(), "Spade");
99
        $this->assertEquals($deck->cardCollection->getCards()[1]->getValue(), "3");
100
        $this->assertEquals($deck->cardCollection->getCards()[51]->getSuit(), "Club");
101
    }
102
103
    /**
104
     * Assert that sortByValue method returns a sorted deck by value.
105
     */
106
107
    public function testSortByValue(): void
108
    {
109
        $deck = FrenchSuitedDeck::create();
110
        $deck->sort();
111
        $res = $deck->sortByValue($deck->cardCollection->getCards()[10], $deck->cardCollection->getCards()[1]);
112
        $this->assertEquals($res, 1);
113
114
        // equal
115
        $res = $deck->sortByValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[13]);
116
        $this->assertEquals($res, 0);
117
118
        $res = $deck->sortByValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
119
120
        $this->assertEquals($res, -1);
121
122
        $res = $deck->sortByValue($deck->cardCollection->getCards()[12], $deck->cardCollection->getCards()[25]);
123
        $this->assertEquals($res, 0);
124
        // var_dump($deck->cards[12]->getAlternativeValue(), $deck->cards[25]->getAlternativeValue());
125
        // ob_flush();
126
    }
127
128
    /**
129
     * Assert that sortBySuitAndValue method returns a sorted deck by suit and value.
130
     */
131
132
    public function testSortBySuitAndValue(): void
133
    {
134
        $deck = FrenchSuitedDeck::create();
135
        $deck->sort();
136
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[1]);
137
        $this->assertEquals($res, -1);
138
139
        // equal
140
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[0], $deck->cardCollection->getCards()[13]);
141
        $this->assertEquals($res, -1);
142
143
        $res = $deck->sortBySuitAndValue($deck->cardCollection->getCards()[1], $deck->cardCollection->getCards()[0]);
144
        $this->assertEquals($res, 1);
145
    }
146
}
147