Skip to content

Commit

Permalink
Episode 7 complete
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed May 12, 2020
1 parent 2695f55 commit a52b1ce
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
13 changes: 10 additions & 3 deletions app/Clients/BestBuy.php
Expand Up @@ -9,11 +9,18 @@ class BestBuy implements Client
{
public function checkAvailability(Stock $stock): StockStatus
{
$results = Http::get('http://foo.test')->json();
$results = Http::get($this->endpoint($stock->sku))->json();

return new StockStatus(
$results['available'],
$results['price']
$results['onlineAvailability'],
(int) $results['salePrice'] * 100
);
}

protected function endpoint($sku): string
{
$key = config('services.clients.bestBuy.key');

return "https://api.bestbuy.com/v1/products/{$sku}.json?apiKey={$key}";
}
}
2 changes: 1 addition & 1 deletion app/Clients/StockStatus.php
Expand Up @@ -7,7 +7,7 @@ class StockStatus
public $available;
public $price;

public function __construct($available, $price)
public function __construct(bool $available, int $price)
{
$this->available = $available;
$this->price = $price;
Expand Down
5 changes: 5 additions & 0 deletions config/services.php
Expand Up @@ -30,4 +30,9 @@
'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
],

'clients' => [
'bestBuy' => [
'key' => env('BEST_BUY_KEY')
]
]
];
3 changes: 3 additions & 0 deletions phpunit.xml
Expand Up @@ -11,6 +11,9 @@
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Clients">
<directory suffix="Test.php">./tests/Clients</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
Expand Down
36 changes: 36 additions & 0 deletions tests/Clients/BestBuyTest.php
@@ -0,0 +1,36 @@
<?php

namespace Tests\Clients;

use App\Stock;
use App\Clients\BestBuy;
use Tests\TestCase;
use RetailerWithProductSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;

/**
* @group api
*/
class BestBuyTest extends TestCase
{
use RefreshDatabase;

/** @test */
function it_tracks_a_product()
{
$this->seed(RetailerWithProductSeeder::class);

$stock = tap(Stock::first())->update([
'sku' => '6364253', // Nintendo Switch sku
'url' => 'https://www.bestbuy.com/site/nintendo-switch-32gb-console-gray-joy-con/6364253.p?skuId=6364253'
]);

try {
(new BestBuy())->checkAvailability($stock);
} catch (\Exception $e) {
$this->fail('Failed to track the BestBuy API properly.');
}

$this->assertTrue(true);
}
}

0 comments on commit a52b1ce

Please sign in to comment.