Skip to content

Commit 11ffb49

Browse files
committed
Merge branch 'release/1.1.2'
2 parents 10c3761 + e80305f commit 11ffb49

File tree

7 files changed

+127
-14
lines changed

7 files changed

+127
-14
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ jobs:
1414
strategy:
1515
fail-fast: true
1616
matrix:
17-
php: [7.4, '8.0', 8.1]
17+
php: [7.4, '8.0', 8.1, 8.2]
1818
laravel: [8, 9]
1919
exclude:
2020
- php: 7.4
2121
laravel: 9
2222

2323
steps:
2424
- name: Checkout Code
25-
uses: actions/checkout@v2
25+
uses: actions/checkout@v3
2626

2727
- name: Setup PHP
2828
uses: shivammathur/setup-php@v2
@@ -37,7 +37,7 @@ jobs:
3737
run: composer require "illuminate/testing:^${{ matrix.laravel }}" --no-update
3838

3939
- name: Install dependencies
40-
uses: nick-invision/retry@v1
40+
uses: nick-fields/retry@v2
4141
with:
4242
timeout_minutes: 5
4343
max_attempts: 5

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## [1.1.2] - 2023-01-15
7+
8+
### Fixed
9+
10+
- The `TestBuilder::withData()` method no longer converts the provided data to a `Collection` if it implements
11+
the `JsonSerializable` interface. This change was required because Laravel changed how they converted objects to
12+
collections. It is not strictly necessary to convert objects that implement the JSON serializable interface, because
13+
they will be converted when the test JSON value is JSON encoded. Technically this is a breaking fix if
14+
the `TestBuilder` class has been extended. However, this change is being done as a bug fix because the bug was caused
15+
by a minor release of Laravel.
16+
617
## [1.1.1] - 2022-02-27
718

819
### Fixed

src/MakesJsonApiRequests.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* Copyright 2022 Cloud Creativity Limited
3+
* Copyright 2023 Cloud Creativity Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

src/TestBuilder.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* Copyright 2022 Cloud Creativity Limited
3+
* Copyright 2023 Cloud Creativity Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -21,12 +21,12 @@
2121
use Illuminate\Support\Arr;
2222
use Illuminate\Support\Collection;
2323
use Illuminate\Support\Str;
24+
use JsonSerializable;
2425
use PHPUnit\Framework\Assert;
2526
use PHPUnit\Framework\TestCase;
2627
use function array_walk_recursive;
2728
use function implode;
2829
use function is_bool;
29-
use function is_null;
3030
use function is_scalar;
3131

3232
final class TestBuilder
@@ -236,16 +236,20 @@ public function page(iterable $page): self
236236
/**
237237
* Set the data member of the request JSON API document.
238238
*
239-
* @param iterable|null $data
239+
* @param JsonSerializable|iterable|null $data
240240
* @return $this
241241
*/
242-
public function withData(?iterable $data): self
242+
public function withData($data): self
243243
{
244-
if (is_null($data)) {
244+
if ($data === null) {
245245
return $this->withJson(['data' => null]);
246246
}
247247

248-
return $this->withJson(['data' => Collection::make($data)]);
248+
if (is_object($data) && !$data instanceof JsonSerializable) {
249+
$data = Collection::make($data);
250+
}
251+
252+
return $this->withJson(['data' => $data]);
249253
}
250254

251255
/**

src/TestExceptionHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* Copyright 2022 Cloud Creativity Limited
3+
* Copyright 2023 Cloud Creativity Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

src/TestResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* Copyright 2022 Cloud Creativity Limited
3+
* Copyright 2023 Cloud Creativity Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

tests/Unit/TestBuilderTest.php

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/*
3-
* Copyright 2022 Cloud Creativity Limited
3+
* Copyright 2023 Cloud Creativity Limited
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -31,7 +31,8 @@
3131
use PHPUnit\Framework\MockObject\MockObject;
3232
use PHPUnit\Framework\TestCase;
3333

34-
class MockTestCase extends TestCase {
34+
class MockTestCase extends TestCase
35+
{
3536
use MakesHttpRequests;
3637
}
3738

@@ -201,6 +202,103 @@ public function testDelete(): void
201202
$this->assertEquals(new TestResponse($this->response, 'posts'), $response);
202203
}
203204

205+
/**
206+
* Test using data that is JSON serializable.
207+
*
208+
* When the data is JSON serializable, we just pass it through as we expect it to be
209+
* JSON encoded by the test request.
210+
*
211+
* @return void
212+
*/
213+
public function testWithJsonSerializableData(): void
214+
{
215+
$headers = [
216+
'X-Foo' => 'Bar',
217+
'Accept' => 'application/vnd.api+json',
218+
'CONTENT_TYPE' => 'application/vnd.api+json',
219+
];
220+
221+
$mock = $this->createMock(\JsonSerializable::class);
222+
$mock->method('jsonSerialize')->willReturn([
223+
'type' => 'posts',
224+
'attributes' => [
225+
'content' => '...',
226+
'slug' => 'hello-world',
227+
'title' => 'Hello World!',
228+
],
229+
]);
230+
231+
$this->mock
232+
->expects($this->once())
233+
->method('json')
234+
->with('POST', '/api/v1/posts', ['data' => $mock], $headers)
235+
->willReturn(new IlluminateTestResponse($this->response));
236+
237+
$response = $this->builder
238+
->expects('posts')
239+
->withData($mock)
240+
->post('/api/v1/posts', ['X-Foo' => 'Bar']);
241+
242+
$this->assertEquals(new TestResponse($this->response, 'posts'), $response);
243+
}
244+
245+
public function testWithIterableThatIsNotJsonSerializable(): void
246+
{
247+
$headers = [
248+
'X-Foo' => 'Bar',
249+
'Accept' => 'application/vnd.api+json',
250+
'CONTENT_TYPE' => 'application/vnd.api+json',
251+
];
252+
253+
$object = new class implements \IteratorAggregate {
254+
public function getIterator(): \Generator
255+
{
256+
yield from [
257+
['type' => 'posts', 'id' => '1'],
258+
];
259+
}
260+
};
261+
262+
$expected = Collection::make($object)->toArray();
263+
264+
$this->mock
265+
->expects($this->once())
266+
->method('json')
267+
->with('POST', '/api/v1/posts', ['data' => $expected], $headers)
268+
->willReturn(new IlluminateTestResponse($this->response));
269+
270+
$response = $this->builder
271+
->expects('posts')
272+
->withData($object)
273+
->post('/api/v1/posts', ['X-Foo' => 'Bar']);
274+
275+
$this->assertEquals(new TestResponse($this->response, 'posts'), $response);
276+
}
277+
278+
public function testWithNullData(): void
279+
{
280+
$headers = [
281+
'X-Foo' => 'Bar',
282+
'Accept' => 'application/vnd.api+json',
283+
'CONTENT_TYPE' => 'application/vnd.api+json',
284+
];
285+
286+
$data = null;
287+
288+
$this->mock
289+
->expects($this->once())
290+
->method('json')
291+
->with('POST', '/api/v1/posts', compact('data'), $headers)
292+
->willReturn(new IlluminateTestResponse($this->response));
293+
294+
$response = $this->builder
295+
->expects('posts')
296+
->withData($data)
297+
->post('/api/v1/posts', ['X-Foo' => 'Bar']);
298+
299+
$this->assertEquals(new TestResponse($this->response, 'posts'), $response);
300+
}
301+
204302
public function testWithHeader(): void
205303
{
206304
$headers = [

0 commit comments

Comments
 (0)