|
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()); |
|
|
|
|
|
|
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']); |
|
|
|
|
|
|
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']); |
|
|
|
|
|
|
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']); |
|
|
|
|
|
|
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( |
|
|
|
|
|
|
79
|
|
|
AbstractRepository::class, |
|
80
|
|
|
[$adapter], |
|
81
|
|
|
'Tests' |
|
82
|
|
|
); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|