Skip to content

Commit

Permalink
Merge pull request #9 from richard-muvirimi/development
Browse files Browse the repository at this point in the history
Add api end point tests, scope tests
  • Loading branch information
richard-muvirimi committed Sep 27, 2023
2 parents 842d925 + ee85629 commit 8959883
Show file tree
Hide file tree
Showing 14 changed files with 721 additions and 55 deletions.
28 changes: 0 additions & 28 deletions app/GraphQL/Scalars/UnixTimeStamp.php

This file was deleted.

1 change: 1 addition & 0 deletions app/Models/Rate.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Rate extends Model
protected $casts = [
'status' => 'boolean',
'enabled' => 'boolean',
'javascript' => 'boolean',
'rate' => 'float',
'rate_updated_at' => 'datetime',
'updated_at' => 'datetime',
Expand Down
2 changes: 1 addition & 1 deletion app/Traits/ScrapesRates.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private function getHtmlContent(Rate $theRate): string
'timeout' => env('SCRAPPY_TIMEOUT'),
'user_agent' => $this->getUserAgent(),
'css' => 'body',
'javascript' => $theRate->javascript ? 'true' : 'false',
'javascript' => strval($theRate->javascript),
];

$options = [
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"vendor/bin/pint ."
],
"crawl": "@php spark crawl",
"test": "php artisan test",
"test": "php artisan test --stop-on-failure",
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
Expand Down
4 changes: 1 addition & 3 deletions graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
"A datetime string with format `Y-m-d H:i:s`, e.g. `2018-05-23 13:43:32`."
scalar DateTime @scalar(class: "Nuwave\\Lighthouse\\Schema\\Types\\Scalars\\DateTime")

scalar UnixTimeStamp @scalar(class: "UnixTimeStamp")

"Indicates what fields are available at the top level of a query operation."
type Query {

rate(
search: String @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "search") @rules(apply: ["string"])
date: UnixTimeStamp @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "date") @rules(apply: ["numeric", "date_format:U", "before:now"])
date: Int @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "date") @rules(apply: ["numeric", "date_format:U", "before:now"])
currency: Currency @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "currency") @rules(apply : ["string", "exists:rates,rate_currency"])
cors: Boolean @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "cors") @rules(apply: ["boolean"])
prefer: Prefer @scope(name : "logAnalyticsEvent") @scope(name : "enabled") @scope(name : "updated") @scope(name: "preferred") @rules(apply: ["string", "in:MIN,min,MAX,max,MEAN,mean,MEDIAN,median,RANDOM,random,MODE,mode"])
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</source>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_URL" value="http://localhost/zimrate/public_html"/>
<env name="APP_URL" value="http://127.0.0.1:8000"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<!-- <env name="DB_CONNECTION" value="sqlite"/> -->
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ current days rate.

5. Visit `your-site/api` or `your-site/api/v1` for the api

### Tests

1. Make sure the server is running `php artisan serve`
2. Run `php artisan test`

### Contributions and Issues

Contributions are more than welcome, as well as issues
4 changes: 3 additions & 1 deletion tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ trait CreatesApplication
*/
public function createApplication(): Application
{
$app = require __DIR__.'/../bootstrap/app.php';
$app = require __DIR__ . '/../bootstrap/app.php';

$app->usePublicPath(dirname(__DIR__) . '/public_html');

$app->make(Kernel::class)->bootstrap();

Expand Down
208 changes: 208 additions & 0 deletions tests/Feature/ApiGraphqlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\Option;
use App\Models\Rate;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\TestCase;
use Nuwave\Lighthouse\Testing\MakesGraphQLRequests;
use Nuwave\Lighthouse\Testing\RefreshesSchemaCache;
use Tests\CreatesApplication;

/**
* Graphql Api Test Class
*/
class ApiGraphqlTest extends TestCase
{
use CreatesApplication;
use MakesGraphQLRequests;
use RefreshesSchemaCache;

/**
* Test the api responds.
*/
public function test_api_responds(): void
{
$response = $this->graphQl(/** @lang GraphQL */ 'query {USD : rate { currency last_checked last_updated name rate url }}');

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'USD' => [
"*" => [
'currency',
'last_checked',
'last_updated',
'name',
'rate',
'url'
],
],
],
]);

