| 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\ResourceFieldPopulator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SilverStripe\CKANRegistry\Service\ResourceFieldPopulatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class ResourceFieldPopulatorTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $usesDatabase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @expectedException RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @expectedExceptionMessage Could not fetch fields for a resource that is not fully configured | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     public function testThrowsExceptionWhenResourceIsNotConfigured() | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $resource = new Resource(); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |         $populator = new ResourceFieldPopulator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |         $populator->populateFields($resource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function testPopulate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $resource = new Resource(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $resource->Endpoint = 'http://example.com'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $resource->Identifier = '123-456'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         /** @var PHPUnit_Framework_MockObject_MockObject|ResourceFieldPopulatorInterface $populator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $populator = $this->getMockBuilder(ResourceFieldPopulator::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ->setMethods(['doFieldRequest']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $populator->expects($this->once())->method('doFieldRequest')->willReturn([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 'id' => 'field_a', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                 'type' => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 'id' => 'field_bar-captain', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |                 'type' => 'select', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $this->assertCount(0, $resource->Fields(), 'Resource has no fields before population'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $populator->populateFields($resource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertCount(2, $resource->Fields(), 'Fields should be populated'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         // Test that the readable names were generated correctly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assertSame('Field a', $resource->Fields()->first()->ReadableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertSame('Field bar captain', $resource->Fields()->last()->ReadableName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  |