Skip to content

Commit

Permalink
Merge pull request #4 from SethSharp/tests
Browse files Browse the repository at this point in the history
Add Tests
  • Loading branch information
SethSharp authored Oct 5, 2024
2 parents 2ff3e38 + 7454a98 commit ffb8203
Show file tree
Hide file tree
Showing 6 changed files with 235 additions and 20 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ jobs:
- name: Install composer dependencies
run: composer install --no-cache --no-ansi --no-interaction --no-progress

- name: Execute tests
run: ./vendor/bin/phpunit
- name: Execute pest tests
run: ./vendor/bin/pest
15 changes: 12 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,23 @@
"nesbot/carbon": "^3.7",
"illuminate/support": "^11.26"
},
"require-dev": {
"phpunit/phpunit": "10.0"
},
"extra": {
"laravel":{
"providers": [
"SethSharp\\OddsApi\\OddsApiServiceProvider"
]
}
},
"require-dev": {
"pestphp/pest": "^3.2",
"orchestra/testbench": "^9.5"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"scripts": {
"test": "pest"
}
}
4 changes: 2 additions & 2 deletions src/OddsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,10 +159,10 @@ public function setBookmakers(mixed $bookmakers): self
}

/**
* @param string|array $eventId
* @param mixed $eventId
* @return $this
*/
public function setEvents(string|array $eventId): self
public function setEvents(mixed $eventId): self
{
if (is_array($eventId)) {
$this->params['eventIds'] = $this->buildEvents($eventId);
Expand Down
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "pest()" function to bind a different classes or traits.
|
*/

// pest()->extend(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
13 changes: 0 additions & 13 deletions tests/TestCase.php

This file was deleted.

174 changes: 174 additions & 0 deletions tests/Unit/OddsClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

namespace Tests\Unit;

use Orchestra\Testbench\TestCase;
use SethSharp\OddsApi\OddsClient;

class OddsClientTest extends TestCase
{
protected function getPackageProviders($app)
{
return [
'SethSharp\OddsApi\OddsApiServiceProvider'
];
}

protected function setUp(): void
{
parent::setUp();

$this->app['config']->set('odds-api.default_region', 'au');
$this->app['config']->set('odds-api.default_odds_format', 'decimal');
}

public function testSetsUpProvidedApiKeyAndDefaultParams()
{
$apiKey = 'test-api-key';
$oddsClient = new OddsClient($apiKey);

$this->assertEquals('https://api.the-odds-api.com', $oddsClient->getApiEndpoint());

$this->assertEquals([
'api_key' => $apiKey,
'regions' => 'au',
'oddsFormat' => 'decimal',
], $oddsClient->getParams());
}

public function testSetsProvidedApiEndpoint()
{
$oddsClient = new OddsClient('test-api-key', 'a-new-endpoint');

$this->assertEquals('a-new-endpoint', $oddsClient->getApiEndpoint());
}

public function testSetsRegionString()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->setRegion('us');

$this->assertEquals('us', $oddsClient->getParams()['regions']);
}

public function testSetsMarkets()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->setMarkets([
'h2h',
'spreads',
'outrights'
]);

$this->assertEquals('0=h2h,1=spreads,2=outrights', $oddsClient->getParams()['markets']);
}

public function testSetsBookmakers()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->setBookmakers([
'sportsbet',
'tab',
'topsport'
]);

$this->assertEquals('0=sportsbet,1=tab,2=topsport', $oddsClient->getParams()['bookmakers']);
}

public function testSetsEventsString()
{

$oddsClient = new OddsClient('test-api-key');

$oddsClient->setEvents('some-event');

$this->assertEquals('some-event', $oddsClient->getParams()['eventIds']);
}

public function testSetsEventsArray()
{

$oddsClient = new OddsClient('test-api-key');

$oddsClient->setEvents([
'event-1',
'event-2',
'event-3',
]);

$this->assertEquals('event-1,event-2,event-3', $oddsClient->getParams()['eventIds']);
}

public function testSetsOddsFormat()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->oddsFormat('american');

$this->assertEquals('american', $oddsClient->getParams()['oddsFormat']);
}

public function testSetsDateFormat()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->dateFormat('unix');

$this->assertEquals('unix', $oddsClient->getParams()['dateForm']);
}

public function testCommenceTimeFromAndConvertsToIso()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->commenceTimeFrom('2024-05-10');

$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeFrom']);
}

public function testCommenceTimeFrom()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->commenceTimeFrom('2024-05-10T00:00:00Z', isIsoFormat: true);

$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeFrom']);
}

public function testCommenceTimeToAndConvertsToIso()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->commenceTimeTo('2024-05-10');

$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeTo']);
}

public function testCommenceTimeTo()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->commenceTimeTo('2024-05-10T00:00:00Z', isIsoFormat: true);

$this->assertEquals('2024-05-10T00:00:00Z', $oddsClient->getParams()['commenceTimeTo']);
}

public function testCanAddCustomParams()
{
$oddsClient = new OddsClient('test-api-key');

$oddsClient->addParams([
'a-new-param' => 'some-value'
]);

$this->assertEquals([
'api_key' => 'test-api-key',
'regions' => 'au',
'oddsFormat' => 'decimal',
'a-new-param' => 'some-value',
], $oddsClient->getParams());
}
}

0 comments on commit ffb8203

Please sign in to comment.