My attempts at exercism.org
1<?php
2
3/*
4 * By adding type hints and enabling strict type checking, code can become
5 * easier to read, self-documenting and reduce the number of potential bugs.
6 * By default, type declarations are non-strict, which means they will attempt
7 * to change the original type to match the type specified by the
8 * type-declaration.
9 *
10 * In other words, if you pass a string to a function requiring a float,
11 * it will attempt to convert the string value to a float.
12 *
13 * To enable strict mode, a single declare directive must be placed at the top
14 * of the file.
15 * This means that the strictness of typing is configured on a per-file basis.
16 * This directive not only affects the type declarations of parameters, but also
17 * a function's return type.
18 *
19 * For more info review the Concept on strict type checking in the PHP track
20 * <link>.
21 *
22 * To disable strict typing, comment out the directive below.
23 */
24
25declare(strict_types=1);
26
27use PHPUnit\Framework\TestCase;
28
29class TournamentTest extends TestCase
30{
31 private Tournament $tournament;
32
33 public static function setUpBeforeClass(): void
34 {
35 require_once 'Tournament.php';
36 }
37
38 protected function setUp(): void
39 {
40 $this->tournament = new Tournament();
41 }
42
43 public function testHeaderOnlyNoTeams(): void
44 {
45 $scores = '';
46 $expected = 'Team | MP | W | D | L | P';
47 $this->assertEquals($expected, $this->tournament->tally($scores));
48 }
49
50 public function testWinIsThreePointsLossIsZeroPoints(): void
51 {
52 $scores = 'Allegoric Alaskans;Blithering Badgers;win';
53 $expected =
54 "Team | MP | W | D | L | P\n" .
55 "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n" .
56 "Blithering Badgers | 1 | 0 | 0 | 1 | 0";
57 $this->assertEquals($expected, $this->tournament->tally($scores));
58 }
59
60 public function testWinCanAlsoBeExpressedAsALoss(): void
61 {
62 $scores = 'Blithering Badgers;Allegoric Alaskans;loss';
63 $expected =
64 "Team | MP | W | D | L | P\n" .
65 "Allegoric Alaskans | 1 | 1 | 0 | 0 | 3\n" .
66 "Blithering Badgers | 1 | 0 | 0 | 1 | 0";
67 $this->assertEquals($expected, $this->tournament->tally($scores));
68 }
69
70 public function testDifferentTeamsCanWin(): void
71 {
72 $scores = 'Blithering Badgers;Allegoric Alaskans;win';
73 $expected =
74 "Team | MP | W | D | L | P\n" .
75 "Blithering Badgers | 1 | 1 | 0 | 0 | 3\n" .
76 "Allegoric Alaskans | 1 | 0 | 0 | 1 | 0";
77 $this->assertEquals($expected, $this->tournament->tally($scores));
78 }
79
80 public function testADrawIsOnePointEach(): void
81 {
82 $scores = 'Allegoric Alaskans;Blithering Badgers;draw';
83 $expected =
84 "Team | MP | W | D | L | P\n" .
85 "Allegoric Alaskans | 1 | 0 | 1 | 0 | 1\n" .
86 "Blithering Badgers | 1 | 0 | 1 | 0 | 1";
87 $this->assertEquals($expected, $this->tournament->tally($scores));
88 }
89
90 public function testThereCanBeMultipleMatches(): void
91 {
92 $scores =
93 "Allegoric Alaskans;Blithering Badgers;win\n" .
94 "Allegoric Alaskans;Blithering Badgers;win";
95 $expected =
96 "Team | MP | W | D | L | P\n" .
97 "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n" .
98 "Blithering Badgers | 2 | 0 | 0 | 2 | 0";
99 $this->assertEquals($expected, $this->tournament->tally($scores));
100 }
101
102 public function testThereCanBeMoreThanOneWinner(): void
103 {
104 $scores =
105 "Allegoric Alaskans;Blithering Badgers;loss\n" .
106 "Allegoric Alaskans;Blithering Badgers;win";
107 $expected =
108 "Team | MP | W | D | L | P\n" .
109 "Allegoric Alaskans | 2 | 1 | 0 | 1 | 3\n" .
110 "Blithering Badgers | 2 | 1 | 0 | 1 | 3";
111 $this->assertEquals($expected, $this->tournament->tally($scores));
112 }
113
114 public function testThereCanBeMoreThanTwoTeams(): void
115 {
116 $scores =
117 "Allegoric Alaskans;Blithering Badgers;win\n" .
118 "Blithering Badgers;Courageous Californians;win\n" .
119 "Courageous Californians;Allegoric Alaskans;loss";
120 $expected =
121 "Team | MP | W | D | L | P\n" .
122 "Allegoric Alaskans | 2 | 2 | 0 | 0 | 6\n" .
123 "Blithering Badgers | 2 | 1 | 0 | 1 | 3\n" .
124 "Courageous Californians | 2 | 0 | 0 | 2 | 0";
125 $this->assertEquals($expected, $this->tournament->tally($scores));
126 }
127
128 public function testStandardInput(): void
129 {
130 $scores =
131 "Allegoric Alaskans;Blithering Badgers;win\n" .
132 "Devastating Donkeys;Courageous Californians;draw\n" .
133 "Devastating Donkeys;Allegoric Alaskans;win\n" .
134 "Courageous Californians;Blithering Badgers;loss\n" .
135 "Blithering Badgers;Devastating Donkeys;loss\n" .
136 "Allegoric Alaskans;Courageous Californians;win";
137 $expected =
138 "Team | MP | W | D | L | P\n" .
139 "Devastating Donkeys | 3 | 2 | 1 | 0 | 7\n" .
140 "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n" .
141 "Blithering Badgers | 3 | 1 | 0 | 2 | 3\n" .
142 "Courageous Californians | 3 | 0 | 1 | 2 | 1";
143 $this->assertEquals($expected, $this->tournament->tally($scores));
144 }
145
146 public function testIncompleteCompetitionWhereNotAllGamesPlayed(): void
147 {
148 $scores =
149 "Allegoric Alaskans;Blithering Badgers;loss\n" .
150 "Devastating Donkeys;Allegoric Alaskans;loss\n" .
151 "Courageous Californians;Blithering Badgers;draw\n" .
152 "Allegoric Alaskans;Courageous Californians;win";
153 $expected =
154 "Team | MP | W | D | L | P\n" .
155 "Allegoric Alaskans | 3 | 2 | 0 | 1 | 6\n" .
156 "Blithering Badgers | 2 | 1 | 1 | 0 | 4\n" .
157 "Courageous Californians | 2 | 0 | 1 | 1 | 1\n" .
158 "Devastating Donkeys | 1 | 0 | 0 | 1 | 0";
159 $this->assertEquals($expected, $this->tournament->tally($scores));
160 }
161
162 public function testTiesSortedAlphabetically(): void
163 {
164 $scores =
165 "Courageous Californians;Devastating Donkeys;win\n" .
166 "Allegoric Alaskans;Blithering Badgers;win\n" .
167 "Devastating Donkeys;Allegoric Alaskans;loss\n" .
168 "Courageous Californians;Blithering Badgers;win\n" .
169 "Blithering Badgers;Devastating Donkeys;draw\n" .
170 "Allegoric Alaskans;Courageous Californians;draw";
171 $expected =
172 "Team | MP | W | D | L | P\n" .
173 "Allegoric Alaskans | 3 | 2 | 1 | 0 | 7\n" .
174 "Courageous Californians | 3 | 2 | 1 | 0 | 7\n" .
175 "Blithering Badgers | 3 | 0 | 1 | 2 | 1\n" .
176 "Devastating Donkeys | 3 | 0 | 1 | 2 | 1";
177 $this->assertEquals($expected, $this->tournament->tally($scores));
178 }
179}