Added tests for logging and data collection
This commit is contained in:
parent
fe3d4e06b7
commit
adf85973f5
63
Tests/DataCollector/ElasticaDataCollectorTest.php
Normal file
63
Tests/DataCollector/ElasticaDataCollectorTest.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Tests\DataCollector;
|
||||
|
||||
use FOQ\ElasticaBundle\DataCollector\ElasticaDataCollector;
|
||||
|
||||
/**
|
||||
* @author Richard Miller <info@limethinking.co.uk>
|
||||
*/
|
||||
class ElasticaDataCollectorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testCorrectAmountOfQueries()
|
||||
{
|
||||
$requestMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$responseMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$loggerMock = $this->getMockBuilder('FOQ\ElasticaBundle\Logger\ElasticaLogger')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$totalQueries = rand();
|
||||
|
||||
$loggerMock->expects($this->once())
|
||||
->method('getNbQueries')
|
||||
->will($this->returnValue($totalQueries));
|
||||
|
||||
$elasticaDataCollector = new ElasticaDataCollector($loggerMock);
|
||||
$elasticaDataCollector->collect($requestMock, $responseMock);
|
||||
$this->assertEquals($totalQueries, $elasticaDataCollector->getQueryCount());
|
||||
}
|
||||
|
||||
public function testCorrectQueriesReturned()
|
||||
{
|
||||
$requestMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$responseMock = $this->getMockBuilder('Symfony\Component\HttpFoundation\Response')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$loggerMock = $this->getMockBuilder('FOQ\ElasticaBundle\Logger\ElasticaLogger')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$queries = array('testQueries');
|
||||
|
||||
$loggerMock->expects($this->once())
|
||||
->method('getQueries')
|
||||
->will($this->returnValue($queries));
|
||||
|
||||
$elasticaDataCollector = new ElasticaDataCollector($loggerMock);
|
||||
$elasticaDataCollector->collect($requestMock, $responseMock);
|
||||
$this->assertEquals($queries, $elasticaDataCollector->getQueries());
|
||||
}
|
||||
|
||||
}
|
77
Tests/Logger/ElasticaLoggerTest.php
Normal file
77
Tests/Logger/ElasticaLoggerTest.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace FOQ\ElasticaBundle\Tests\Logger;
|
||||
|
||||
use FOQ\ElasticaBundle\Logger\ElasticaLogger;
|
||||
|
||||
/**
|
||||
* @author Richard Miller <info@limethinking.co.uk>
|
||||
*/
|
||||
class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testGetZeroIfNoQueriesAdded()
|
||||
{
|
||||
$elasticaLogger = new ElasticaLogger;
|
||||
$this->assertEquals(0, $elasticaLogger->getNbQueries());
|
||||
}
|
||||
|
||||
public function testCorrectAmountIfRandomNumberOfQueriesAdded()
|
||||
{
|
||||
$elasticaLogger = new ElasticaLogger;
|
||||
|
||||
$total = rand(1, 15);
|
||||
for ($i = 0; $i < $total; $i++) {
|
||||
$elasticaLogger->logQuery('testPath', 'testMethod', array('data'), 12);
|
||||
}
|
||||
|
||||
$this->assertEquals($total, $elasticaLogger->getNbQueries());
|
||||
}
|
||||
|
||||
public function testCorrectlyFormattedQueryReturned()
|
||||
{
|
||||
$elasticaLogger = new ElasticaLogger;
|
||||
|
||||
$path = 'testPath';
|
||||
$method = 'testMethod';
|
||||
$data = array('data');
|
||||
$time = 12;
|
||||
|
||||
$expected = array(
|
||||
'path' => $path,
|
||||
'method' => $method,
|
||||
'data' => $data,
|
||||
'executionMS' => $time
|
||||
);
|
||||
|
||||
$elasticaLogger->logQuery($path, $method, $data, $time);
|
||||
$returnedQueries = $elasticaLogger->getQueries();
|
||||
$this->assertEquals($expected, $returnedQueries[0]);
|
||||
}
|
||||
|
||||
public function testQueryIsLogged()
|
||||
{
|
||||
$loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$elasticaLogger = new ElasticaLogger($loggerMock);
|
||||
|
||||
$path = 'testPath';
|
||||
$method = 'testMethod';
|
||||
$data = array('data');
|
||||
$time = 12;
|
||||
|
||||
$expectedMessage = 'testPath (testMethod) 12000.00 ms';
|
||||
|
||||
$loggerMock->expects($this->once())
|
||||
->method('info')
|
||||
->with(
|
||||
$this->equalTo($expectedMessage),
|
||||
$this->equalTo($data)
|
||||
);
|
||||
|
||||
$elasticaLogger->logQuery($path, $method, $data, $time);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue