| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @copyright (c) 2020 Mendel <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @license see license.txt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | namespace drycart\data\tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use drycart\data\MetaData; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author mendel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class MetadataTest extends \PHPUnit\Framework\TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 14 |  | View Code Duplication |     public function testDirectFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         $helper = new MetaData( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |             dummy\DummyModel::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             ['@var'=>'var','@param'=>'param', '@return'=>'return']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $fields = $helper->fields(true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->assertIsArray($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->assertCount(2, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->assertArrayHasKey('name', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->assertArrayHasKey('age', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->assertIsArray($fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->assertCount(1, $fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $this->assertArrayHasKey('var', $fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertEquals([['string','name']],$fields['name']['var']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->assertIsArray($fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->assertCount(1, $fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->assertArrayHasKey('var', $fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertEquals([['int','age']],$fields['age']['var']); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 35 |  | View Code Duplication |     public function testDirectMethods() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $helper = new MetaData( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             dummy\DummyModel::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ['@var'=>'var','@param'=>'param', '@return'=>'return']); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $this->assertEquals( | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |             $helper->methods(true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |                 'hydrate'=>['return'=>[['void']],'param'=>[['array', '$data']]], | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             ] | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 49 |  | View Code Duplication |     public function testExtendedFields() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $helper = new MetaData( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             dummy\DummyExtendedModel::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             ['@var'=>'var','@param'=>'param', '@return'=>'return']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $fields = $helper->fields(true); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->assertIsArray($fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertCount(2, $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->assertArrayHasKey('name', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->assertArrayHasKey('age', $fields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertIsArray($fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->assertCount(1, $fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertArrayHasKey('var', $fields['name']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertEquals([['string','name']],$fields['name']['var']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertIsArray($fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $this->assertCount(1, $fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertArrayHasKey('var', $fields['age']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertEquals([['int','age']],$fields['age']['var']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 71 |  | View Code Duplication |     public function testExtendedMethods() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $helper = new MetaData( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             dummy\DummyExtendedModel::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             ['@var'=>'var','@param'=>'param', '@return'=>'return']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->assertEquals( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $helper->methods(true), | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 'hydrate'=>['return'=>[['void']],'param'=>[['array', '$data']]], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     public function testClassRules() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $helper = new \drycart\data\MetaDataHelper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $rules = $helper->classRules(dummy\DummyModel::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $this->assertIsArray($rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->assertArrayHasKey('@author', $rules); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertEquals('mendel',$rules['@author'][0][0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     public function testGetSet() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         // Yes, it just for 100% coverage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $helper = new \drycart\data\MetaDataHelper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $config = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             'classMeta' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 "Description of DummyModel", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |                 "@author mendel" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'classRules' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |                 '@author' => [['max']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $helper->setCache([dummy\DummyModel::class=>$config]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $rules = $helper->classRules(dummy\DummyModel::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         var_dump($helper->getCache()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertEquals('max',$rules['@author'][0][0]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $this->assertEquals([dummy\DummyModel::class=>$config],$helper->getCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 115 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.