RepositoryTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 71
rs 10
wmc 8

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testFind() 0 8 1
A testCreate() 0 7 1
A testSearchWithCriteria() 0 8 1
A getResource() 0 6 1
A testDelete() 0 5 1
A testSearchWithTerm() 0 8 1
A testUpdate() 0 7 1
A testFindAll() 0 6 1
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\Mocks\TestModel;
9
use GuillermoandraeTest\Highrise\TestCase;
10
11
class RepositoryTest extends TestCase
12
{
13
    public function testFind()
14
    {
15
        $id = '12345';
16
        $expectedBody = $this->getMockModelXml('test');
17
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
18
        $repository = $this->getResource($adapter);
19
        $item = $repository->find($id);
20
        $this->assertEquals($id, $item->getId());
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Guillermoandrae\Models\ModelInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Guillermoandrae\Models\AbstractModel. Are you sure you never get one of those? ( Ignorable by Annotation )

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

20
        $this->assertEquals($id, $item->/** @scrutinizer ignore-call */ getId());
Loading history...
21
    }
22
23
    public function testFindAll()
24
    {
25
        $expectedBody = $this->getMockModelsXml('test');
26
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
27
        $repository = $this->getResource($adapter);
28
        $this->assertCount(2, $repository->findAll());
29
    }
30
31
    public function testSearchWithTerm()
32
    {
33
        $expectedBody = $this->getMockModelsXml('test');
34
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
35
        $repository = $this->getResource($adapter);
36
        $repository->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

36
        $repository->/** @scrutinizer ignore-call */ 
37
                     search(['term' => 'test']);
Loading history...
37
        $actualQuery = $repository->getAdapter()->getLastRequest()->getUri()->getQuery();
38
        $this->assertSame(http_build_query(['term' => 'test']), $actualQuery);
39
    }
40
41
    public function testSearchWithCriteria()
42
    {
43
        $expectedBody = $this->getMockModelsXml('test');
44
        $adapter = $this->getAdapter($this->getMockClient(200, [], $expectedBody));
45
        $repository = $this->getResource($adapter);
46
        $repository->search(['test' => 'test']);
47
        $actualQuery = $repository->getAdapter()->getLastRequest()->getUri()->getQuery();
48
        $this->assertSame(http_build_query(['criteria[test]' => 'test']), $actualQuery);
49
    }
50
51
    public function testCreate()
52
    {
53
        $expectedBody = '<test><name>test</name></test>';
54
        $adapter = $this->getAdapter($this->getMockClient(201, [], $expectedBody));
55
        $repository = $this->getResource($adapter);
56
        $results = $repository->create(['name' => 'test']);
0 ignored issues
show
Unused Code introduced by
The assignment to $results is dead and can be removed.
Loading history...
57
        $this->assertSame($expectedBody, (string) $repository->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
        $repository = $this->getResource($adapter);
65
        $results = $repository->update('123456', ['name' => 'test']);
0 ignored issues
show
Unused Code introduced by
The assignment to $results is dead and can be removed.
Loading history...
66
        $this->assertSame($expectedBody, (string) $repository->getAdapter()->getLastRequest()->getBody());
67
    }
68
69
    public function testDelete()
70
    {
71
        $adapter = $this->getAdapter($this->getMockClient(200));
72
        $repository = $this->getResource($adapter);
73
        $this->assertTrue($repository->delete('123456'));
74
    }
75
76
    private function getResource(AdapterInterface $adapter): RepositoryInterface
77
    {
78
        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...
79
            AbstractRepository::class,
80
            [$adapter],
81
            'Tests'
82
        );
83
    }
84
}
85