| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Spiral Framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @license   MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author    Anton Titov (Wolfy-J) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Spiral\Tests\Debug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Psr\Log\LogLevel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Spiral\Debug\Exception\StateException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Spiral\Debug\State; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Spiral\Logger\Event\LogEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class StateTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     public function testTags(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $state = new State(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->assertEquals([], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         $state->setTag('key', 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |             'key' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         ], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $state->setTag('key2', 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             'key'  => 'value', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'key2' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         ], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $state->setTag('key', 'value2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'key'  => 'value2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'key2' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $state->setTags(['a' => 'b']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'a' => 'b', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         ], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $state->reset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertEquals([], $state->getTags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     public function testTagsException(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $state = new State(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->expectException(StateException::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $state->setTags(['aa' => 11]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function testExtras(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $state = new State(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertEquals([], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $state->setVariable('key', 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'key' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         ], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $state->setVariable('key2', 'value'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             'key'  => 'value', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             'key2' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         ], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $state->setVariable('key', 'value2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'key'  => 'value2', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'key2' => 'value' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         ], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $state->setVariables(['a' => 'b']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->assertEquals([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             'a' => 'b', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $state->reset(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertEquals([], $state->getVariables()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     public function testLogEvents(): void | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         $state = new State(); | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |         $this->assertEquals([], $state->getLogEvents()); | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $state->addLogEvent(new LogEvent( | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             new \DateTime(), | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |             'default', | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             LogLevel::ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             'message' | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |         )); | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |         $this->assertCount(1, $state->getLogEvents()); | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |         $state->addLogEvent( | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             new LogEvent( | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |                 new \DateTime(), | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 'default1', | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |                 LogLevel::ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |                 'message' | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             new LogEvent( | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |                 new \DateTime(), | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |                 'default1', | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |                 LogLevel::ERROR, | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |                 'message' | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |             ) | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         $this->assertCount(3, $state->getLogEvents()); | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |         $state->reset(); | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |         $this->assertEquals([], $state->getLogEvents()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  |