'Array', 'scopes' => ['queriesLog'], ]); } public function tearDown(): void { parent::tearDown(); Log::drop('queries'); } /** * Tests that queries are logged when executed without params * * @return void */ public function testExecuteNoParams() { $inner = $this->getMockBuilder(StatementInterface::class)->getMock(); $inner->method('rowCount')->will($this->returnValue(3)); $inner->method('execute')->will($this->returnValue(true)); $driver = $this->getMockBuilder(DriverInterface::class)->getMock(); $st = new LoggingStatement($inner, $driver); $st->queryString = 'SELECT bar FROM foo'; $st->setLogger(new QueryLogger()); $st->execute(); $messages = Log::engine('queries')->read(); $this->assertCount(1, $messages); $this->assertRegExp('/^debug connection= duration=\d+ rows=3 SELECT bar FROM foo$/', $messages[0]); } /** * Tests that queries are logged when executed with params * * @return void */ public function testExecuteWithParams() { $inner = $this->getMockBuilder(StatementInterface::class)->getMock(); $inner->method('rowCount')->will($this->returnValue(4)); $inner->method('execute')->will($this->returnValue(true)); $driver = $this->getMockBuilder(DriverInterface::class)->getMock(); $st = new LoggingStatement($inner, $driver); $st->queryString = 'SELECT bar FROM foo WHERE x=:a AND y=:b'; $st->setLogger(new QueryLogger()); $st->execute(['a' => 1, 'b' => 2]); $messages = Log::engine('queries')->read(); $this->assertCount(1, $messages); $this->assertRegExp('/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE x=1 AND y=2$/', $messages[0]); } /** * Tests that queries are logged when executed with bound params * * @return void */ public function testExecuteWithBinding() { $inner = $this->getMockBuilder(StatementInterface::class)->getMock(); $inner->method('rowCount')->will($this->returnValue(4)); $inner->method('execute')->will($this->returnValue(true)); $date = new \DateTime('2013-01-01'); $inner->expects($this->at(0))->method('bindValue')->with('a', 1); $inner->expects($this->at(1))->method('bindValue')->with('b', $date); $driver = $this->getMockBuilder('Cake\Database\Driver')->getMock(); $st = new LoggingStatement($inner, $driver); $st->queryString = 'SELECT bar FROM foo WHERE a=:a AND b=:b'; $st->setLogger(new QueryLogger()); $st->bindValue('a', 1); $st->bindValue('b', $date, 'date'); $st->execute(); $st->bindValue('b', new \DateTime('2014-01-01'), 'date'); $st->execute(); $messages = Log::engine('queries')->read(); $this->assertCount(2, $messages); $this->assertRegExp("/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2013-01-01'$/", $messages[0]); $this->assertRegExp("/^debug connection= duration=\d+ rows=4 SELECT bar FROM foo WHERE a='1' AND b='2014-01-01'$/", $messages[1]); } /** * Tests that queries are logged despite database errors * * @return void */ public function testExecuteWithError() { $exception = new LogicException('This is bad'); $inner = $this->getMockBuilder(StatementInterface::class)->getMock(); $inner->expects($this->once()) ->method('execute') ->will($this->throwException($exception)); $driver = $this->getMockBuilder(DriverInterface::class)->getMock(); $st = new LoggingStatement($inner, $driver); $st->queryString = 'SELECT bar FROM foo'; $st->setLogger(new QueryLogger()); try { $st->execute(); } catch (LogicException $e) { $this->assertSame('This is bad', $e->getMessage()); $this->assertSame($st->queryString, $e->queryString); } $messages = Log::engine('queries')->read(); $this->assertCount(1, $messages); $this->assertRegExp("/^debug connection= duration=\d+ rows=0 SELECT bar FROM foo$/", $messages[0]); } /** * Tests setting and getting the logger * * @return void */ public function testSetAndGetLogger() { $logger = new QueryLogger(); $st = new LoggingStatement( $this->getMockBuilder(StatementInterface::class)->getMock(), $this->getMockBuilder(DriverInterface::class)->getMock() ); $st->setLogger($logger); $this->assertSame($logger, $st->getLogger()); } }