Skip to content

Commit ffb8203

Browse files
authored
Merge pull request #4 from SethSharp/tests
Add Tests
2 parents 2ff3e38 + 7454a98 commit ffb8203

File tree

6 files changed

+235
-20
lines changed

6 files changed

+235
-20
lines changed

.github/workflows/testing.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ jobs:
2626
- name: Install composer dependencies
2727
run: composer install --no-cache --no-ansi --no-interaction --no-progress
2828

29-
- name: Execute tests
30-
run: ./vendor/bin/phpunit
29+
- name: Execute pest tests
30+
run: ./vendor/bin/pest

composer.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,23 @@
2121
"nesbot/carbon": "^3.7",
2222
"illuminate/support": "^11.26"
2323
},
24-
"require-dev": {
25-
"phpunit/phpunit": "10.0"
26-
},
2724
"extra": {
2825
"laravel":{
2926
"providers": [
3027
"SethSharp\\OddsApi\\OddsApiServiceProvider"
3128
]
3229
}
30+
},
31+
"require-dev": {
32+
"pestphp/pest": "^3.2",
33+
"orchestra/testbench": "^9.5"
34+
},
35+
"config": {
36+
"allow-plugins": {
37+
"pestphp/pest-plugin": true
38+
}
39+
},
40+
"scripts": {
41+
"test": "pest"
3342
}
3443
}

src/OddsClient.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,10 @@ public function setBookmakers(mixed $bookmakers): self
159159
}
160160

