Issues (50)

tests/Service/ResourcePopulatorTest.php (1 issue)

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);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->createMock(Silver...rvice\APIClient::class) of type PHPUnit_Framework_MockObject_MockObject is incompatible with the declared type SilverStripe\CKANRegistry\Service\APIClient of property $client.

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..

Loading history...
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
                    ['id' => 'field_a', 'type' => 'text'],
56
                    ['id' => 'field_bar-^captain', 'type' => 'select'],
57
                    ['id' => 'City/Town', 'type' => 'text'],
58
                    ['id' => 'City / Town', 'type' => 'text'],
59
                ],
60
            ],
61
        ]);
62
        $populator = new ResourcePopulator();
63
        $populator->setAPIClient($this->client);
64
65
        $this->assertCount(0, $this->resource->Fields(), 'Resource has no fields before population');
66
        $populator->populateFields($this->resource);
67
        $fields = $this->resource->Fields();
68
        $this->assertCount(4, $fields, 'Fields should be populated');
69
70
        // Test that positions were assigned incrementally
71
        $this->assertEquals(1, $fields[0]->Position);
72
        $this->assertEquals(2, $fields[1]->Position);
73
74
75
        // Test that the readable names were generated correctly
76
        $this->assertSame('Field a', $fields[0]->ReadableLabel);
77
        $this->assertSame('Field bar captain', $fields[1]->ReadableLabel);
78
        $this->assertSame('City/town', $fields[2]->ReadableLabel);
79
        $this->assertSame('City/town', $fields[3]->ReadableLabel);
80
    }
81
82
    public function testPopulateMetadata()
83
    {
84
        $this->client->expects($this->once())->method('getPackage')->willReturn([
85
            'result' => [
86
                'title' => 'List of Animals',
87
                'resources' => [
88
                    ['id' => '654-321', 'name' => 'Monkey'],
89
                    ['id' => '123-456', 'name' => 'Giraffe'],
90
                    ['id' => '987-342', 'name' => 'Horse'],
91
                ],
92
            ],
93
        ]);
94
        $populator = new ResourcePopulator();
95
        $populator->setAPIClient($this->client);
96
        $populator->populateMetadata($this->resource);
97
98
        $this->assertSame('List of Animals', $this->resource->Name, 'DataSet name should be populated');
99
        $this->assertSame('Giraffe', $this->resource->ResourceName, 'Selected resource name should be populated');
100
    }
101
}
102