Skip to content

Commit

Permalink
Improving test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Mar 16, 2015
1 parent 5a4161b commit f1cbc7d
Show file tree
Hide file tree
Showing 19 changed files with 293 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ before_script:

script:
- phpunit --coverage-text
- vendor/behat/behat/bin/behat
- vendor/behat/behat/bin/behat --suite=zend_lucene
- vendor/behat/behat/bin/behat --suite=elastic
23 changes: 21 additions & 2 deletions Behat/SearchManagerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function theFollowingEntityExists($name, PyStringNode $string)
{
$this->entityClasses[$name] = 'Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\\' . $name;
file_put_contents(AppKernel::getEntityDir() . '/' . $name . '.php', $string->getRaw());
$this->pause();
}

/**
Expand Down Expand Up @@ -136,10 +137,27 @@ public function iShouldHaveTheFollowingDocuments(PyStringNode $string)
Assert::assertEquals($expected, $documents);
}

/**
* @When I index the following ":className" objects
*/
public function whenIIndexTheFollowingObjects($className, PyStringNode $string)
{
try {
$this->doIndexTheFollowingObjects($className, $string);
} catch (\Exception $e) {
$this->lastException = $e;
}
}

/**
* @Given the following ":className" objects have been indexed
*/
public function iIndexTheFollowingObjects($className, PyStringNode $string)
public function givenIIndexTheFollowingObjects($className, PyStringNode $string)
{
$this->doIndexTheFollowingObjects($className, $string);
}

private function doIndexTheFollowingObjects($className, PyStringNode $string)
{
$objectsData = json_decode($string->getRaw(), true);
Assert::assertArrayHasKey($className, $this->entityClasses, 'Entity exists');
Expand All @@ -155,6 +173,7 @@ public function iIndexTheFollowingObjects($className, PyStringNode $string)
}

$this->getSearchManager()->flush();

$this->pause();
}

Expand Down Expand Up @@ -216,7 +235,7 @@ public function iSearchForInCategoryAndIndex()
public function thenAnExceptionWithMessageShouldBeThrown($message)
{
Assert::assertNotNull($this->lastException, 'An exception has been thrown');
Assert::assertEquals($message, $this->lastException->getMessage());
Assert::assertContains($message, $this->lastException->getMessage());
}

/**
Expand Down
7 changes: 5 additions & 2 deletions Command/QueryCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ public function execute(InputInterface $input, OutputInterface $output)
$locale = $input->getOption('locale');

$searchManager = $this->getContainer()->get('massive_search.search_manager');
$res = $searchManager->createSearch($query)->indexes($index)->locale($locale)->execute();
$start = microtime(true);
$hits = $searchManager->createSearch($query)->indexes($index)->locale($locale)->execute();
$timeElapsed = microtime(true) - $start;

$table = new Table($output);
$table->setHeaders(array('Score', 'ID', 'Title', 'Description', 'Url', 'Image', 'Class'));
foreach ($res as $hit) {
foreach ($hits as $hit) {
$document = $hit->getDocument();
$table->addRow(array(
$hit->getScore(),
Expand All @@ -68,6 +70,7 @@ public function execute(InputInterface $input, OutputInterface $output)
));
}
$table->render();
$output->writeln(sprintf('%s result(s) in %fs', count($hits), $timeElapsed));
}

/**
Expand Down
29 changes: 21 additions & 8 deletions Search/Adapter/ElasticSearchAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public function index(Document $document, $indexName)
break;
default:
throw new \InvalidArgumentException(sprintf(
'Search field type "%s" is not know. Known types are: %s',
implode(', ', Field::getValidTypes())
'Search field type "%s" is not known. Known types are: %s',
$massiveField->getType(), implode(', ', Field::getValidTypes())
));
}
}
Expand Down Expand Up @@ -152,12 +152,25 @@ public function search(SearchQuery $searchQuery)
$document->setId($elasticHit['_id']);

$elasticSource = $elasticHit['_source'];
$document->setTitle($elasticSource[self::TITLE_FIELDNAME]);
$document->setDescription($elasticSource[self::DESCRIPTION_FIELDNAME]);
$document->setLocale($elasticSource[self::LOCALE_FIELDNAME]);
$document->setUrl($elasticSource[self::URL_FIELDNAME]);
$document->setClass($elasticSource[self::CLASS_TAG]);
$document->setImageUrl($elasticSource[self::IMAGE_URL]);

if (isset($elasticSource[self::TITLE_FIELDNAME])) {
$document->setTitle($elasticSource[self::TITLE_FIELDNAME]);
}
if (isset($elasticSource[self::DESCRIPTION_FIELDNAME])) {
$document->setDescription($elasticSource[self::DESCRIPTION_FIELDNAME]);
}
if (isset($elasticSource[self::LOCALE_FIELDNAME])) {
$document->setLocale($elasticSource[self::LOCALE_FIELDNAME]);
}
if (isset($elasticSource[self::URL_FIELDNAME])) {
$document->setUrl($elasticSource[self::URL_FIELDNAME]);
}
if (isset($elasticSource[self::CLASS_TAG])) {
$document->setClass($elasticSource[self::CLASS_TAG]);
}
if (isset($elasticSource[self::IMAGE_URL])) {
$document->setImageUrl($elasticSource[self::IMAGE_URL]);
}

$hit->setId($document->getId());

Expand Down
8 changes: 6 additions & 2 deletions Search/Adapter/ZendLuceneAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public function index(Document $document, $indexName)
break;
default:
throw new \InvalidArgumentException(sprintf(
'Search field type "%s" is not know. Known types are: %s',
implode(', ', Field::getValidTypes())
'Search field type "%s" is not known. Known types are: %s',
$field->getType(), implode('", "', Field::getValidTypes())
));
}

Expand Down Expand Up @@ -226,6 +226,10 @@ public function purge($indexName)
*/
public function listIndexes()
{
if (!file_exists($this->basePath)) {
return array();
}

$finder = new Finder();
$indexDirs = $finder->directories()->depth('== 0')->in($this->basePath);
$names = array();
Expand Down
9 changes: 7 additions & 2 deletions Search/SearchManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,15 @@ private function markIndexToFlush($indexName)
$this->indexesToFlush[$indexName] = true;
}