161161
/**
162-
* @param string|array $eventId
162+
* @param mixed $eventId
163163
* @return $this
164164
*/
165-
public function setEvents(string|array $eventId): self
165+
public function setEvents(mixed $eventId): self
166166
{
167167
if (is_array($eventId)) {
168168
$this->params['eventIds'] = $this->buildEvents($eventId);

tests/Pest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Test Case
6+
|--------------------------------------------------------------------------
7+
|
8+
| The closure you provide to your test functions is always bound to a specific PHPUnit test
9+
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
10+
| need to change it using the "pest()" function to bind a different classes or traits.
11+
|
12+
*/
13+
14+
// pest()->extend(Tests\TestCase::class)->in('Feature');
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Expectations
19+
|--------------------------------------------------------------------------
20+
|
21+
| When you're writing tests, you often need to check that values meet certain conditions. The
22+
| "expect()" function gives you access to a set of "expectations" methods that you can use
23+
| to assert different things. Of course, you may extend the Expectation API at any time.
24+
|
25+
*/
26+
27+
expect()->extend('toBeOne', function () {
28+
return $this->toBe(1);
29+
});
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Functions
34+
|--------------------------------------------------------------------------
35+
|
36+
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37+
| project that you don't want to repeat in every file. Here you can also expose helpers as
38+
| global functions to help you to reduce the number of lines of code in your test files.
39+
|
40+
*/
41+
42+
function something()
43+
{
44+
// ..
45+
}

tests/TestCase.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/Unit/OddsClientTest.php

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace Tests\Unit;
4+
5+
use Orchestra\Testbench\TestCase;
6+
use SethSharp\OddsApi\OddsClient;
7+
8+
class OddsClientTest extends TestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [
13+
'SethSharp\OddsApi\OddsApiServiceProvider'
14+
];
15+
}
16+
17+
protected function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->app['config']->set('odds-api.default_region', 'au');
22+
$this->app['config']->set('odds-api.default_odds_format', 'decimal');
23+
}
24+
25+
public function testSetsUpProvidedApiKeyAndDefaultParams()
26+
{
27+
$apiKey = 'test-api-key';
28+
$oddsClient = new OddsClient($apiKey);
29+
30+
$this->assertEquals('https://api.the-odds-api.com', $oddsClient->getApiEndpoint());
31+
32+
$this->assertEquals([
33+
'api_key' => $apiKey,
34+
'regions' => 'au',
35+
'oddsFormat' => 'decimal',
36+
], $oddsClient->getParams());
37+
}
38+
39+
public function testSetsProvidedApiEndpoint()
40+
{
41+
$oddsClient = new OddsClient('test-api-key', 'a-new-endpoint');
42+
43+
$this->assertEquals('a-new-endpoint', $oddsClient->getApiEndpoint());
44+
}
45+
46+
public function testSetsRegionString()
47+
{
48+
$oddsClient = new OddsClient('test-api-key');
49+
50+
$oddsClient->setRegion('us');
51+
52+
$this->assertEquals('us', $oddsClient->getParams()['regions']);
53+
}
54+
55+
public function testSetsMarkets()
56+
{
57+
$oddsClient = new OddsClient('test-api-key');
58+
59+
$oddsClient->setMarkets([
60+
'h2h',
61+
'spreads',
62+
'outrights'
63+
]);
64+
65+
$this->assertEquals('0=h2h,1=spreads,2=outrights', $oddsClient->getParams()['markets']);
66+
}
67+
68+
public function testSetsBookmakers()
69+
{
70+
$oddsClient = new OddsClient('test-api-key');
71+
72+
$oddsClient->setBookmakers([
73+
'sportsbet',
74+
'tab',
75+
'topsport'
76+
]);
77+
78+
$this->assertEquals('0=sportsbet,1=tab,2=topsport', $oddsClient->getParams()['bookmakers']);
79+
}
80+
81+
public function testSetsEventsString()
82+
{
83+
84+
$oddsClient = new OddsClient('test-api-key');
85+
86+
$oddsClient->setEvents('some-event');
87+
88+
$this->assertEquals('some-event', $oddsClient->getParams()['eventIds']);
89+
}
90+
91+
public function testSetsEventsArray()
92+
{
93+
94+
$oddsClient = new OddsClient('test-api-key');
95+
96+
$oddsClient->setEvents([
97+
'event-1',
98+
'event-2',
99+
'event-3',
100+
]);
101+
102+
$this->assertEquals('event-1,event-2,event-3', $oddsClient->getParams()['eventIds']);
103+
}
104+
105+
public function testSetsOddsFormat()
106+
{
107+
$oddsClient = new OddsClient('test-api-key');
108+
109+
$oddsClient->oddsFormat('american');
110+
111+
$this->assertEquals('american', $oddsClient->getParams()['oddsFormat']);
112+
}
113+
114+
public function testSetsDateFormat()
115+
{
116+
$oddsClient = new OddsClient('test-api-key');
117+
118+
$oddsClient->dateFormat('unix');
119+
120+
$this->assertEquals('unix', $oddsClient->getParams()['dateForm']);
121+
}
122+
123+
public function testCommenceTimeFromAndConvertsToIso()
124+
{
125+
$oddsClient = new OddsClient('test-api-key');
126+
127+
$oddsClient->commenceTimeFrom('2024-05-10');
128+
129+
$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeFrom']);
130+
}
131+
132+
public function testCommenceTimeFrom()
133+
{
134+
$oddsClient = new OddsClient('test-api-key');
135+
136+
$oddsClient->commenceTimeFrom('2024-05-10T00:00:00Z', isIsoFormat: true);
137+
138+
$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeFrom']);
139+
}
140+
141+
public function testCommenceTimeToAndConvertsToIso()
142+
{
143+
$oddsClient = new OddsClient('test-api-key');
144+
145+
$oddsClient->commenceTimeTo('2024-05-10');
146+
147+
$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeTo']);
148+
}
149+
150+
public function testCommenceTimeTo()
151+
{
152+
$oddsClient = new OddsClient('test-api-key');
153+
154+
$oddsClient->commenceTimeTo('2024-05-10T00:00:00Z', isIsoFormat: true);
155+
156+
$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeTo']);
157+
}
158+
159+
public function testCanAddCustomParams()
160+
{
161+
$oddsClient = new OddsClient('test-api-key');
162+
163+
$oddsClient->addParams([
164+
'a-new-param' => 'some-value'
165+
]);
166+
167+
$this->assertEquals([
168+
'api_key' => 'test-api-key',
169+
'regions' => 'au',
170+
'oddsFormat' => 'decimal',
171+
'a-new-param' => 'some-value',
172+
], $oddsClient->getParams());
173+
}
174+
}

0 commit comments

Comments
 (0)