| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @package silverstripe-jsontext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @subpackage fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @author Russell Michell <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use JSONText\Fields; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use JSONText\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class JSONTextIntegrationTest extends SapphireTest | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected static $fixture_file = '/tests/fixtures/yml/MyAwesomeJSONModel.yml'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     public function __construct() | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |         self::$fixture_file = MODULE_DIR . '/tests/fixtures/yml/MyAwesomeJSONModel.yml'; | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a nested JSON array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * on a saved DataObject record. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 32 |  | View Code Duplication |     public function testSetValueOnNestedArray() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $model = $this->objFromFixture('MyAwesomeJSONModel', 'json-as-object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $expression = '$.cars.american.[0]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $field = $model->dbObject('MyJSON'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         // What's the value at $expression now? (JSON as return type is the default) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertEquals('["buick"]', $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         // How about now? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $field->setValue('ford', null, $expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $model->setField('MyJSON', $field->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $model->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertEquals('["ford"]', $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // And now? (With chaining) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             ->setValue('chrysler', null, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             ->setReturnType('array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $model->setField('MyJSON', $field->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $model->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->assertEquals(['chrysler'], $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Tests JSONText::setValue() by means of a simple JSONPath expression operating on a simple, un-nested JSON array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * on a saved DataObject record. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |     public function testSetValueOnUnNestedArray() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $model = $this->objFromFixture('MyAwesomeJSONModel', 'json-as-array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $expression = '$.[0]'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $field = $model->dbObject('MyJSON'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // What's the value at $expression now? (JSON as return type is the default) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertEquals('["buick"]', $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         // How about now? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $field->setValue('ford', null, $expression); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $model->setField('MyJSON', $field->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $model->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->assertEquals('["ford"]', $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         // And now? (With chaining) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             ->setValue('chrysler', null, $expression) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             ->setReturnType('array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $model->setField('MyJSON', $field->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $model->write(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $this->assertEquals(['chrysler'], $field->query($expression)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  * @package silverstripe-jsontext | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | class MyAwesomeJSONModel extends DataObject | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     private static $db = [ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         'MyJSON' => '\JSONText\Fields\JSONText' | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |     ]; | 
            
                                                        
            
                                    
            
            
                | 96 |  |  | } | 
            
                        
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.