/**
* Return metadata for the given classname
*
* @param string $className
*
* @return ClassMetadata
*/
private function getMetadataFor($className)
{
syslog(LOG_INFO, $className);
$metadata = $this->metadataFactory->getMetadataForClass($className);

if (null === $metadata) {
Expand Down Expand Up @@ -413,6 +419,5 @@ private function validateQuery(SearchQuery $query)
implode('", "', $queryCategoryNames), implode('", "', $categoryNames)
));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ Feature: Search Managager category searching
I should be able to use the search manager API to do that

Background:
Given the entity "Car" exists:
Given the entity "Mouse" exists:
"""
<?php
namespace Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity;
class Car {
class Mouse {
public $id;
public $title;
}
Expand All @@ -37,11 +37,11 @@ Feature: Search Managager category searching
public $title;
}
"""
And that the following mapping for "Car" exists:
And that the following mapping for "Mouse" exists:
"""
<massive-search-mapping xmlns="http://massiveart.com/schema/dic/massive-search-mapping">
<mapping class="Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\Car">
<mapping class="Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\Mouse">
<index name="index_car"/>
<id property="id"/>
<title property="title" />
Expand Down Expand Up @@ -85,11 +85,11 @@ Feature: Search Managager category searching
</massive-search-mapping>
"""
And the following "Car" objects have been indexed
And the following "Mouse" objects have been indexed
"""
[
{ "id": 123, "title": "Car one"},
{ "id": 321, "title": "Car two"}
{ "id": 123, "title": "Mouse one"},
{ "id": 321, "title": "Mouse two"}
]
"""
And the following "Bicycle" objects have been indexed
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ Feature: Search Manager
I should be able to use the search manager API

Background:
Given the entity "Car" exists:
Given the entity "Car" exists:
"""
<?php
namespace Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity;
class Car { public $id;
class Car {
public $id;
public $title;
public $body;
public $numberOfWheels;
Expand Down Expand Up @@ -45,17 +46,6 @@ Feature: Search Manager
</massive-search-mapping>
"""

Scenario: Basic indexing
Given the following "Car" objects have been indexed
"""
[
{ "id": 123, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"},
{ "id": 321, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"}
]
"""
When I search for "My car"
Then there should be 2 results

Scenario: Purging
Given the following "Car" objects have been indexed
"""
Expand Down
90 changes: 90 additions & 0 deletions Tests/Features/index.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Feature: Indexing
In order to search for things
As a developer
I must first index them

Background:
Given the entity "Car" exists:
"""
<?php
namespace Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity;
class Car {
public $id;
public $title;
public $body;
public $numberOfWheels;
public $cost;
public $date;
public $image;
public $locale;
}
"""
And I purge the index "car"

Scenario: Basic indexing
Given that the following mapping for "Car" exists:
"""
<massive-search-mapping xmlns="http://massiveart.com/schema/dic/massive-search-mapping">
<mapping class="Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\Car">
<index name="car"/>
<id property="id"/>
<url expr="'foobar'" />
<title property="title" />
<description property="body" />
<category name="Car" />
<image property="image" />
<locale property="locale" />
<fields>
<field name="title" expr="object.title" type="string" />
<field name="body" type="string" />
<field name="numberOfWheels" type="string" />
</fields>
</mapping>
</massive-search-mapping>
"""
When I index the following "Car" objects
"""
[
{ "id": 123, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"},
{ "id": 321, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"}
]
"""
And I search for "My car"
Then there should be 2 results

Scenario: Invalid mapping, unknown field type
Given that the following mapping for "Car" exists:
"""
<massive-search-mapping xmlns="http://massiveart.com/schema/dic/massive-search-mapping">
<mapping class="Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity\Car">
<index name="car"/>
<id property="id"/>
<url expr="'foobar'" />
<title property="title" />
<description property="body" />
<category name="Car" />
<image property="image" />
<locale property="locale" />
<fields>
<field name="title" field="title" type="foobar" />
</fields>
</mapping>
</massive-search-mapping>
"""
When I index the following "Car" objects
"""
[
{ "id": 123, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"},
{ "id": 321, "url": "/url/to", "title": "My car", "body": "Hello", "image": "foo.jpg"}
]
"""
Then an exception with message 'Search field type "foobar" is not known.' should be thrown

File renamed without changes.
2 changes: 1 addition & 1 deletion Tests/Functional/Command/QueryCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public function testCommand()

$display = $this->tester->getDisplay();
$display = explode("\n", $display);
$this->assertCount(15, $display);
$this->assertCount(16, $display);
}
}
Loading

0 comments on commit f1cbc7d

Please sign in to comment.