|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\CKANRegistry\Tests\Service; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_MockObject_MockObject; |
|
6
|
|
|
use RuntimeException; |
|
7
|
|
|
use SilverStripe\CKANRegistry\Model\Resource; |
|
8
|
|
|
use SilverStripe\CKANRegistry\Service\APIClient; |
|
9
|
|
|
use SilverStripe\CKANRegistry\Service\ResourcePopulator; |
|
10
|
|
|
use SilverStripe\Dev\SapphireTest; |
|
11
|
|
|
|
|
12
|
|
|
class ResourcePopulatorTest extends SapphireTest |
|
13
|
|
|
{ |
|
14
|
|
|
protected $usesDatabase = true; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var Resource |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $resource; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var APIClient |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $client; |
|
25
|
|
|
|
|
26
|
|
|
protected function setUp() |
|
27
|
|
|
{ |
|
28
|
|
|
parent::setUp(); |
|
29
|
|
|
|
|
30
|
|
|
$this->resource = new Resource(); |
|
31
|
|
|
$this->resource->Endpoint = 'http://example.com'; |
|
32
|
|
|
$this->resource->DataSet = 'foo-bar'; |
|
33
|
|
|
$this->resource->Identifier = '123-456'; |
|
34
|
|
|
|
|
35
|
|
|
/** @var PHPUnit_Framework_MockObject_MockObject|APIClient $client */ |
|
36
|
|
|
$this->client = $this->createMock(APIClient::class); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @expectedException RuntimeException |
|
41
|
|
|
* @expectedExceptionMessage Could not fetch fields for a resource that is not fully configured |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testThrowsExceptionWhenResourceIsNotConfigured() |
|
44
|
|
|
{ |
|
45
|
|
|
$resource = new Resource(); |
|
46
|
|
|
$populator = new ResourcePopulator(); |
|
47
|
|
|
$populator->populateFields($resource); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testPopulateFields() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->client->expects($this->once())->method('getSearchData')->willReturn([ |
|
|
|
|
|
|
53
|
|
|
'result' => [ |
|
54
|
|
|
'fields' => [ |
|
55
|
|
|
[ |
|
56
|
|
|
'id' => 'field_a', |
|
57
|
|
|
'type' => 'text', |
|
58
|
|
|
], |
|
59
|
|
|
[ |
|
60
|
|
|
'id' => 'field_bar-^captain', |
|
61
|
|
|
'type' => 'select', |
|
62
|
|
|
] |
|
63
|
|
|
], |
|
64
|
|
|
], |
|
65
|
|
|
]); |
|
66
|
|
|
$populator = new ResourcePopulator(); |
|
67
|
|
|
$populator->setAPIClient($this->client); |
|
68
|
|
|
|
|
69
|
|
|
$this->assertCount(0, $this->resource->Fields(), 'Resource has no fields before population'); |
|
70
|
|
|
$populator->populateFields($this->resource); |
|
71
|
|
|
$this->assertCount(2, $this->resource->Fields(), 'Fields should be populated'); |
|
72
|
|
|
|
|
73
|
|
|
// Test that the readable names were generated correctly |
|
74
|
|
|
$this->assertSame('Field a', $this->resource->Fields()->first()->ReadableLabel); |
|
75
|
|
|
$this->assertEquals(1, $this->resource->Fields()->first()->Position); |
|
76
|
|
|
$this->assertSame('Field bar captain', $this->resource->Fields()->last()->ReadableLabel); |
|
77
|
|
|
$this->assertEquals(2, $this->resource->Fields()->last()->Position); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function testPopulateMetadata() |
|
81
|
|
|
{ |
|
82
|
|
|
$this->client->expects($this->once())->method('getPackage')->willReturn([ |
|
83
|
|
|
'result' => [ |
|
84
|
|
|
'title' => 'List of Animals', |
|
85
|
|
|
'resources' => [ |
|
86
|
|
|
['id' => '654-321', 'name' => 'Monkey'], |
|
87
|
|
|
['id' => '123-456', 'name' => 'Giraffe'], |
|
88
|
|
|
['id' => '987-342', 'name' => 'Horse'], |
|
89
|
|
|
], |
|
90
|
|
|
], |
|
91
|
|
|
]); |
|
92
|
|
|
$populator = new ResourcePopulator(); |
|
93
|
|
|
$populator->setAPIClient($this->client); |
|
94
|
|
|
$populator->populateMetadata($this->resource); |
|
95
|
|
|
|
|
96
|
|
|
$this->assertSame('List of Animals', $this->resource->Name, 'DataSet name should be populated'); |
|
97
|
|
|
$this->assertSame('Giraffe', $this->resource->ResourceName, 'Selected resource name should be populated'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..