SchoppAx /
php-sleeper-api
| 1 | <?php |
||
| 2 | |||
| 3 | use PHPUnit\Framework\TestCase; |
||
| 4 | use SchoppAx\Sleeper\SleeperClient; |
||
| 5 | use GuzzleHttp\Handler\MockHandler; |
||
| 6 | use GuzzleHttp\Psr7\Response; |
||
| 7 | use GuzzleHttp\Psr7\Request; |
||
| 8 | |||
| 9 | class SleeperLeagueTest extends TestCase |
||
| 10 | { |
||
| 11 | |||
| 12 | public function testLeague() |
||
| 13 | { |
||
| 14 | $data = file_get_contents(__DIR__ . '/data/league.json'); |
||
| 15 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 16 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 17 | |||
| 18 | $client = new SleeperClient($mock); |
||
| 19 | |||
| 20 | $league = (object) $client->leagues()->find('289646328504385536'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 21 | |||
| 22 | $this->assertIsObject($league); |
||
| 23 | $this->assertEquals('289646328504385536', $league->league_id); |
||
| 24 | $this->assertEquals('Sleeper Friends League', $league->name); |
||
| 25 | $this->assertEquals('2018', $league->season); |
||
| 26 | $this->assertCount(15, $league->roster_positions); |
||
| 27 | $this->assertIsArray($league->settings); |
||
| 28 | } |
||
| 29 | |||
| 30 | public function testLeagues() |
||
| 31 | { |
||
| 32 | $data = file_get_contents(__DIR__ . '/data/leagues.json'); |
||
| 33 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 34 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 35 | |||
| 36 | $client = new SleeperClient($mock); |
||
| 37 | |||
| 38 | $leagues = $client->leagues()->byUser('457511950237696', 2018); |
||
| 39 | $league = (object) $leagues[0]; |
||
| 40 | |||
| 41 | $this->assertIsArray($leagues); |
||
| 42 | $this->assertCount(2, $leagues); |
||
| 43 | $this->assertEquals('337383787396628480', $league->league_id); |
||
| 44 | $this->assertEquals('Dynasty Warriors', $league->name); |
||
| 45 | $this->assertEquals('2018', $league->season); |
||
| 46 | $this->assertCount(18, $league->roster_positions); |
||
| 47 | $this->assertIsArray($league->settings); |
||
| 48 | } |
||
| 49 | |||
| 50 | public function testLeaguesSeasonException() |
||
| 51 | { |
||
| 52 | $response = new GuzzleHttp\Psr7\Response(200, [], null); |
||
| 53 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 54 | |||
| 55 | $client = new SleeperClient($mock); |
||
| 56 | |||
| 57 | $this->expectException(InvalidArgumentException::class); |
||
| 58 | $this->expectExceptionMessage("byUser function only accepts seasons since 2015 and sport type 'nfl'. Inputs were: 1988, nfl"); |
||
| 59 | |||
| 60 | $client->leagues()->byUser('457511950237696', 1988); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function testLeaguesSportException() |
||
| 64 | { |
||
| 65 | $response = new GuzzleHttp\Psr7\Response(200, [], null); |
||
| 66 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 67 | |||
| 68 | $client = new SleeperClient($mock); |
||
| 69 | |||
| 70 | $this->expectException(InvalidArgumentException::class); |
||
| 71 | $this->expectExceptionMessage("byUser function only accepts seasons since 2015 and sport type 'nfl'. Inputs were: 2018, none"); |
||
| 72 | |||
| 73 | $client->leagues()->byUser('457511950237696', 2018, 'none'); |
||
| 74 | } |
||
| 75 | |||
| 76 | public function testState() |
||
| 77 | { |
||
| 78 | $data = file_get_contents(__DIR__ . '/data/state.json'); |
||
| 79 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 80 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 81 | |||
| 82 | $client = new SleeperClient($mock); |
||
| 83 | |||
| 84 | $state = (object) $client->leagues()->state(); |
||
| 85 | |||
| 86 | $this->assertIsObject($state); |
||
| 87 | $this->assertEquals(2, $state->week); |
||
| 88 | $this->assertEquals('2021-09-09', $state->season_start_date); |
||
| 89 | $this->assertEquals('2020', $state->previous_season); |
||
| 90 | $this->assertEquals('2021', $state->season); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function testStateException() |
||
| 94 | { |
||
| 95 | $response = new GuzzleHttp\Psr7\Response(200, [], null); |
||
| 96 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 97 | |||
| 98 | $client = new SleeperClient($mock); |
||
| 99 | |||
| 100 | $this->expectException(InvalidArgumentException::class); |
||
| 101 | $this->expectExceptionMessage("state function only accepts sports like nfl, nba, lcs. Input was: none"); |
||
| 102 | |||
| 103 | $client->leagues()->state('none'); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function testUsers() |
||
| 107 | { |
||
| 108 | $data = file_get_contents(__DIR__ . '/data/users.json'); |
||
| 109 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 110 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 111 | |||
| 112 | $client = new SleeperClient($mock); |
||
| 113 | |||
| 114 | $users = $client->leagues()->users('289646328504385536'); |
||
| 115 | $user = (object) $users[0]; |
||
| 116 | |||
| 117 | $this->assertIsArray($users); |
||
| 118 | $this->assertCount(14, $users); |
||
| 119 | $this->assertEquals('457511950237696', $user->user_id); |
||
| 120 | $this->assertTrue($user->is_owner); |
||
| 121 | $this->assertEquals('2KSports', $user->display_name); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function testMatchup() |
||
| 125 | { |
||
| 126 | $data = file_get_contents(__DIR__ . '/data/matchups.json'); |
||
| 127 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 128 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 129 | |||
| 130 | $client = new SleeperClient($mock); |
||
| 131 | |||
| 132 | $matchups = $client->leagues()->matchups('337383787396628480', 3); |
||
| 133 | $matchup = (object) $matchups[0]; |
||
| 134 | |||
| 135 | $this->assertIsArray($matchups); |
||
| 136 | $this->assertCount(12, $matchups); |
||
| 137 | $this->assertEquals(108.9, $matchup->points); |
||
| 138 | $this->assertIsArray($matchup->starters); |
||
| 139 | $this->assertIsArray($matchup->players); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function testMatchupWeekException() |
||
| 143 | { |
||
| 144 | $response = new GuzzleHttp\Psr7\Response(200, [], null); |
||
| 145 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 146 | |||
| 147 | $client = new SleeperClient($mock); |
||
| 148 | |||
| 149 | $this->expectException(InvalidArgumentException::class); |
||
| 150 | $this->expectExceptionMessage("matchups function only accepts weeks between 1 and 16. Input was: 0"); |
||
| 151 | |||
| 152 | $client->leagues()->matchups('337383787396628480', 0); |
||
| 153 | } |
||
| 154 | |||
| 155 | public function testRoster() |
||
| 156 | { |
||
| 157 | $data = file_get_contents(__DIR__ . '/data/rosters.json'); |
||
| 158 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 159 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 160 | |||
| 161 | $client = new SleeperClient($mock); |
||
| 162 | |||
| 163 | $rosters = $client->leagues()->rosters('337383787396628480'); |
||
| 164 | $roster = (object) $rosters[0]; |
||
| 165 | |||
| 166 | $this->assertIsArray($rosters); |
||
| 167 | $this->assertCount(12, $rosters); |
||
| 168 | $this->assertEquals('189140835533586432', $roster->owner_id); |
||
| 169 | $this->assertEquals('289646328504385536', $roster->league_id); |
||
| 170 | $this->assertIsArray($roster->settings); |
||
| 171 | $this->assertEquals(7, $roster->settings['wins']); |
||
| 172 | $this->assertEquals(6, $roster->settings['losses']); |
||
| 173 | } |
||
| 174 | |||
| 175 | public function testLosersBracket() |
||
| 176 | { |
||
| 177 | $data = file_get_contents(__DIR__ . '/data/playoff-bracket-loser.json'); |
||
| 178 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 179 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 180 | |||
| 181 | $client = new SleeperClient($mock); |
||
| 182 | |||
| 183 | $bracket = $client->leagues()->losersBracket('289646328504385536'); |
||
| 184 | $firstMatchup = (object) $bracket[0]; |
||
| 185 | $secondMatchup = (object) $bracket[2]; |
||
| 186 | |||
| 187 | $this->assertIsArray($bracket); |
||
| 188 | $this->assertCount(7, $bracket); |
||
| 189 | $this->assertEquals(7, $firstMatchup->w); |
||
| 190 | $this->assertEquals(1, $firstMatchup->r); |
||
| 191 | $this->assertEquals(1, $secondMatchup->t2_from['w']); |
||
| 192 | $this->assertEquals(7, $secondMatchup->l); |
||
| 193 | $this->assertEquals(11, $secondMatchup->w); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function testWinnersBracket() |
||
| 197 | { |
||
| 198 | $data = file_get_contents(__DIR__ . '/data/playoff-bracket-winner.json'); |
||
| 199 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 200 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 201 | |||
| 202 | $client = new SleeperClient($mock); |
||
| 203 | |||
| 204 | $bracket = $client->leagues()->winnersBracket('289646328504385536'); |
||
| 205 | $firstMatchup = (object) $bracket[0]; |
||
| 206 | $secondMatchup = (object) $bracket[3]; |
||
| 207 | |||
| 208 | $this->assertIsArray($bracket); |
||
| 209 | $this->assertCount(7, $bracket); |
||
| 210 | $this->assertEquals(1, $firstMatchup->w); |
||
| 211 | $this->assertEquals(5, $firstMatchup->t1); |
||
| 212 | $this->assertEquals(2, $secondMatchup->t2_from['w']); |
||
| 213 | $this->assertEquals(2, $secondMatchup->l); |
||
| 214 | $this->assertEquals(3, $secondMatchup->w); |
||
| 215 | } |
||
| 216 | |||
| 217 | public function testTransactions() |
||
| 218 | { |
||
| 219 | $data = file_get_contents(__DIR__ . '/data/transactions.json'); |
||
| 220 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 221 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 222 | |||
| 223 | $client = new SleeperClient($mock); |
||
| 224 | |||
| 225 | $transactions = $client->leagues()->transactions('289646328504385536', 1); |
||
| 226 | $firstTransaction = (object) $transactions[0]; |
||
| 227 | $secondTransaction = (object) $transactions[2]; |
||
| 228 | |||
| 229 | $this->assertIsArray($transactions); |
||
| 230 | $this->assertEquals('Unfortunately, your roster will have too many players after this transaction.', $firstTransaction->metadata['notes']); |
||
| 231 | $this->assertEquals('waiver', $firstTransaction->type); |
||
| 232 | $this->assertEquals('failed', $firstTransaction->status); |
||
| 233 | $this->assertEquals('Your waiver claim was processed successfully!', $secondTransaction->metadata['notes']); |
||
| 234 | $this->assertEquals('waiver', $secondTransaction->type); |
||
| 235 | $this->assertEquals('complete', $secondTransaction->status); |
||
| 236 | } |
||
| 237 | |||
| 238 | public function testTransactionsRoundException() |
||
| 239 | { |
||
| 240 | $response = new GuzzleHttp\Psr7\Response(200, [], null); |
||
| 241 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 242 | |||
| 243 | $client = new SleeperClient($mock); |
||
| 244 | |||
| 245 | $this->expectException(InvalidArgumentException::class); |
||
| 246 | $this->expectExceptionMessage("transactions function only accepts rounds between 1 and 16. Input was: 17"); |
||
| 247 | |||
| 248 | $client->leagues()->transactions('289646328504385536', 17); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function testTradedPicks() |
||
| 252 | { |
||
| 253 | $data = file_get_contents(__DIR__ . '/data/traded-picks.json'); |
||
| 254 | $response = new GuzzleHttp\Psr7\Response(200, [], $data); |
||
| 255 | $mock = new GuzzleHttp\Handler\MockHandler([ $response, $response ]); |
||
| 256 | |||
| 257 | $client = new SleeperClient($mock); |
||
| 258 | |||
| 259 | $picks = $client->leagues()->tradedPicks('289646328504385536'); |
||
| 260 | $firstTrade = (object) $picks[0]; |
||
| 261 | |||
| 262 | $this->assertIsArray($picks); |
||
| 263 | $this->assertEquals('2019', $firstTrade->season); |
||
| 264 | $this->assertEquals(5, $firstTrade->round); |
||
| 265 | $this->assertEquals(1, $firstTrade->roster_id); |
||
| 266 | $this->assertEquals(1, $firstTrade->previous_owner_id); |
||
| 267 | $this->assertEquals(2, $firstTrade->owner_id); |
||
| 268 | } |
||
| 269 | |||
| 270 | } |
||
| 271 |