Example #1
0
 public function testStartQueryThrowsExceptionWhenStreamWriteFails()
 {
     $log = new SqlLogger('php://memory');
     fclose($log->getStream());
     // Prematurely close the logger's stream
     // Now have logger attempt to write to its stream
     $sql = 'SELECT * FROM test WHERE a1 = ? AND a2 = ?';
     $params = array('one', 'two');
     try {
         $log->startQuery($sql, $params);
         $this->fail();
     } catch (\Exception $e) {
         $this->assertEquals('Failed to write to stream', $e->getMessage());
     }
 }
Example #2
0
 /**
  * (non-PHPdoc)
  * @see Ray\Aop.MethodInterceptor::invoke()
  */
 public function invoke(MethodInvocation $invocation)
 {
     $object = $invocation->getThis();
     $method = $invocation->getMethod();
     $connectionParams = $method->name !== 'onGet' ? $this->slaveDb : $this->masterDb;
     $pagerAnnotation = $this->reader->getMethodAnnotation($method, 'BEAR\\Sunday\\Annotation\\DbPager');
     if ($pagerAnnotation) {
         $connectionParams['wrapperClass'] = 'BEAR\\Package\\Module\\Database\\DoctrineDbalModule\\Connection';
         $db = DriverManager::getConnection($connectionParams);
         $db->setMaxPerPage($pagerAnnotation->limit);
     } else {
         $db = DriverManager::getConnection($connectionParams);
     }
     /* @var $db \BEAR\Package\Module\Database\DoctrineDbalModule\Connection */
     if ($this->sqlLogger instanceof SQLLogger) {
         $db->getConfiguration()->setSQLLogger($this->sqlLogger);
     }
     $object->setDb($db);
     $result = $invocation->proceed();
     if ($this->sqlLogger instanceof SQLLogger) {
         $this->sqlLogger->stopQuery();
         $object->headers['x-sql'] = [$this->sqlLogger->queries];
     }
     if ($pagerAnnotation) {
         $pagerData = $db->getPager();
         if ($pagerData) {
             $object->headers['pager'] = $pagerData;
         }
     }
     return $result;
 }