Skip to content

Commit

Permalink
✨ Add setup and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonJnsson committed Nov 27, 2023
1 parent e1df1f0 commit ce688ab
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 9 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
"require": {
"php": "^8.1",
"spatie/laravel-package-tools": "^1.14.0",
"illuminate/contracts": "^10.0"
"illuminate/contracts": "^10.0",
"illuminate/http": "^10.0",
"morning-train/e-conomic": "dev-main"
},
"require-dev": {
"laravel/pint": "^1.0",
Expand All @@ -31,7 +33,8 @@
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-deprecation-rules": "^1.0",
"phpstan/phpstan-phpunit": "^1.0",
"spatie/laravel-ray": "^1.26"
"spatie/laravel-ray": "^1.26",
"guzzlehttp/guzzle": "^7.5"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions config/e-conomic.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

// config for MorningTrain/LaravelEConomic
return [

'app_secret_token' => env('ECONOMIC_APP_SECRET_TOKEN'),
'agreement_grant_token' => env('ECONOMIC_AGREEMENT_GRANT_TOKEN'),
];
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,8 @@
<directory suffix=".php">./src</directory>
</include>
</source>
<php>
<env name="ECONOMIC_APP_SECRET_TOKEN" value="SECRET_TOKEN"/>
<env name="ECONOMIC_AGREEMENT_GRANT_TOKEN" value="GRANT_TOKEN"/>
</php>
</phpunit>
45 changes: 45 additions & 0 deletions src/Drivers/HttpEconomicDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace MorningTrain\LaravelEConomic\Drivers;

use Illuminate\Support\Facades\Http;
use MorningTrain\Economic\Classes\EconomicResponse;
use MorningTrain\Economic\Interfaces\EconomicDriver;

class HttpEconomicDriver implements EconomicDriver
{
public function get(string $url, array $queryArgs = []): EconomicResponse
{
$response = Http::economic()->get($url, $queryArgs);

Check failure on line 13 in src/Drivers/HttpEconomicDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Facades\Http::economic().

return new EconomicResponse($response->status(), $response->json());
}

public function post(string $url, array $body = []): EconomicResponse
{
$response = Http::economic()->post($url, $body);

Check failure on line 20 in src/Drivers/HttpEconomicDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Facades\Http::economic().

return new EconomicResponse($response->status(), $response->json());
}

public function put(string $url, array $body = []): EconomicResponse
{
$response = Http::economic()->put($url, $body);

Check failure on line 27 in src/Drivers/HttpEconomicDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Facades\Http::economic().

return new EconomicResponse($response->status(), $response->json());
}

public function delete(string $url): EconomicResponse
{
$response = Http::economic()->delete($url);

Check failure on line 34 in src/Drivers/HttpEconomicDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Facades\Http::economic().

return new EconomicResponse($response->status(), $response->json());
}

public function patch(string $url): EconomicResponse
{
$response = Http::economic()->patch($url);

Check failure on line 41 in src/Drivers/HttpEconomicDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined static method Illuminate\Support\Facades\Http::economic().

return new EconomicResponse($response->status(), $response->json());
}
}
20 changes: 20 additions & 0 deletions src/LaravelEconomicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,31 @@

namespace MorningTrain\LaravelEConomic;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use MorningTrain\Economic\Services\EconomicApiService;
use MorningTrain\LaravelEConomic\Drivers\HttpEconomicDriver;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class LaravelEconomicServiceProvider extends PackageServiceProvider
{
public function boot()
{
Http::macro('economic', function (): PendingRequest {
/** @var \Illuminate\Http\Client\Factory $this */
return $this
->withHeaders([
'X-AppSecretToken' => config('e-conomic.app_secret_token'),
'X-AgreementGrantToken' => config('e-conomic.agreement_grant_token'),
'Content-Type' => 'application/json',
])
->throw();
});

EconomicApiService::setDriver(new HttpEconomicDriver());
}

public function configurePackage(Package $package): void
{
/*
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

5 changes: 5 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

use Illuminate\Support\Facades\Http;
use MorningTrain\LaravelEConomic\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);

uses()->beforeEach(function() {
Http::preventStrayRequests();
})->in(__DIR__);
35 changes: 35 additions & 0 deletions tests/Unit/DriverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use MorningTrain\Economic\Resources\Customer;

it('adds headers from env', function () {
Http::fake([
'*' => Http::response([]),
]);

Customer::first();

Http::assertSent(function (Request $request) {
return $request->hasHeader('X-AppSecretToken', 'SECRET_TOKEN')
&& $request->hasHeader('X-AgreementGrantToken', 'GRANT_TOKEN')
&& $request->hasHeader('Content-Type');
});
});

it('sends request with filters', function () {
Http::fake([
'https://restapi.e-conomic.com/customers*' => Http::response([]),
]);

Customer::where('name', 'Morningtrain')->first();

Http::assertSent(function (Request $request) {
return $request->data() === [
'pageSize' => 1,
'skipPages' => 0,
'filter' => 'name$eq:Morningtrain',
];
});
});

0 comments on commit ce688ab

Please sign in to comment.