| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LeKoala\DebugBar\Test\Collector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use SilverStripe\ORM\DB; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LeKoala\DebugBar\DebugBar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use SilverStripe\Dev\SapphireTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SilverStripe\Core\Config\Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use LeKoala\DebugBar\Extension\ProxyDBExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use LeKoala\DebugBar\Collector\DatabaseCollector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class DatabaseCollectorTest extends SapphireTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var DatabaseCollector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $collector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     protected $usesDatabase = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     public function setUp() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         DebugBar::initDebugBar(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->collector = DebugBar::getDebugBar()->getCollector('db'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function tearDown() | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         DebugBar::clearDebugBar(); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->collector = null; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function testCollectorExists() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->assertInstanceOf(DatabaseCollector::class, $this->collector); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function testCollect() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // Update the limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $testLimit = 500; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         Config::modify()->set(DebugBar::class, 'query_limit', $testLimit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // Deliberately high warning threshold | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         Config::modify()->set(DebugBar::class, 'warn_dbqueries_threshold_seconds', 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $result = $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertGreaterThan(1, $result['nb_statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->assertEquals(0, $result['nb_failed_statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         // This should be equal if below the limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($result['nb_statements'] <= $testLimit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $this->assertCount($result['nb_statements'], $result['statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // Make sure each statement has all its required details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $statement = array_shift($result['statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $this->assertNotEmpty($statement['sql']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->assertEquals(1, $statement['is_success']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertNotEmpty($statement['source']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->assertFalse($statement['warn']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         // Deliberately low warning threshold | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         Config::modify()->set(DebugBar::class, 'warn_dbqueries_threshold_seconds', 0.0000001); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $result = $this->collector->collect(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertNotEmpty($result['statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $statement = array_shift($result['statements']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->assertTrue($statement['warn']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     public function testGetWidgets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $expected = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'database' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 'icon' => 'database', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 'widget' => 'PhpDebugBar.Widgets.SQLQueriesWidget', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 'map' => 'db', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 'default' => '[]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'database:badge' => array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 'map' => 'db.nb_statements', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 'default' => 0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $this->assertSame($expected, $this->collector->getWidgets()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     public function testGetAssets() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $config = $this->collector->getAssets(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $this->assertArrayHasKey('base_path', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->assertArrayHasKey('base_url', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->assertArrayHasKey('css', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->assertArrayHasKey('js', $config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $this->assertFileExists(implode(DIRECTORY_SEPARATOR, [BASE_PATH, $config['base_path'], $config['css']])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $this->assertFileExists(implode(DIRECTORY_SEPARATOR, [BASE_PATH, $config['base_path'], $config['js']])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |  |