From a14d56720f5d4280f6293532095b2cc0a3481a02 Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Tue, 7 Aug 2012 19:25:43 +0100 Subject: [PATCH 1/3] Added findHybrid to Repository --- Repository.php | 7 +++++-- Tests/RepositoryTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Repository.php b/Repository.php index b70166f..63eea71 100644 --- a/Repository.php +++ b/Repository.php @@ -17,15 +17,18 @@ class Repository $this->finder = $finder; } - public function find($query, $limit=null) { return $this->finder->find($query, $limit); } + public function findHybrid($query, $limit=null) + { + return $this->finder->findHybrid($query, $limit); + } + public function findPaginated($query) { return $this->finder->findPaginated($query); } - } diff --git a/Tests/RepositoryTest.php b/Tests/RepositoryTest.php index 1b44543..6d51f29 100644 --- a/Tests/RepositoryTest.php +++ b/Tests/RepositoryTest.php @@ -25,6 +25,22 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $repository->find($testQuery); } + public function testThatFindCallsFindOnFinderWithLimit() + { + $testQuery = 'Test Query'; + $testLimit = 20; + + $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder') + ->disableOriginalConstructor() + ->getMock(); + $finderMock->expects($this->once()) + ->method('find') + ->with($this->equalTo($testQuery), $this->equalTo($testLimit)); + + $repository = new Repository($finderMock); + $repository->find($testQuery, $testLimit); + } + public function testThatFindPaginatedCallsFindPaginatedOnFinder() { $testQuery = 'Test Query'; @@ -40,4 +56,20 @@ class RepositoryTest extends \PHPUnit_Framework_TestCase $repository->findPaginated($testQuery); } + public function testThatFindHybridCallsFindHybridOnFinder() + { + $testQuery = 'Test Query'; + $testLimit = 20; + + $finderMock = $this->getMockBuilder('FOQ\ElasticaBundle\Finder\TransformedFinder') + ->disableOriginalConstructor() + ->getMock(); + $finderMock->expects($this->once()) + ->method('findHybrid') + ->with($this->equalTo($testQuery), $this->equalTo($testLimit)); + + $repository = new Repository($finderMock); + $repository->findHybrid($testQuery, $testLimit); + } + } From 3bc34c159c304405fc588091fdef88eb355c5aa1 Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Tue, 7 Aug 2012 19:26:41 +0100 Subject: [PATCH 2/3] Added default value for limit in FinderInterface --- Finder/FinderInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Finder/FinderInterface.php b/Finder/FinderInterface.php index 01e8971..9094149 100644 --- a/Finder/FinderInterface.php +++ b/Finder/FinderInterface.php @@ -11,5 +11,5 @@ interface FinderInterface * @param int $limit How many results to get * @return array results */ - function find($query, $limit); + function find($query, $limit = null); } From 16f439cfaf3275b36f294abeb001f613a6b7f77f Mon Sep 17 00:00:00 2001 From: Richard Miller Date: Tue, 7 Aug 2012 19:39:14 +0100 Subject: [PATCH 3/3] Stopped logger collecting data when not in debug mode --- Logger/ElasticaLogger.php | 18 +++++++++++------- Resources/config/config.xml | 1 + Tests/Logger/ElasticaLoggerTest.php | 16 ++++++++++++++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php index 6e39602..415e53b 100644 --- a/Logger/ElasticaLogger.php +++ b/Logger/ElasticaLogger.php @@ -16,16 +16,18 @@ class ElasticaLogger { protected $logger; protected $queries; + protected $debug; /** * Constructor. * * @param LoggerInterface $logger The Symfony logger */ - public function __construct(LoggerInterface $logger = null) + public function __construct(LoggerInterface $logger = null, $debug = false) { $this->logger = $logger; $this->queries = array(); + $this->debug = $debug; } /** @@ -38,12 +40,14 @@ class ElasticaLogger */ public function logQuery($path, $method, $data, $time) { - $this->queries[] = array( - 'path' => $path, - 'method' => $method, - 'data' => $data, - 'executionMS' => $time - ); + if ($this->debug) { + $this->queries[] = array( + 'path' => $path, + 'method' => $method, + 'data' => $data, + 'executionMS' => $time + ); + } if (null !== $this->logger) { $message = sprintf("%s (%s) %0.2f ms", $path, $method, $time * 1000); diff --git a/Resources/config/config.xml b/Resources/config/config.xml index f4a54b3..942ddef 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -18,6 +18,7 @@ + %kernel.debug% diff --git a/Tests/Logger/ElasticaLoggerTest.php b/Tests/Logger/ElasticaLoggerTest.php index 858975b..566757c 100644 --- a/Tests/Logger/ElasticaLoggerTest.php +++ b/Tests/Logger/ElasticaLoggerTest.php @@ -18,7 +18,7 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase public function testCorrectAmountIfRandomNumberOfQueriesAdded() { - $elasticaLogger = new ElasticaLogger; + $elasticaLogger = new ElasticaLogger(null, true); $total = rand(1, 15); for ($i = 0; $i < $total; $i++) { @@ -30,7 +30,7 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase public function testCorrectlyFormattedQueryReturned() { - $elasticaLogger = new ElasticaLogger; + $elasticaLogger = new ElasticaLogger(null, true); $path = 'testPath'; $method = 'testMethod'; @@ -49,6 +49,18 @@ class ElasticaLoggerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($expected, $returnedQueries[0]); } + public function testNoQueriesStoredIfDebugFalseAdded() + { + $elasticaLogger = new ElasticaLogger(null, false); + + $total = rand(1, 15); + for ($i = 0; $i < $total; $i++) { + $elasticaLogger->logQuery('testPath', 'testMethod', array('data'), 12); + } + + $this->assertEquals(0, $elasticaLogger->getNbQueries()); + } + public function testQueryIsLogged() { $loggerMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Log\LoggerInterface')