Skip to content

Commit 84355a8

Browse files
committed
Close #171
1 parent 9acd609 commit 84355a8

File tree

10 files changed

+117
-37
lines changed

10 files changed

+117
-37
lines changed

composer.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
"psr/log": "^1.0"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^4.6 || ^5.0",
29+
"phpunit/phpunit": "^4.6 || ^5.0 || ^6.0",
3030
"mockery/mockery": "~0.9.4",
3131
"scrutinizer/ocular": "^1.3",
3232
"squizlabs/php_codesniffer": "^2.5",
33-
"monolog/monolog": "^1.18"
33+
"monolog/monolog": "^1.18",
34+
"phpmd/phpmd": "^2.6"
3435
},
3536
"minimum-stability": "stable",
3637
"autoload": {
@@ -45,9 +46,10 @@
4546
}
4647
},
4748
"scripts": {
48-
"test": ["@test-phpunit", "@test-phpcs"],
49-
"test-phpunit": "./vendor/phpunit/phpunit/phpunit",
50-
"test-phpcs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
49+
"test": ["@test-unit", "@test-cs", "@test-md"],
50+
"test-unit": "./vendor/phpunit/phpunit/phpunit --coverage-text",
51+
"test-cs": "./vendor/bin/phpcs -p -s --standard=PSR2 ./src ./tests",
52+
"test-md": "./vendor/bin/phpmd ./src text codesize,controversial,cleancode,design,unusedcode,naming",
5153

5254
"perf-php": "docker-compose run --rm cli_php php /app/sample/sample.php -t=10000",
5355
"perf-hhvm": "docker-compose run --rm cli_hhvm hhvm /app/sample/sample.php -t=10000"

src/Document/Document.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,13 @@ public function setResourceCompleted(ResourceObjectInterface $resource)
311311
public function getDocument()
312312
{
313313
if ($this->errors !== null) {
314-
return [self::KEYWORD_ERRORS => $this->errors];
314+
return array_filter([
315+
self::KEYWORD_JSON_API => $this->version,
316+
self::KEYWORD_META => $this->meta,
317+
self::KEYWORD_ERRORS => $this->errors,
318+
], function ($value) {
319+
return $value !== null;
320+
});
315321
}
316322

317323
$document = array_filter([

src/Encoder/Encoder.php

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -220,17 +220,9 @@ protected function encodeDataToArray(
220220
$interpreter->handle($reply);
221221
}
222222

223-
if ($this->getMeta() !== null) {
224-
$docWriter->setMetaToDocument($this->getMeta());
225-
}
226-
227-
if (empty($this->getLinks()) === false) {
228-
$docWriter->setDocumentLinks($this->getLinks());
229-
}
230-
231-
if ($this->isWithJsonApiVersion() === true) {
232-
$docWriter->addJsonApiVersion(self::JSON_API_VERSION, $this->getJsonApiVersionMeta());
233-
}
223+
$this->addTopLevelMeta($docWriter);
224+
$this->addTopLevelLinks($docWriter);
225+
$this->addTopLevelJsonApiVersion($docWriter);
234226

235227
$result = $docWriter->getDocument();
236228
$this->resetEncodeParameters();
@@ -310,6 +302,10 @@ protected function encodeErrorToArray(ErrorInterface $error)
310302
{
311303
$docWriter = $this->getFactory()->createDocument();
312304
$docWriter->addError($error);
305+
306+
$this->addTopLevelMeta($docWriter);
307+
$this->addTopLevelJsonApiVersion($docWriter);
308+
313309
$array = $docWriter->getDocument();
314310

315311
return $array;
@@ -324,6 +320,10 @@ protected function encodeErrorsToArray($errors)
324320
{
325321
$docWriter = $this->getFactory()->createDocument();
326322
$docWriter->addErrors($errors);
323+
324+
$this->addTopLevelMeta($docWriter);
325+
$this->addTopLevelJsonApiVersion($docWriter);
326+
327327
$array = $docWriter->getDocument();
328328

329329
return $array;
@@ -345,6 +345,36 @@ protected function encodeMetaToArray($meta)
345345
return $array;
346346
}
347347

348+
/**
349+
* @param DocumentInterface $docWriter
350+
*/
351+
protected function addTopLevelMeta(DocumentInterface $docWriter)
352+
{
353+
if ($this->getMeta() !== null) {
354+
$docWriter->setMetaToDocument($this->getMeta());
355+
}
356+
}
357+
358+
/**
359+
* @param DocumentInterface $docWriter
360+
*/
361+
protected function addTopLevelLinks(DocumentInterface $docWriter)
362+
{
363+
if (empty($this->getLinks()) === false) {
364+
$docWriter->setDocumentLinks($this->getLinks());
365+
}
366+
}
367+
368+
/**
369+
* @param DocumentInterface $docWriter
370+
*/
371+
protected function addTopLevelJsonApiVersion(DocumentInterface $docWriter)
372+
{
373+
if ($this->isWithJsonApiVersion() === true) {
374+
$docWriter->addJsonApiVersion(self::JSON_API_VERSION, $this->getJsonApiVersionMeta());
375+
}
376+
}
377+
348378
/**
349379
* @return ContainerInterface
350380
*/

tests/BaseTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
use \Mockery;
2020
use \Monolog\Logger;
21-
use \PHPUnit_Framework_TestCase;
21+
use \PHPUnit\Framework\TestCase;
2222
use \Monolog\Handler\StreamHandler;
2323
use \Neomerx\JsonApi\Factories\Factory;
2424
use \Neomerx\JsonApi\Encoder\EncoderOptions;
@@ -27,7 +27,7 @@
2727
/**
2828
* @package Neomerx\JsonApi
2929
*/
30-
abstract class BaseTestCase extends PHPUnit_Framework_TestCase
30+
abstract class BaseTestCase extends TestCase
3131
{
3232
/**
3333
* Tear down test.

tests/Encoder/EncodeErrorsTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,48 @@ public function testEncodeEmptyErrorArray()
163163
$this->assertEquals($expected, $actual);
164164
}
165165

166+
/**
167+
* Test encode error.
168+
*
169+
* @see https://github.com/neomerx/json-api/issues/171
170+
*/
171+
public function testEncodeErrorWithMetaAndJsonApi()
172+
{
173+
$error = $this->getError();
174+
$encoder = Encoder::instance();
175+
176+
$actual = $encoder
177+
->withJsonApiVersion(['some' => 'meta'])
178+
->withMeta(["copyright" => "Copyright 2015 Example Corp."])
179+
->encodeError($error);
180+
181+
$expected = <<<EOL
182+
{
183+
"jsonapi" : {
184+
"version" : "1.0",
185+
"meta" : { "some" : "meta" }
186+
},
187+
"meta" : {
188+
"copyright" : "Copyright 2015 Example Corp."
189+
},
190+
"errors":[{
191+
"id" : "some-id",
192+
"links" : {"about" : "about-link"},
193+
"status" : "some-status",
194+
"code" : "some-code",
195+
"title" : "some-title",
196+
"detail" : "some-detail",
197+
"source" : {"source" : "data"},
198+
"meta" : {"some" : "meta"}
199+
}]
200+
}
201+
EOL;
202+
// remove formatting from 'expected'
203+
$expected = json_encode(json_decode($expected));
204+
205+
$this->assertEquals($expected, $actual);
206+
}
207+
166208
/**
167209
* @return Error
168210
*/

tests/Factories/FactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testSetLogger()
3333
{
3434
$factory = new Factory();
3535

36-
$logMock = Mockery::mock(LoggerInterface::class);
36+
$this->assertNotNull($logMock = Mockery::mock(LoggerInterface::class));
3737

3838
/** @var LoggerInterface $logMock */
3939

tests/Factories/ProxyLoggerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testSetLogger()
3333
{
3434
$logger = new ProxyLogger();
3535

36-
$logMock = Mockery::mock(LoggerInterface::class);
36+
$this->assertNotNull($logMock = Mockery::mock(LoggerInterface::class));
3737

3838
$logger->debug('Nothing hapens. Should not fail.');
3939

tests/Http/Headers/RestrictiveHeadersCheckerTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function setUp()
6868
*/
6969
public function testDefaultNotReallyRestrictiveSettings()
7070
{
71-
$checker = $this->getCheckerWithExtensions();
71+
$this->assertNotNull($checker = $this->getCheckerWithExtensions());
7272

7373
$parameters = $this->parser->parse(
7474
$this->prepareRequest('POST', self::JSON_API_TYPE, self::JSON_API_TYPE)
@@ -82,7 +82,7 @@ public function testDefaultNotReallyRestrictiveSettings()
8282
*/
8383
public function testAllowedExtensions()
8484
{
85-
$checker = $this->getCheckerWithExtensions();
85+
$this->assertNotNull($checker = $this->getCheckerWithExtensions());
8686

8787
$parameters = $this->parser->parse($this->prepareRequest(
8888
'POST',

tests/Http/Query/RestrictiveQueryCheckerTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function setUp()
6868
*/
6969
public function testDefaultNotReallyRestrictiveSettings()
7070
{
71-
$checker = $this->getChecker();
71+
$this->assertNotNull($checker = $this->getChecker());
7272

7373
$parameters = $this->parser->parse(
7474
$this->prepareRequest($this->requestParams)
@@ -82,10 +82,10 @@ public function testDefaultNotReallyRestrictiveSettings()
8282
*/
8383
public function testAllowedInputPaths()
8484
{
85-
$checker = $this->getChecker(
85+
$this->assertNotNull($checker = $this->getChecker(
8686
false,
8787
['author', 'comments', 'comments.author', 'and.one.more.path']
88-
);
88+
));
8989

9090
$parameters = $this->parser->parse(
9191
$this->prepareRequest($this->requestParams)
@@ -123,11 +123,11 @@ public function testNotAllowedInputPaths()
123123
*/
124124
public function testAllowedFieldSets()
125125
{
126-
$checker = $this->getChecker(
126+
$this->assertNotNull($checker = $this->getChecker(
127127
false,
128128
null,
129129
['type1' => ['fields1', 'fields2', 'fields3'],]
130-
);
130+
));
131131

132132
$parameters = $this->parser->parse(
133133
$this->prepareRequest($this->requestParams)
@@ -141,11 +141,11 @@ public function testAllowedFieldSets()
141141
*/
142142
public function testAllowedAllFieldSets()
143143
{
144-
$checker = $this->getChecker(
144+
$this->assertNotNull($checker = $this->getChecker(
145145
false,
146146
null,
147147
['type1' => null] // all fields are allowed for type1
148-
);
148+
));
149149

150150
$parameters = $this->parser->parse(
151151
$this->prepareRequest($this->requestParams)
@@ -267,12 +267,12 @@ public function testNotAllowedFieldSets()
267267
public function testAllowedSearchParams()
268268
{
269269
$allowedSortParams = ['created', 'title', 'name.with.dots', 'and-others'];
270-
$checker = $this->getChecker(
270+
$this->assertNotNull($checker = $this->getChecker(
271271
false,
272272
null,
273273
null,
274274
$allowedSortParams
275-
);
275+
));
276276

277277
$parameters = $this->parser->parse(
278278
$this->prepareRequest($this->requestParams)
@@ -314,9 +314,9 @@ public function testNotAllowedSortParams()
314314
*/
315315
public function testAllowedUnrecognizedParameters()
316316
{
317-
$checker = $this->getChecker(
317+
$this->assertNotNull($checker = $this->getChecker(
318318
true
319-
);
319+
));
320320

321321
$parameters = $this->parser->parse(
322322
$this->prepareRequest(

tests/Schema/ResourceIdentifierContainerAdapterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ public function testAdapter()
4949
$adapter = new ResourceIdentifierContainerAdapter($factory, $container);
5050

5151
$resource = (object)['whatever'];
52-
$adapter->getSchema($resource);
53-
$adapter->getSchemaByType($resource);
54-
$adapter->getSchemaByResourceType('does not matter');
52+
$this->assertNotNull($adapter->getSchema($resource));
53+
$this->assertNotNull($adapter->getSchemaByType($resource));
54+
$this->assertNotNull($adapter->getSchemaByResourceType('does not matter'));
5555
}
5656
}

0 commit comments

Comments
 (0)