8000 [Refactor] Add further test helper refactoring · tooshay/laravel-json-api@1c8f696 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1c8f696

Browse files
committed
[Refactor] Add further test helper refactoring
Moved a number of the test helpers into the `json-api-testing` package.
1 parent 03277bd commit 1c8f696

File tree

3 files changed

+34
-187
lines changed

3 files changed

+34
-187
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@ All notable changes to this project will be documented in this file. This projec
88
- Client supplied ids will now be hydrated into Eloquent models, configurable via the `$clientId` property
99
on the Eloquent hydrator.
1010

11+
### Removed
12+
- The following deprecated methods were removed from the `TestResponse` helper:
13+
- `seeStatusCode()`: use `assertStatus()`
14+
- `seeDataCollection()`: use `assertDataCollection()`
15+
- `seeDataResource()`: use `assertDataResource()`
16+
- `seeDataResourceIdentifier()`: use `assertDataResourceIdentifier()`
17+
- `seeDocument()`: use `assertDocument()`
18+
- `seeErrors()`: use `assertErrors()`
19+
20+
### Deprecated
21+
- The `TestResponse::assertStatusCode()` method is deprecated in favour of `assertStatus()`.
22+
- The `InteractsWithModels::seeModelInDatabase()` method is deprecated in favour of `assertDatabaseHasModel()`.
23+
- The `InteractsWithModels::notSeeModelInDatabase()` method is deprecated in favour of `assertDatabaseMissingModel()`.
24+
1125
## [0.10.3] - 2017-09-02
1226

