| 1 |  |  | <?php namespace Chekote\NounStore\Key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Chekote\Phake\Phake; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @covers \Chekote\NounStore\Key::parse() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ParseTest extends KeyTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         Phake::when($this->key)->parse(Phake::anyParameters())->thenCallParent(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * Provides examples of valid key and index pairs with expected parse results. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function successScenarioDataProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         //   key           index   parsedKey,  parsedIndex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             ['Thing',       null, 'Thing',            null], // no nth in key or index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             ['1st Thing',   null, 'Thing',               0], // 1st in key with no index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             ['1st Thing',      0, 'Thing',               0], // nth in key with matching index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             ['2nd Thing',   null, 'Thing',               1], // 2nd in key with no index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             ['3rd Thing',   null, 'Thing',               2], // 3rd in key with no index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             ['4th Thing',   null, 'Thing',               3], // 3th in key with no index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             ['478th Thing', null, 'Thing',             477], // high nth in key with no index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             ['Thing',          0, 'Thing',               0], // no nth in key with 0 index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             ['Thing',         49, 'Thing',              49], // no nth in key with high index param | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Tests that calling Key::parse with valid key and index combinations works correctly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @dataProvider successScenarioDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param string $key         the key to parse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param int    $index       the index to pass along with the key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $parsedKey   the expected resulting parsed key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param int    $parsedIndex the expected resulting parsed index | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     public function testSuccessScenario($key, $index, $parsedKey, $parsedIndex) | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertEquals([$parsedKey, $parsedIndex], $this->key->parse($key, $index)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Provides examples of mismatched key & index pairs. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function mismatchedKeyAndIndexDataProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ['1st Thing', 1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             ['1st Thing', 2], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ['4th Person', 0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             ['4th Person', 4], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ['4th Person', 10], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * Tests that calling Key::parse with mismatched key and index param throws an exception. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @dataProvider mismatchedKeyAndIndexDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param string $key   the key to parse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $index the mismatched index to pass along with the key | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function testParseKeyThrowsExceptionIfKeyAndIndexMismatch($key, $index) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $this->expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->expectExceptionMessage( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             "$index was provided for index param when key '$key' contains an nth value, but they do not match" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->key->parse($key, $index); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |  |