Esempio n. 1
0
 /**
  * Perform a succesful fetch all query
  * 
  * @covers ::fetchAll
  * @covers ::getQueryLog
  * @test
  */
 public function fetchAll()
 {
     // Mock mysqli instance
     $mysqliMock = $this->getMockBuilder('\\mysqli')->disableOriginalConstructor()->setMethods(array('query'))->getMock();
     // Mock mysqli result instance which will be returned by
     // mysqli mock
     $mysqliResultMock = $this->getMockBuilder('\\mysqli_result')->disableOriginalConstructor()->setMethods(array('fetch_assoc', 'free'))->getMock();
     // Fetch resulting rows mocked
     $mysqliResultMock->expects($this->atLeastOnce())->method('fetch_assoc')->will($this->onConsecutiveCalls(array('name' => 'Gaffer'), false));
     // Resource should be freed when done
     $mysqliResultMock->expects($this->once())->method('free');
     // The following select query is expected to be executed
     $mysqliMock->expects($this->once())->method('query')->with('SELECT `name` FROM `users` WHERE `userId` = 1')->will($this->returnValue($mysqliResultMock));
     // Get client instance with the mysql mock injected
     $client = new Client($mysqliMock);
     // Execute query to get a user name
     $userData = $client->fetchAll('SELECT `name` FROM `users` WHERE `userId` = :userId', array('userId' => 1));
     // Get a log of the executed queries
     $queryLog = $client->getQueryLog();
     // Query should be "executed" like this
     $this->assertSame(array('SELECT `name` FROM `users` WHERE `userId` = 1'), $queryLog);
     // The result should be
     $this->assertSame(array(array('name' => 'Gaffer')), $userData);
 }
Esempio n. 2
0
 /**
  * Execute a failing query
  * 
  * @covers ::query
  * @expectedException \ITC\DataSource\Database\Exception
  * @test
  */
 public function queryFailure()
 {
     // Mock mysqli instance
     $mysqliMock = $this->getMockBuilder('\\mysqli')->disableOriginalConstructor()->setMethods(array('prepare', 'real_escape_string'))->getMock();
     // Get client
     $client = new \ITC\DataSource\Database\Client($mysqliMock);
     // Mock mysqli stmt instance which will be returned by
     // mysqli mock
     $mysqliStatementMock = $this->getMockBuilder('\\mysqli_stmt')->disableOriginalConstructor()->setMethods(array('execute', 'close'))->getMock();
     // Mock prepare method to return mysqli stmt
     $mysqliMock->expects($this->once())->method('prepare')->will($this->returnValue($mysqliStatementMock));
     // Expect statment to be executed
     $mysqliStatementMock->expects($this->once())->method('execute')->will($this->returnValue(false));
     // Expect statment to be closed
     $mysqliStatementMock->expects($this->once())->method('close')->will($this->returnValue(true));
     // Execute query
     $client->query('TRUNCATE `users`');
 }