Rate::query()->enabled()->updated()->get(["rate_currency", "updated_at", "rate_updated_at", "rate_name", "rate", "source_url"])->each(function (Rate $rate) use ($response) {
$response->assertJsonFragment([
'currency' => $rate->rate_currency,
'last_checked' => $rate->last_checked,
'last_updated' => $rate->last_updated,
'name' => $rate->rate_name,
'rate' => $rate->rate,
'url' => $rate->source_url,
]);
});
}

/**
* Test the prefer aggregate of the api
*/
public function test_filter_prefer_aggregate_works(): void
{
$aggregate = "MEDIAN";

$response = $this->graphQl(/** @lang GraphQL */ 'query($prefer : Prefer!) {USD : rate(prefer : $prefer) { currency last_checked last_updated rate }}', ["prefer" => $aggregate]);

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'USD' => [
"*" => [
'currency',
'last_checked',
'last_updated',
'rate',
],
],
],
]);

Rate::query()->enabled()->updated()->preferred($aggregate)->get(["rate_currency", "updated_at", "rate_updated_at", "rate"])->each(function (Rate $rate) use ($response) {
$response->assertJsonFragment([
'currency' => $rate->rate_currency,
'last_checked' => $rate->last_checked,
'last_updated' => $rate->last_updated,
'rate' => $rate->rate,
]);
});
}

/**
* Test the currency of the api
*/
public function test_filter_currency_works(): void
{

$currency = Rate::query()->enabled()->updated()->first(['rate_currency'])->currency;

$response = $this->graphQl(/** @lang GraphQL */ 'query ($currency: Currency!) { USD: rate(currency : $currency) { currency last_checked last_updated name rate url }}', ["currency" => $currency]);

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'USD' => [
"*" => [
'currency',
'last_checked',
'last_updated',
'name',
'rate',
'url'
],
],
],
]);

Rate::query()->enabled()->updated()->currency($currency)->get(["rate_currency", "updated_at", "rate_updated_at", "rate_name", "rate", "source_url"])->each(function (Rate $rate) use ($response) {
$response->assertJsonFragment([
'currency' => $rate->rate_currency,
'last_checked' => $rate->last_checked,
'last_updated' => $rate->last_updated,
'name' => $rate->rate_name,
'rate' => $rate->rate,
'url' => $rate->source_url,
]);
});
}

/**
* Test the date of the api
*/
public function test_filter_date_works(): void
{

$date = Carbon::now()->subDay()->getTimestamp();

$response = $this->graphQl(/** @lang GraphQL */ 'query ($date : Int!) { USD : rate(date: $date) { currency last_checked last_updated name rate url }}', ["date" => $date]);

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'USD' => [
"*" => [
'currency',
'last_checked',
'last_updated',
'name',
'rate',
'url'
],
],
],
]);

Rate::query()->enabled()->updated()->date($date)->get(["rate_currency", "updated_at", "rate_updated_at", "rate_name", "rate", "source_url"])->each(function (Rate $rate) use ($response) {
$response->assertJsonFragment([
'currency' => $rate->rate_currency,
'last_checked' => $rate->last_checked,
'last_updated' => $rate->last_updated,
'name' => $rate->rate_name,
'rate' => $rate->rate,
'url' => $rate->source_url,
]);
});

}

/**
* Test the info is returned in response
*/
public function test_info_is_included_in_response(): void
{
$response = $this->graphQl(/** @lang GraphQL */ 'query {USD : rate { currency last_checked last_updated name rate url }, info: info}');

$response->assertStatus(200);
$response->assertJsonStructure([
'data' => [
'USD' => [
"*" => [
'currency',
'last_checked',
'last_updated',
'name',
'rate',
'url'
],
],
'info'
],
]);

$response->assertJsonFragment([
'info' => Option::query()->firstWhere('key', 'notice')->value('value'),
]);
}

/**
* Test the cors support of the api
*/
public function test_cors_headers_are_set(): void
{
$response = $this->graphQl(/** @lang GraphQL */ 'query($cors : Boolean!) {USD : rate(cors : $cors) { currency last_checked last_updated name rate url }}', ["cors" => true]);

$response->assertStatus(200);
$response->assertHeader('Access-Control-Allow-Origin', '*');
}
}
Loading

0 comments on commit 8959883

Please sign in to comment.