1414 */
1515namespace DebugKit \Test \TestCase ;
1616
17- use Cake \Database \Driver \Postgres ;
1817use Cake \Datasource \ConnectionManager ;
1918use Cake \TestSuite \TestCase ;
2019use DebugKit \DebugSql ;
20+ use DebugKit \TestApp \Stub \DebugSqlStub ;
2121
2222/**
2323 * Test the debugging SQL
@@ -41,62 +41,100 @@ public function setUp(): void
4141 }
4242
4343 /**
44- * Tests that a SQL string is outputted as text on the CLI.
44+ * Tests that a SQL string is outputted in a formatted and
45+ * highlighted fashion in a CLI environment.
4546 */
46- public function testSqlText ()
47+ public function testSqlCli ()
4748 {
4849 $ query = $ this ->newQuery ()->select (['panels.id ' ]);
4950
5051 ob_start ();
5152 $ this ->assertSame ($ query , DebugSql::sql ($ query ));
5253 $ result = ob_get_clean ();
5354
54- $ expectedText = <<<EXPECTED
55+ $ expected = <<<EXPECTED
5556%s (line %d)
5657########## DEBUG ##########
57- SELECT panels.id AS %s FROM panels panels
58- ###########################
59-
58+ [37mSELECT[0m
6059EXPECTED ;
61- $ fieldName = $ this ->connection ->getDriver () instanceof Postgres ? '"panels__id" ' : 'panels__id ' ;
62- $ expected = sprintf ($ expectedText , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 11 , $ fieldName );
63- $ this ->assertSame ($ expected , $ result );
60+ $ expected = sprintf ($ expected , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 8 );
61+ $ this ->assertTextContains ($ expected , $ result );
6462 }
6563
6664 /**
67- * Tests that a SQL string is outputted as HTML.
65+ * Tests that a SQL string is outputted as HTML in a CLI
66+ * environment.
6867 */
69- public function testSqlHtml ()
68+ public function testSqlHtmlOnCli ()
7069 {
7170 $ query = $ this ->newQuery ()->select (['panels.id ' ]);
7271
7372 ob_start ();
7473 $ this ->assertSame ($ query , DebugSql::sql ($ query , true , true ));
74+ $ result = strip_tags (ob_get_clean ());
75+ $ result = preg_replace ("/[ \n\r]/ " , '' , $ result );
76+
77+ $ this ->assertStringContainsString (sprintf ('%s (line %s) ' , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 4 ), $ result );
78+ $ this ->assertStringContainsString ('SELECT panels.id AS ' , $ result );
79+ $ this ->assertStringContainsString ('panels__id ' , $ result );
80+ $ this ->assertStringContainsString ('FROM panels panels ' , $ result );
81+ }
82+
83+ /**
84+ * Tests that a SQL string is outputted as HTML in a non-CLI
85+ * environment.
86+ */
87+ public function testSqlHtml ()
88+ {
89+ $ query = $ this ->newQuery ()->select (['panels.id ' ]);
90+
91+ ob_start ();
92+ DebugSqlStub::$ isCli = false ;
93+ $ this ->assertSame ($ query , DebugSqlStub::sql ($ query , true , true ));
94+ DebugSqlStub::$ isCli = true ;
7595 $ result = ob_get_clean ();
7696
77- $ expectedHtml = <<<EXPECTED
97+ $ expected = <<<EXPECTED
7898<div class="cake-debug-output">
7999<span><strong>%s</strong> (line <strong>%d</strong>)</span>
80100<pre class="cake-debug">
81- SELECT
82- panels.id AS %s
83- FROM
84- panels panels
85- </pre>
86- </div>
101+ <span style="font-weight:bold;">SELECT</span>
87102EXPECTED ;
88- $ fieldName = $ this ->connection ->getDriver () instanceof Postgres ? '"panels__id" ' : 'panels__id ' ;
89- $ expected = sprintf ($ expectedHtml , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 15 , $ fieldName );
90- $ this ->assertSame (str_replace ("\r" , '' , $ expected ), str_replace ("\r" , '' , $ result ));
103+ $ expected = sprintf ($ expected , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 10 );
104+ $ this ->assertTextContains (str_replace ("\r" , '' , $ expected ), str_replace ("\r" , '' , $ result ));
105+ }
106+
107+ /**
108+ * Tests that a SQL string is outputted as plain text in a non-CLI
109+ * environment.
110+ */
111+ public function testSqlPlain ()
112+ {
113+ $ query = $ this ->newQuery ()->select (['panels.id ' ]);
114+
115+ ob_start ();
116+ DebugSqlStub::$ isCli = false ;
117+ $ this ->assertSame ($ query , DebugSqlStub::sql ($ query , true , false ));
118+ DebugSqlStub::$ isCli = true ;
119+ $ result = ob_get_clean ();
120+
121+ $ expectedHtml = <<<EXPECTED
122+ %s (line %s)
123+ ########## DEBUG ##########
124+ SELECT
125+ EXPECTED ;
126+
127+ $ expected = sprintf ($ expectedHtml , str_replace (ROOT , '' , __FILE__ ), __LINE__ - 10 );
128+ $ this ->assertTextContains (str_replace ("\r" , '' , $ expected ), str_replace ("\r" , '' , $ result ));
91129 }
92130
93131 /**
94132 * Creates a Query object for testing.
95133 *
96- * @return Query
134+ * @return \Cake\ORM\ Query
97135 */
98136 private function newQuery ()
99137 {
100- return $ this ->getTableLocator ()->get ('panels ' )->query ();
138+ return $ this ->getTableLocator ()->get ('panels ' )->selectQuery ();
101139 }
102140}
0 commit comments