1327
### Fixed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"require": {
2525
"php": "^5.6|^7.0",
2626
"cloudcreativity/json-api": "^0.11",
27-
"cloudcreativity/json-api-testing": "^0.1.0",
2827
"illuminate/console": "5.4.*",
2928
"illuminate/contracts": "5.4.*",
3029
"illuminate/database": "5.4.*",
@@ -37,10 +36,14 @@
3736
},
3837
"require-dev": {
3938
"ext-sqlite3": "*",
39+
"cloudcreativity/json-api-testing": "^0.1.1",
4040
"illuminate/routing": "5.4.*",
4141
"orchestra/testbench": "~3.0",
4242
"phpunit/phpunit": "^5.7"
4343
},
44+
"suggest": {
45+
"cloudcreativity/json-api-testing": "Required to use the test helpers."
46+
},
4447
"autoload": {
4548
"psr-4": {
4649
"CloudCreativity\\LaravelJsonApi\\": "src/"

src/Testing/TestResponse.php

Lines changed: 16 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Illuminate\Support\Collection;
1313
use Neomerx\JsonApi\Contracts\Document\DocumentInterface as Keys;
1414
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
15-
use PHPUnit_Framework_Assert as PHPUnit;
15+
use RuntimeException;
1616

1717
class TestResponse extends BaseTestResponse
1818
{
@@ -47,7 +47,7 @@ public function assertJsonApiResponse(
4747
$statusCode = Response::HTTP_OK,
4848
$contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE
4949
) {
50-
$this->assertStatusCode($statusCode);
50+
$this->assertStatus($statusCode);
5151

5252
if ($contentType) {
5353
$this->assertHeader('Content-Type', $contentType);
@@ -213,7 +213,7 @@ public function assertDeleteResponse(
213213
$contentType = MediaTypeInterface::JSON_API_MEDIA_TYPE
214214
) {
215215
if (Response::HTTP_NO_CONTENT == $statusCode) {
216-
$this->assertStatusCode(Response::HTTP_NO_CONTENT);
216+
$this->assertStatus(Response::HTTP_NO_CONTENT);
217217
} else {
218218
$this->assertJsonApiResponse($statusCode, $contentType);
219219
}
@@ -262,86 +262,37 @@ public function assertHasOneRelationshipResponse(
262262
*
263263
* @param $expected
264264
* @return $this
265+
* @deprecated use `assertStatus`
265266
*/
266267
public function assertStatusCode($expected)
267268
{
268-
$actual = $this->baseResponse->getStatusCode();
269-
$message = "Expected status code {$expected}, got {$actual}";
270-
$content = (array) json_decode((string) $this->baseResponse->getContent(), true);
271-
272-
if (isset($content[Keys::KEYWORD_ERRORS])) {
273-
$message .= " with errors:\n" . json_encode($content, JSON_PRETTY_PRINT);
274-
}
275-
276-
PHPUnit::assertEquals($expected, $actual, $message);
269+
$this->assertStatus($expected);
277270

278271
return $this;
279272
}
280273

281-
/**
282-
* @param $expected
283-
* @return TestResponse
284-
* @deprecated use `assertStatusCode`
285-
*/
286-
public function seeStatusCode($expected)
287-
{
288-
return $this->assertStatusCode($expected);
289-
}
290-
291274
/**
292275
* See that there is a collection of resources as primary data.
293276
*
294277
* @param string|string[] $resourceType
295278
* @param bool $allowEmpty
296279
* @return $this
297280
*/
298-
public function assertDataCollection($resourceType, $allowEmpty = true)
281+
public function assertDataCollection($resourceType = null, $allowEmpty = true)
299282
{
300-
$this->assertJsonStructure([
301-
Keys::KEYWORD_DATA,
302-
]);
303-
304-
$collection = $this->decodeResponseJson()[Keys::KEYWORD_DATA];
283+
$resources = $this->assertDocument()->assertResourceCollection();
305284

306285
if (!$allowEmpty) {
307-
PHPUnit::assertNotEmpty($collection, 'Data collection is empty');
308-
} elseif (empty($collection)) {
309-
return $this;
286+
$resources->assertNotEmpty();
310287
}
311288

312-
$expected = array_combine((array) $resourceType, (array) $resourceType);
313-
$actual = [];
314-
315-
/** @var array $resource */
316-
foreach ($collection as $resource) {
317-
318-
if (!isset($resource[Keys::KEYWORD_TYPE])) {
319-
PHPUnit::fail('Encountered a resource without a type key.');
320-
}
321-
322-
$type = $resource[Keys::KEYWORD_TYPE];
323-
324-
if (!isset($actual[$type])) {
325-
$actual[$type] = $type;
326-
}
289+
if (!$resources->isEmpty()) {
290+
$resources->assertTypes($resourceType ?: $this->expectedResourceType());
327291
}
328292

329-
PHPUnit::assertEquals($expected, $actual, 'Unexpected resource types in data collection.');
330-
331293
return $this;
332294
}
333295

334-
/**
335-
* @param $resourceType
336-
* @param bool $allowEmpty
337-
* @return TestResponse
338-
* @deprecated use `assertDataCollection`
339-
*/
340-
public function seeDataCollection($resourceType, $allowEmpty = true)
341-
{
342-
return $this->assertDataCollection($resourceType, $allowEmpty);
343-
}
344-
345296
/**
346297
* See that there is a resource object as primary data.
347298
*
@@ -356,70 +307,11 @@ public function assertDataResource(array $expected)
356307
$expected[Keys::KEYWORD_TYPE] = $this->expectedResourceType();
357308
}
358309

359-
$attributes = isset($expected[Keys::KEYWORD_ATTRIBUTES]) ?
360-
$expected[Keys::KEYWORD_ATTRIBUTES] : [];
361-
362-
$relationships = isset($expected[Keys::KEYWORD_RELATIONSHIPS]) ?
363-
$this->normalizeResourceRelationships($expected[Keys::KEYWORD_RELATIONSHIPS]) : [];
364-
365-
/** Check the structure is as expected. */
366-
$structure = [
367-
Keys::KEYWORD_TYPE,
368-
Keys::KEYWORD_ID,
369-
];
370-
371-
if (!empty($attributes)) {
372-
$structure[Keys::KEYWORD_ATTRIBUTES] = array_keys($attributes);
373-
}
374-
375-
if (!empty($relationships)) {
376-
$structure[Keys::KEYWORD_RELATIONSHIPS] = array_keys($relationships);
377-
}
378-
379-
$this->assertJsonStructure([
380-
Keys::KEYWORD_DATA => $structure,
381-
]);
382-
383-
$data = $this->decodeResponseJson()[Keys::KEYWORD_DATA];
384-
385-
/** Have we got the correct resource type? */
386-
PHPUnit::assertEquals($expected[Keys::KEYWORD_TYPE], $data[Keys::KEYWORD_TYPE], 'Unexpected resource type');
387-
388-
/** Have we got the correct resource id? */
389-
if (isset($expected[Keys::KEYWORD_ID])) {
390-
PHPUnit::assertEquals($expected[Keys::KEYWORD_ID], $data[Keys::KEYWORD_ID], 'Unexpected resource id');
391-
}
392-
393-
/** Have we got the correct attributes? */
394-
PHPUnit::assertArraySubset(
395-
$attributes,
396-
$data[Keys::KEYWORD_ATTRIBUTES],
397-
false,
398-
"Unexpected resource attributes\n" . json_encode($data[Keys::KEYWORD_ATTRIBUTES])
399-
);
400-
401-
/** Have we got the correct relationships? */
402-
$actualRelationships = isset($data[Keys::KEYWORD_RELATIONSHIPS]) ? $data[Keys::KEYWORD_RELATIONSHIPS] : [];
403-
PHPUnit::assertArraySubset(
404-
$relationships,
405-
$actualRelationships,
406-
false,
407-
"Unexpected resource relationships\n" . json_encode($actualRelationships)
408-
);
310+
$this->assertDocument()->assertResource()->assertMatches($expected);
409311

410312
return $this;
411313
}
412314

413-
/**
414-
* @param array $expected
415-
* @return TestResponse
416-
* @deprecated use `assertDataResource`
417-
*/
418-
public function seeDataResource(array $expected)
419-
{
420-
return $this->assertDataResource($expected);
421-
}
422-
423315
/**
424316
* @param string|string[]|null $resourceType
425317
* if null, will use the expected resource type.
@@ -429,41 +321,17 @@ public function seeDataResource(array $expected)
429321
*/
430322
public function assertDataResourceIdentifier($resourceType = null, $id = null)
431323
{
432-
if (is_null($resourceType)) {
433-
$this->expectedResourceType();
434-
}
435-
436-
$this->assertJsonStructure([
437-
Keys::KEYWORD_DATA,
438-
]);
439-
440-
$data = (array) $this->decodeResponseJson()[Keys::KEYWORD_DATA];
324+
$document = $this->assertDocument();
441325

442326
if (is_null($id)) {
443-
PHPUnit::assertNull($data, 'Expecting data to be null (no identifier present).');
444-
return $this;
327+
$document->assertDataNull();
328+
} else {
329+
$document->assertResourceIdentifier()->assertIs($resourceType ?: $this->expectedResourceType(), $id);
445330
}
446331

447-
$actualType = isset($data[Keys::KEYWORD_TYPE]) ? $data[Keys::KEYWORD_TYPE] : null;
448-
$actualId = isset($data[Keys::KEYWORD_ID]) ? $data[Keys::KEYWORD_ID] : null;
449-
450-
PHPUnit::assertContains($actualType, (array) $resourceType, 'Unexpected resource type in identifier.');
451-
PHPUnit::assertEquals($id, $actualId, 'Unexpected resource id.');
452-
453332
return $this;
454333
}
455334

456-
/**
457-
* @param string|null $resourceType
458-
* @param string|null $id
459-
* @return $this
460-
* @deprecated use `assertDataResourceIdentifier`
461-
*/
462-
public function seeDataResourceIdentifier($resourceType = null, $id = null)
463-
{
464-
return $this->assertDataResourceIdentifier($resourceType, $id);
465-
}
466-
467335
/**
468336
* Assert that the response to a search by id(s) request contains the expected ids.
469337
*
@@ -492,15 +360,6 @@ public function assertDocument()
492360
return DocumentTester::create($this->baseResponse->getContent());
493361
}
494362

495-
/**
496-
* @return DocumentTester
497-
* @deprecated use `assertDocument`
498-
*/
499-
public function seeDocument()
500-
{
501-
return $this->assertDocument();
502-
}
503-
504363
/**
505364
* @return ErrorsTester
506365
*/
@@ -509,22 +368,13 @@ public function assertErrors()
509368
return $this->assertDocument()->assertErrors();
510369
}
511370

512-
/**
513-
* @return ErrorsTester
514-
* @deprecated use `assertErrors`
515-
*/
516-
public function seeErrors()
517-
{
518-
return $this->assertErrors();
519-
}
520-
521371
/**
522372
* @return string
523373
*/
524374
protected function expectedResourceType()
525375
{
526376
if (!$this->expectedResourceType) {
527-
PHPUnit::fail('You must have provided the expected resource type to the test resposne helper.');
377+
throw new RuntimeException('No expected resource type set on the test response helper.');
528378
}
529379

530380
return $this->expectedResourceType;
@@ -547,24 +397,4 @@ protected function normalizeIds($ids)
547397
})->all();
548398
}
549399

550-
/**
551-
* @param array $relationships
552-
* @return array
553-
*/
554-
private function normalizeResourceRelationships(array $relationships)
555-
{
556-
$normalized = [];
557-
558-
foreach ($relationships as $key => $value) {
559-
560-
if (is_numeric($key)) {
561-
$key = $value;
562-
$value = [];
563-
}
564-
565-
$normalized[$key] = $value;
566-
}
567-
568-
return $normalized;
569-
}
570400
}

0 commit comments

Comments
 (0)
0