From 195726a02d872bba6e1d3247eda5e44574ff45ca Mon Sep 17 00:00:00 2001 From: Josh Forbes Date: Sat, 25 Jun 2016 09:35:23 -0400 Subject: [PATCH 1/4] Removing gateways. --- src/ForeignTransformer.php | 23 --- src/Gateway.php | 135 ---------------- tests/Integration/ForeignTransformerTest.php | 24 --- tests/Integration/GatewayTest.php | 159 ------------------- tests/Integration/TestGateway.php | 10 -- 5 files changed, 351 deletions(-) delete mode 100644 src/ForeignTransformer.php delete mode 100644 src/Gateway.php delete mode 100644 tests/Integration/ForeignTransformerTest.php delete mode 100644 tests/Integration/GatewayTest.php delete mode 100644 tests/Integration/TestGateway.php diff --git a/src/ForeignTransformer.php b/src/ForeignTransformer.php deleted file mode 100644 index 0619045..0000000 --- a/src/ForeignTransformer.php +++ /dev/null @@ -1,23 +0,0 @@ -fractal = App::make(Fractal::class); - } - - /** - * Returns the current status code. - * - * @return int - */ - protected function getStatusCode() - { - return $this->statusCode; - } - - /** - * Sets the current status code. - * - * @param $statusCode - * @return $this - */ - protected function setStatusCode($statusCode) - { - $this->statusCode = $statusCode; - - return $this; - } - - /** - * Returns a json response that contains the specified resource - * passed through fractal and optionally a transformer. - * - * @param $item - * @param null $callback - * @param null $resourceKey - * @return \Illuminate\Http\JsonResponse - */ - protected function respondWithItem($item, $callback = null, $resourceKey = null) - { - $rootScope = $this->fractal - ->item($item, $callback, $resourceKey); - - return $this->respondWithArray($rootScope->toArray()); - } - - /** - * Returns a json response that contains the specified collection - * passed through fractal and optionally a transformer. - * - * @param $collection - * @param $callback - * @param null $resourceKey - * @return \Illuminate\Http\JsonResponse - */ - protected function respondWithCollection($collection, $callback, $resourceKey = null) - { - $rootScope = $this->fractal->collection($collection, $callback, $resourceKey); - - return $this->respondWithArray($rootScope->toArray()); - } - - /** - * Parses the incoming json parameters into a ParameterBag object. - * - * @param $parameters - * @return ParameterBag - */ - protected function parseParameters($parameters) - { - if (!($parameters instanceof ParameterBag) && !($parameters instanceof ParamBag)) { - $parameters = new ParameterBag(json_decode($parameters, true)); - } - - if ($parameters->get('with')) { - $this->fractal->parseIncludes($parameters->get('with')); - } - - return $parameters; - } - - /** - * Returns a json response that contains the specified array, - * and the current status code. - * - * @param array $array - * @return \Illuminate\Http\JsonResponse - */ - protected function respondWithArray(array $array) - { - return json_encode(array_merge($array, ['status_code' => $this->statusCode])); - } - - /** - * Returns a response that indicates a 404 Not Found. - * - * @param string $message - * @return \Illuminate\Http\JsonResponse - */ - protected function errorNotFound($message = 'Resource Not Found') - { - return $this->setStatusCode(404)->respondWithError($message); - } - - /** - * Returns a response that indicates an an error occurred. - * - * @param $message - * @return \Illuminate\Http\JsonResponse - */ - protected function respondWithError($message) - { - return $this->respondWithArray([ - 'error' => [ - 'status_code' => $this->statusCode, - 'message' => $message, - ] - ]); - } -} \ No newline at end of file diff --git a/tests/Integration/ForeignTransformerTest.php b/tests/Integration/ForeignTransformerTest.php deleted file mode 100644 index 105a11c..0000000 --- a/tests/Integration/ForeignTransformerTest.php +++ /dev/null @@ -1,24 +0,0 @@ - $this->testBooks[0], - 'status_code' => 200 - ]; - - $transformer = new ForeignTransformer(); - $response = $transformer->transform(json_encode($data)); - - $this->assertEquals($this->testBooks[0], $response); - } -} diff --git a/tests/Integration/GatewayTest.php b/tests/Integration/GatewayTest.php deleted file mode 100644 index 3d1b1c9..0000000 --- a/tests/Integration/GatewayTest.php +++ /dev/null @@ -1,159 +0,0 @@ -gateway = new ReflectionClass(TestGateway::class); - } - - /** - * @test - */ - public function it_can_be_instantiated() - { - $gateway = new TestGateway(); - - $this->assertTrue(isset($gateway)); - } - - /** - * @test - */ - public function it_gets_status_codes() - { - $method = new ReflectionMethod( - TestGateway::class, 'getStatusCode' - ); - - $method->setAccessible(TRUE); - - $this->assertEquals( - 200, $method->invoke(new TestGateway()) - ); - } - - /** - * @test - */ - public function it_can_set_status_codes() - { - $setStatusCode = $this->getMethod('setStatusCode'); - $getStatusCode = $this->getMethod('getStatusCode'); - - $testGateway = new TestGateway(); - $setStatusCode->invokeArgs($testGateway, [400]); - - $this->assertEquals( - 400, $getStatusCode->invoke($testGateway) - ); - } - - /** - * @test - */ - public function it_can_respond_with_a_single_item() - { - $respondWithItem = $this->getMethod('respondWithItem'); - $testGateway = new TestGateway(); - - $response = $respondWithItem->invokeArgs($testGateway, [$this->testBooks[0], new TestTransformer()]); - $array = json_decode($response, true); - - $expectedArray = [ - 'data' => [ - 'id' => 1, - 'author' => 'Philip K Dick' - ], - 'status_code' => '200' - ]; - - $this->assertEquals($expectedArray, $array); - } - - /** - * @test - */ - public function it_can_respond_with_a_collection() - { - $respondWithCollection = $this->getMethod('respondWithCollection'); - $testGateway = new TestGateway(); - - $response = $respondWithCollection->invokeArgs($testGateway, [$this->testBooks, new TestTransformer()]); - $array = json_decode($response, true); - - $expectedArray = [ - 'data' => [ - ['id' => 1, 'author' => 'Philip K Dick'], - ['id' => 2, 'author' => 'George R. R. Satan'] - ], - 'status_code' => 200 - ]; - - $this->assertEquals($expectedArray, $array); - } - - /** - * @test - */ - public function it_can_parse_parameters() - { - $parseParameters = $this->getMethod('parseParameters'); - $testGateway = new TestGateway(); - - $parameters = [ - 'with' => 'characters, publisher' - ]; - - $response = $parseParameters->invokeArgs($testGateway, [json_encode($parameters)]); - - $this->assertTrue($response instanceof ParameterBag); - $this->assertEquals('characters, publisher', $response->get('with')); - } - - /** - * @test - */ - public function it_can_respond_with_a_not_found_error() - { - $errorNotFound = $this->getMethod('errorNotFound'); - $testGateway = new TestGateway(); - - $response = $errorNotFound->invoke($testGateway); - $array = json_decode($response, true); - - $expectedArray = [ - 'error' => [ - 'status_code' => 404, - 'message' => 'Resource Not Found' - ], - 'status_code' => 404 - ]; - - $this->assertEquals($expectedArray, $array); - } - - - /** - * @param $methodName - * @return mixed - */ - protected function getMethod($methodName) - { - $method = $this->gateway->getMethod($methodName); - $method->setAccessible(true); - - return $method; - } -} diff --git a/tests/Integration/TestGateway.php b/tests/Integration/TestGateway.php deleted file mode 100644 index 9f59f3f..0000000 --- a/tests/Integration/TestGateway.php +++ /dev/null @@ -1,10 +0,0 @@ - Date: Sat, 25 Jun 2016 09:35:44 -0400 Subject: [PATCH 2/4] Updating Readme. --- README.md | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 9b8963e..0339aad 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -[![Circle CI](https://circleci.com/gh/NavJobs/Transmit.svg?style=shield)](https://circleci.com/gh/NavJobs/Transmit) +[![Circle CI](https://circleci.com/gh/navjobs/transmit.svg?style=shield)](https://circleci.com/gh/navjobs/transmit) [![Coverage Status](https://coveralls.io/repos/NavJobs/Transmit/badge.svg?branch=master&service=github)](https://coveralls.io/github/NavJobs/Transmit?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/NavJobs/Transmit/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/NavJobs/Transmit/?branch=master) [![Latest Stable Version](https://poser.pugx.org/navjobs/transmit/v/stable)](https://packagist.org/packages/navjobs/transmit) [![Total Downloads](https://poser.pugx.org/navjobs/transmit/downloads)](https://packagist.org/packages/navjobs/transmit) [![License](https://poser.pugx.org/navjobs/transmit/license)](https://packagist.org/packages/navjobs/transmit) ###### Communication Layer For Laravel -Transmit was created to abstact the process of implementing external APIs and internal communications gateways across microservices. +Transmit was created to expedite the process of implementing REST APIs. #### Install @@ -252,13 +252,6 @@ Includes can also be sorted by query parameters, the URL format is: http://www.example.com/books?include=authors:sort(name|-created_at),publisher ``` -#### Gateway -Transmit provides an abstract gateway class that facilitates internal communication in a microservice architecture. - -**Gateways are still a work in progress, and not recommended to be used in production yet** - -Gateway Documentation coming soon. - ## Fractal Transmit is built on the back of two amazing PHP packages. From a9d8e59d9e5acb9560277c292fe8d10be680a597 Mon Sep 17 00:00:00 2001 From: Josh Forbes Date: Sat, 25 Jun 2016 10:00:02 -0400 Subject: [PATCH 3/4] Adding test coverage for returning an item or collection without a resource key. --- tests/Integration/ControllerTest.php | 46 ++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/tests/Integration/ControllerTest.php b/tests/Integration/ControllerTest.php index af7fa4a..b824107 100644 --- a/tests/Integration/ControllerTest.php +++ b/tests/Integration/ControllerTest.php @@ -93,6 +93,23 @@ public function it_can_respond_with_a_single_item() $this->assertEquals($expectedArray, $array); } + /** + * @test + */ + public function it_can_respond_with_a_single_item_at_the_top_level() + { + $respondWithItem = $this->getMethod('respondWithItem'); + $testController = new TestController(); + + $response = $respondWithItem->invokeArgs($testController, [$this->testBooks[0], new TestTransformer(), false]); + $array = json_decode(json_encode($response->getData()), true); + + $expectedArray = [ + 'id' => 1, 'author' => 'Philip K Dick', ]; + + $this->assertEquals($expectedArray, $array); + } + /** * @test */ @@ -122,15 +139,38 @@ public function it_can_respond_with_a_collection() $respondWithCollection = $this->getMethod('respondWithCollection'); $testController = new TestController(); + $expectedData = [ + ['id' => 1, 'author' => 'Philip K Dick'], + ['id' => 2, 'author' => 'George R. R. Satan'], + ]; + $response = $respondWithCollection->invokeArgs($testController, [$this->testBooks, new TestTransformer()]); $array = json_decode(json_encode($response->getData()), true); - $expectedArray = ['data' => [ + $this->assertEquals(['data' => $expectedData], $array); + } + + /** + * @test + */ + public function it_can_respond_with_a_collection_as_a_top_level_item() + { + $respondWithCollection = $this->getMethod('respondWithCollection'); + $testController = new TestController(); + + $expectedData = [ ['id' => 1, 'author' => 'Philip K Dick'], ['id' => 2, 'author' => 'George R. R. Satan'], - ]]; + ]; - $this->assertEquals($expectedArray, $array); + $response = $respondWithCollection->invokeArgs($testController, [ + $this->testBooks, + new TestTransformer(), + false + ]); + $array = json_decode(json_encode($response->getData()), true); + + $this->assertEquals($expectedData, $array); } /** From e54c86bceb9f90496af64c39c94276a1ab45cb6f Mon Sep 17 00:00:00 2001 From: Josh Forbes Date: Sat, 25 Jun 2016 19:50:42 -0400 Subject: [PATCH 4/4] Removing 'Gateway' reference. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0339aad..cd5051a 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ Transmit is built on the back of two amazing PHP packages. - [fractal](https://github.com/thephpleague/fractal) - [laravel-fractal](https://github.com/spatie/laravel-fractal/tree/master/src). -Controllers and Gateways have an instance of laravel-fractal available through: +Controllers have an instance of laravel-fractal available through: ```php $this->fractal;