Completed
Push — master ( 037cc5...776a32 )
by Guillermo A.
09:42
created

RepositoryTest::testUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace GuillermoandraeTest\Highrise\Repositories;
4
5
use Guillermoandrae\Highrise\Http\AdapterInterface;
6
use Guillermoandrae\Highrise\Repositories\AbstractRepository;
7
use Guillermoandrae\Highrise\Repositories\RepositoryInterface;
8
use GuillermoandraeTest\Highrise\TestCase;
9
10
class RepositoryTest extends TestCase
11
{
12
    public function testFind()
13
    {
14
        $id = '12345';
15
        $expectedBody = sprintf('<test><id>%s</id></test>', $id);
16
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
17
        $resource = $this->getResource($adapter);
18
        $item = $resource->find($id);
19
        $this->assertEquals($id, $item['id']);
20
    }
21
22
    public function testFindAll()
23
    {
24
        $expectedBody = '<tests type="array"><test><id>1</id></test><test><id>2</id></test></tests>';
25
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
26
        $resource = $this->getResource($adapter);
27
        $this->assertCount(2, $resource->findAll());
28
    }
29
30
    public function testSearchWithTerm()
31
    {
32
        $expectedBody = '<tests type="array"><test><id>1</id></test><test><id>2</id></test></tests>';
33
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
34
        $resource = $this->getResource($adapter);
35
        $resource->search(['term' => 'test']);
0 ignored issues
show
Bug introduced by
The method search() does not exist on Guillermoandrae\Highrise...ies\RepositoryInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Guillermoandrae\Highrise...ies\RepositoryInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
        $resource->/** @scrutinizer ignore-call */ 
36
                   search(['term' => 'test']);
Loading history...
36
        $actualQuery = $resource->getAdapter()->getLastRequest()->getUri()->getQuery();
37
        $this->assertSame(http_build_query(['term' => 'test']), $actualQuery);
38
    }
39
40
    public function testSearchWithCriteria()
41
    {
42
        $expectedBody = '<tests type="array"><test><id>1</id></test><test><id>2</id></test></tests>';
43
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
44
        $resource = $this->getResource($adapter);
45
        $resource->search(['test' => 'test']);
46
        $actualQuery = $resource->getAdapter()->getLastRequest()->getUri()->getQuery();
47
        $this->assertSame(http_build_query(['criteria[test]' => 'test']), $actualQuery);
48
    }
49
50
    public function testCreate()
51
    {
52
        $expectedBody = '<test><name>test</name></test>';
53
        $adapter = $this->getAdapter($this->getMockClient(201, [], $expectedBody));
54
        $resource = $this->getResource($adapter);
55
        $results = $resource->create(['name' => 'test']);
56
        $this->assertArrayHasKey('name', $results);
0 ignored issues
show
Bug introduced by
$results of type Guillermoandrae\Models\ModelInterface is incompatible with the type ArrayAccess|array expected by parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

56
        $this->assertArrayHasKey('name', /** @scrutinizer ignore-type */ $results);
Loading history...
57
        $this->assertSame($expectedBody, (string) $resource->getAdapter()->getLastRequest()->getBody());
58
    }
59
60
    public function testUpdate()
61
    {
62
        $expectedBody = '<test><name>test</name></test>';
63
        $adapter = $this->getAdapter($this->getMockClient(201, [], $expectedBody));
64
        $resource = $this->getResource($adapter);
65
        $results = $resource->update('123456', ['name' => 'test']);
66
        $this->assertArrayHasKey('name', $results);
0 ignored issues
show
Bug introduced by
$results of type Guillermoandrae\Models\ModelInterface is incompatible with the type ArrayAccess|array expected by parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        $this->assertArrayHasKey('name', /** @scrutinizer ignore-type */ $results);
Loading history...
67
        $this->assertSame($expectedBody, (string) $resource->getAdapter()->getLastRequest()->getBody());
68
    }
69
70
    public function testDelete()
71
    {
72
        $adapter = $this->getAdapter($this->getMockClient(200));
73
        $resource = $this->getResource($adapter);
74
        $this->assertTrue($resource->delete('123456'));
75
    }
76
77
    private function getResource(AdapterInterface $adapter): RepositoryInterface
78
    {
79
        return $this->getMockForAbstractClass(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockFor...ray($adapter), 'Tests') returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Guillermoandrae\Highrise...ies\RepositoryInterface.
Loading history...
80
            AbstractRepository::class,
81
            [$adapter],
82
            'Tests'
83
        );
84
    }
85
}
86