From 1a176c39e386cc997628f5d5b9fd8d4ac9e9599e Mon Sep 17 00:00:00 2001 From: ARCANEDEV Date: Wed, 6 Feb 2019 10:12:02 +0100 Subject: [PATCH] Updating the package --- .scrutinizer.yml | 2 +- .travis.yml | 7 ++- LICENSE.md | 2 +- README.md | 2 +- composer.json | 16 +++---- src/Http/ApiResource.php | 2 +- tests/ApiHelperServiceProviderTest.php | 4 +- tests/Http/ApiResourceTest.php | 7 ++- tests/Http/FormRequestTest.php | 19 ++++---- tests/Http/JsonResponseTest.php | 28 ++++++------ tests/Http/Middleware/AjaxOnlyTest.php | 10 ++--- tests/TestCase.php | 7 +-- tests/TransformerTest.php | 61 ++++++++++++-------------- 13 files changed, 77 insertions(+), 90 deletions(-) diff --git a/.scrutinizer.yml b/.scrutinizer.yml index 2880858..5dc7cd4 100644 --- a/.scrutinizer.yml +++ b/.scrutinizer.yml @@ -23,7 +23,7 @@ checks: tools: external_code_coverage: timeout: 600 - runs: 2 + runs: 4 php_code_sniffer: enabled: true config: diff --git a/.travis.yml b/.travis.yml index 15caf38..1865a56 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,18 +5,17 @@ sudo: false php: - 7.0 - 7.1 + - 7.2 + - 7.3 - nightly matrix: allow_failures: - php: nightly -env: - - TESTBENCH_VERSION=3.5.* - before_script: - travis_retry composer self-update - - travis_retry composer require --prefer-source --no-interaction --dev "orchestra/testbench-browser-kit:${TESTBENCH_VERSION}" + - travis_retry composer install --prefer-source --no-interaction script: - composer validate diff --git a/LICENSE.md b/LICENSE.md index da9be09..77e2238 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) 2017 ARCANEDEV - Laravel API Helper +Copyright (c) 2017-2019 ARCANEDEV - Laravel API Helper Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index a05b90d..6f9995e 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Laravel API Helper is a package that allows you to add features to build api fas * Easy setup & configuration. * Well documented & IDE Friendly. * Well tested with maximum code quality. - * Laravel `5.1 | 5.2 | 5.3 | 5.4 | 5.5` are supported. + * Laravel `5.1` to `5.5` are supported. * Made with :heart: & :coffee:. ## Contribution diff --git a/composer.json b/composer.json index 01630a1..4569fe8 100644 --- a/composer.json +++ b/composer.json @@ -15,11 +15,13 @@ "license": "MIT", "require": { "php": ">=7.0", - "arcanedev/support": "~4.0" + "ext-json": "*", + "arcanedev/support": "~4.2.0" }, "require-dev": { - "phpunit/phpunit": "~6.0", - "phpunit/phpcov": "~4.0" + "phpunit/phpunit": "~6.0", + "phpunit/phpcov": "~4.0", + "orchestra/testbench": "~3.5.0" }, "autoload": { "psr-4": { @@ -31,13 +33,5 @@ "psr-4": { "Arcanedev\\LaravelApiHelper\\Tests\\": "tests/" } - }, - "extra": { - "branch-alias": { - "dev-master": "1.0-dev" - } - }, - "scripts": { - "testbench": "composer require --dev \"orchestra/testbench-browser-kit=~3.0\"" } } diff --git a/src/Http/ApiResource.php b/src/Http/ApiResource.php index d43032f..902e2d3 100644 --- a/src/Http/ApiResource.php +++ b/src/Http/ApiResource.php @@ -301,7 +301,7 @@ public function response($request = null) * * @param \Illuminate\Http\Request $request * - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\Response|mixed */ public function toResponse($request) { diff --git a/tests/ApiHelperServiceProviderTest.php b/tests/ApiHelperServiceProviderTest.php index 248c4ec..ed6bf82 100644 --- a/tests/ApiHelperServiceProviderTest.php +++ b/tests/ApiHelperServiceProviderTest.php @@ -51,7 +51,7 @@ public function it_can_be_instantiated() ]; foreach ($expectations as $expected) { - $this->assertInstanceOf($expected, $this->provider); + static::assertInstanceOf($expected, $this->provider); } } @@ -62,6 +62,6 @@ public function it_can_provides() \Arcanedev\LaravelApiHelper\Contracts\Http\JsonResponse::class ]; - $this->assertSame($expected, $this->provider->provides()); + static::assertSame($expected, $this->provider->provides()); } } diff --git a/tests/Http/ApiResourceTest.php b/tests/Http/ApiResourceTest.php index 990bfe2..329bbf2 100644 --- a/tests/Http/ApiResourceTest.php +++ b/tests/Http/ApiResourceTest.php @@ -32,15 +32,14 @@ public function setUp() /** @test */ public function it_can_be_instantiated() { - $user = new User([ + $user = User::query()->create([ 'first_name' => 'John', 'last_name' => 'DOE', 'email' => 'j.doe@example.com', ]); - $user->save(); - $this->assertJson($content = UserResource::make($user)->response()->content()); - $this->assertSame([ + static::assertJson($content = UserResource::make($user)->response()->content()); + static::assertSame([ 'data' => [ 'hashed_id' => 'hashed_id_here', 'full_name' => 'John DOE', diff --git a/tests/Http/FormRequestTest.php b/tests/Http/FormRequestTest.php index 0b5c601..fde7ca0 100644 --- a/tests/Http/FormRequestTest.php +++ b/tests/Http/FormRequestTest.php @@ -1,6 +1,5 @@ json('POST', 'form-request')->response; + $response = $this->json('POST', 'form-request'); - $this->assertFalse($response->isOk()); - $this->assertSame(422, $response->status()); + $response->assertStatus(422); - $this->assertJson($content = $response->content()); + static::assertJson($content = $response->content()); - $this->assertSame([ + static::assertSame([ 'message' => 'The given data was invalid.', 'errors' => [ 'name' => [ @@ -45,16 +43,15 @@ public function it_can_validate_form_request() $response = $this->json('POST', 'form-request', [ 'name' => 'ARCANEDEV', 'email' => 'arcanedev@example.com', - ])->response; + ]); - $this->assertTrue($response->isOk()); - $this->assertSame(200, $response->status()); + $response->assertSuccessful(); - $this->assertJson($response->content()); + $this->assertJson($content = $response->content()); $this->assertSame(json_encode([ 'status' => 200, 'code' => 'success', 'message' => 'Everything is good !', - ]), $response->content()); + ]), $content); } } diff --git a/tests/Http/JsonResponseTest.php b/tests/Http/JsonResponseTest.php index 007ff73..bfc9555 100644 --- a/tests/Http/JsonResponseTest.php +++ b/tests/Http/JsonResponseTest.php @@ -47,7 +47,7 @@ public function it_can_respond_with_success_response() { $response = $this->jsonResponse->success(['foo' => 'bar']); - $this->assertJsonResponse($response); + static::assertJsonResponse($response); $expected = [ 'status' => 200, @@ -55,7 +55,7 @@ public function it_can_respond_with_success_response() 'foo' => 'bar', ]; - $this->assertSame($expected, $response->getData(true)); + static::assertSame($expected, $response->getData(true)); } /** @test */ @@ -71,18 +71,18 @@ public function it_can_respond_with_error_response() 'message' => $message, ]; - $this->assertJsonResponse($response); - $this->assertSame($expected, $response->getData(true)); + static::assertJsonResponse($response); + static::assertSame($expected, $response->getData(true)); } /** @test */ public function it_can_respond_with_success_response_via_trait() { /** @var \Illuminate\Http\JsonResponse $response */ - $response = $this->json('GET', '/valid-slug')->response; + $response = $this->json('GET', '/valid-slug')->baseResponse; - $this->assertJsonResponse($response); - $this->assertTrue($response->isOk()); + static::assertJsonResponse($response); + static::assertTrue($response->isOk()); $expected = [ 'status' => 200, @@ -91,17 +91,17 @@ public function it_can_respond_with_success_response_via_trait() 'content' => 'Post content', ]; - $this->assertSame($expected, $response->getData(true)); + static::assertSame($expected, $response->getData(true)); } /** @test */ public function it_can_respond_with_error_response_via_trait() { /** @var \Illuminate\Http\JsonResponse $response */ - $response = $this->json('GET', '/invalid-slug')->response; + $response = $this->json('GET', '/invalid-slug')->baseResponse; - $this->assertJsonResponse($response); - $this->assertFalse($response->isOk()); + static::assertJsonResponse($response); + static::assertFalse($response->isOk()); $expected = [ 'status' => 404, @@ -109,7 +109,7 @@ public function it_can_respond_with_error_response_via_trait() 'message' => 'Post not found with the given slug [invalid-slug]', ]; - $this->assertSame($expected, $response->getData(true)); + static::assertSame($expected, $response->getData(true)); } /* ----------------------------------------------------------------- @@ -122,8 +122,8 @@ public function it_can_respond_with_error_response_via_trait() * * @param \Illuminate\Http\JsonResponse $response */ - protected function assertJsonResponse($response) + public static function assertJsonResponse($response) { - $this->assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response); + static::assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response); } } diff --git a/tests/Http/Middleware/AjaxOnlyTest.php b/tests/Http/Middleware/AjaxOnlyTest.php index 4886056..c3def08 100644 --- a/tests/Http/Middleware/AjaxOnlyTest.php +++ b/tests/Http/Middleware/AjaxOnlyTest.php @@ -19,9 +19,9 @@ class AjaxOnlyTest extends TestCase public function it_can_pass_ajax_request() { /** @var \Illuminate\Http\JsonResponse $response */ - $response = $this->json('GET', '/')->response; + $response = $this->json('GET', '/')->baseResponse; - $this->assertTrue($response->isOk()); + static::assertTrue($response->isOk()); $expected = [ 'status' => 200, @@ -29,7 +29,7 @@ public function it_can_pass_ajax_request() 'message' => 'Hello world', ]; - $this->assertSame($expected, $response->getData(true)); + static::assertSame($expected, $response->getData(true)); } /** @test */ @@ -38,7 +38,7 @@ public function it_must_block_non_ajax_request() /** @var \Illuminate\Http\JsonResponse $response */ $response = $this->call('GET', '/'); - $this->assertFalse($response->isOk()); + static::assertFalse($response->isOk()); $expected = [ 'status' => 403, @@ -46,6 +46,6 @@ public function it_must_block_non_ajax_request() 'message' => 'Invalid AJAX Request', ]; - $this->assertSame($expected, $response->getData(true)); + static::assertSame($expected, $response->getData(true)); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index c4c8efe..5ed81be 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,7 +1,8 @@ increments('id'); $table->string('first_name'); $table->string('last_name'); $table->string('email'); }); - Schema::create('posts', function ($table) { + Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id'); $table->string('title'); diff --git a/tests/TransformerTest.php b/tests/TransformerTest.php index 4aec1ba..e9e125f 100644 --- a/tests/TransformerTest.php +++ b/tests/TransformerTest.php @@ -39,8 +39,8 @@ public function it_can_be_instantiated() { $transformer = new BasicTransformer($data = ['foo' => 'bar']); - $this->assertSame(compact('data'), $transformer->transform()); - $this->assertSame(compact('data'), $transformer->toArray()); + static::assertSame(compact('data'), $transformer->transform()); + static::assertSame(compact('data'), $transformer->toArray()); } /** @test */ @@ -50,10 +50,10 @@ public function it_can_transform_to_response() $response = $transformer->toResponse(); - $this->assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response); + static::assertInstanceOf(\Illuminate\Http\JsonResponse::class, $response); - $this->assertSame(200, $response->status()); - $this->assertSame(compact('data'), $response->getData(true)); + static::assertSame(200, $response->status()); + static::assertSame(compact('data'), $response->getData(true)); } /** @test */ @@ -64,7 +64,7 @@ public function it_can_set_extra_data_to_transformer() $transformer->setExtra('code', 'error') ->setExtra('status', 500); - $this->assertSame([ + static::assertSame([ 'code' => 'error', 'data' => ['foo' => 'bar'], 'status' => 500, @@ -75,7 +75,7 @@ public function it_can_set_extra_data_to_transformer() 'status' => 400 ]); - $this->assertSame([ + static::assertSame([ 'code' => 'success', 'data' => ['foo' => 'bar'], 'status' => 400, @@ -87,9 +87,9 @@ public function it_can_convert_transformer_to_json() { $transformer = BasicTransformer::with($data = ['foo' => 'bar']); - $this->assertInstanceOf(\Illuminate\Contracts\Support\Jsonable::class, $transformer); + static::assertInstanceOf(\Illuminate\Contracts\Support\Jsonable::class, $transformer); - $this->assertJsonStringEqualsJsonString( + static::assertJsonStringEqualsJsonString( json_encode(compact('data')), $transformer->toJson() ); } @@ -110,8 +110,8 @@ public function it_can_transform_a_fluent_object() ], ]; - $this->assertSame($expected, $transformer->transform()); - $this->assertSame($expected, $transformer->toArray()); + static::assertSame($expected, $transformer->transform()); + static::assertSame($expected, $transformer->toArray()); } /** @test */ @@ -127,8 +127,8 @@ public function it_can_transform_a_model() ], ]; - $this->assertSame($expected, $transformer->transform()); - $this->assertSame($expected, $transformer->toArray()); + static::assertSame($expected, $transformer->transform()); + static::assertSame($expected, $transformer->toArray()); } /** @test */ @@ -165,8 +165,8 @@ public function it_can_transform_a_model_with_relationship_and_custom_meta() ], ]; - $this->assertSame($expected, $transformer->transform()); - $this->assertSame($expected, $transformer->toArray()); + static::assertSame($expected, $transformer->transform()); + static::assertSame($expected, $transformer->toArray()); } /** @test */ @@ -188,8 +188,8 @@ public function it_can_transform_a_collection() ], ]; - $this->assertSame($expected, $transformer->transform()); - $this->assertSame($expected, $transformer->toArray()); + static::assertSame($expected, $transformer->transform()); + static::assertSame($expected, $transformer->toArray()); } /** @test */ @@ -210,9 +210,10 @@ public function it_can_transform_a_paginator() ]; $data = $transformed['data']; - $this->assertCount(5, $data); + static::assertCount(5, $data); + foreach ($data as $item) { - $this->assertSame($expectedItem, $item); + static::assertSame($expectedItem, $item); } $expectedMeta = [ @@ -229,7 +230,7 @@ public function it_can_transform_a_paginator() 'total' => 10, ]; - $this->assertEquals($expectedMeta, $transformed['meta']); + static::assertEquals($expectedMeta, $transformed['meta']); } /** @test */ @@ -244,7 +245,7 @@ public function it_can_transform_a_paginator_with_custom_meta() $transformer->setExtra('meta', ['type' => 'paginator']); - $this->assertEquals([ + static::assertEquals([ 'data' => [ [ 'title' => 'Post title', @@ -278,6 +279,13 @@ public function it_can_transform_a_paginator_with_custom_meta() | ----------------------------------------------------------------- */ + protected function makePosts($count = 3) + { + return collect()->pad($count, null)->transform(function () { + return $this->makePost(); + }); + } + protected function makePost() { return new Post([ @@ -285,15 +293,4 @@ protected function makePost() 'content' => 'Post content', ]); } - - protected function makePosts($count = 3) - { - $posts = collect(); - - for ($i = 0; $i < $count; $i++) { - $posts->push($this->makePost()); - } - - return $posts; - } }