| 1 |  |  | <?php namespace Chekote\NounStore\Key; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Chekote\Phake\Phake; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @covers \Chekote\NounStore\Key::resolveIndex() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ResolveIndexTest extends KeyTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         Phake::when($this->key)->resolveIndex(Phake::anyParameters())->thenCallParent(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function testIndexIsReturnedIfNthIsNull() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $index = 10; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $nth = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->assertEquals($index, Phake::makeVisible($this->key)->resolveIndex($index, $nth)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     public function mismatchedIndexAndNthDataProvider() | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         //   index, nth | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             [    1,   0], | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |             [    1,   1], | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             [    1,   5], | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             [    4,   0], | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             [    4,   4], | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             [    4,  10], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @dataProvider mismatchedIndexAndNthDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param int $index the mismatched index to pass along with the nth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param int $nth   the mismatched nth to pass along with the index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function testThrowsExceptionIfKeyAndIndexMismatch($index, $nth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->expectExceptionMessage("index $index was provided with nth $nth, but they are not equivalent"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         Phake::makeVisible($this->key)->resolveIndex($index, $nth); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function invalidNthDataProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             [  0], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             [ -1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             [ -4], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             [-10], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @dataProvider invalidNthDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param int $nth the invalid nth to pass along with the index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function testThrowsExceptionIfNthIsLessThanOne($nth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->expectException(InvalidArgumentException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->expectExceptionMessage('nth must be equal to or larger than 1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         Phake::makeVisible($this->key)->resolveIndex(null, $nth); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function validIndexAndNthDataProvider() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         //   index, nth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             [    0,   1], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             [   10,  11], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             [  100, 101], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             [ null,  50], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * Tests that calling with valid index and key param returns nth decremented by 1. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @dataProvider validIndexAndNthDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param int $index the valid index to pass along with the nth | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param int $nth   the valid nth to pass along with the index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function testValidNthIsReturnedDecrementedByOne($index, $nth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $expected = $nth - 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         /* @noinspection PhpUndefinedMethodInspection */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->assertEquals($expected, Phake::makeVisible($this->key)->resolveIndex($index, $nth)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |