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