Passed
Push — master ( fae453...efb6a4 )
by Vincent
02:44
created

AssertIncluded   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 10
1
<?php
2
3
namespace VGirol\JsonApiAssert\Laravel\Asserts;
4
5
use DMS\PHPUnitExtensions\ArraySubset\Assert as AssertArray;
6
use Illuminate\Foundation\Testing\TestResponse;
7
use PHPUnit\Framework\Assert as PHPUnit;
8
9
trait AssertIncluded
10
{
11
    public static function assertIncludedObjectContains(TestResponse $response, $expectedCollection, $expectedResourceType)
12
    {
13
        // Decode JSON response
14
        $json = $response->json();
15
16
        static::assertHasIncluded($json);
17
        $included = $json['included'];
18
19
        foreach ($expectedCollection as $expectedModel) {
20
            $resCollection = collect($included)->filter(function ($item) use ($expectedModel, $expectedResourceType) {
21
                return ($item['id'] == $expectedModel->getKey()) && ($item['type'] == $expectedResourceType);
22
            });
23
24
            PHPUnit::assertEquals(1, $resCollection->count());
25
            static::assertResourceObjectEquals($expectedModel, $expectedResourceType, $resCollection->first());
26
        }
27
    }
28
}
29