Skip to content
Browse files

Merge branch '2.0'

Conflicts:
	Finder/FinderInterface.php
	Repository.php
  • Loading branch information...
2 parents b05af88 + 16f439c commit 67a11a59cee0e181c0d895ec205d6b6afc566ddc @richardmiller richardmiller committed
View
7 Finder/FinderInterface.php
@@ -7,10 +7,9 @@
/**
* Searches for query results within a given limit
*
- * @param mixed $query Can be a string, an array or an Elastica_Query object
- * @param int $limit How many results to get
- *
+ * @param mixed $query Can be a string, an array or an Elastica_Query object
+ * @param int $limit How many results to get
* @return array results
*/
- function find($query, $limit);
+ function find($query, $limit = null);
}
View
18 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 @@ public function __construct(LoggerInterface $logger = null)
*/
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);
View
9 Repository.php
@@ -19,20 +19,19 @@ public function __construct(PaginatedFinderInterface $finder)
$this->finder = $finder;
}
-
public function find($query, $limit=null)
{
return $this->finder->find($query, $limit);
}
- public function findPaginated($query)
+ public function findHybrid($query, $limit=null)
{
- return $this->finder->findPaginated($query);
+ return $this->finder->findHybrid($query, $limit);
}
- public function findHybrid($query)
+ public function findPaginated($query)
{
- return $this->finder->findHybrid($query);
+ return $this->finder->findPaginated($query);
}
public function createPaginatorAdapter($query)
View
1 Resources/config/config.xml
@@ -19,6 +19,7 @@
<service id="foq_elastica.logger" class="%foq_elastica.logger.class%">
<argument type="service" id="logger" on-invalid="null" />
+ <argument>%kernel.debug%</argument>
<tag name="monolog.logger" channel="elastica" />
</service>
<service id="foq_elastica.data_collector" class="%foq_elastica.data_collector.class%" public="true">
View
16 Tests/Logger/ElasticaLoggerTest.php
@@ -18,7 +18,7 @@ public function testGetZeroIfNoQueriesAdded()
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 @@ public function testCorrectAmountIfRandomNumberOfQueriesAdded()
public function testCorrectlyFormattedQueryReturned()
{
- $elasticaLogger = new ElasticaLogger;
+ $elasticaLogger = new ElasticaLogger(null, true);
$path = 'testPath';
$method = 'testMethod';
@@ -49,6 +49,18 @@ public function testCorrectlyFormattedQueryReturned()
$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')
View
32 Tests/RepositoryTest.php
@@ -25,6 +25,22 @@ public function testThatFindCallsFindOnFinder()
$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 @@ public function testThatFindPaginatedCallsFindPaginatedOnFinder()
$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);
+ }
+
}

0 comments on commit 67a11a5

Please sign in to comment.
Something went wrong with that request. Please try again.