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 224519b
Show file tree
Hide file tree
Showing 15 changed files with 242 additions and 108 deletions.
22 changes: 14 additions & 8 deletions Behat/SearchManagerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public function iShouldHaveTheFollowingDocuments(PyStringNode $string)
}

/**
* @When I index the following ":className" objects
* @Given the following ":className" objects have been indexed
*/
public function iIndexTheFollowingObjects($className, PyStringNode $string)
Expand All @@ -145,16 +146,21 @@ public function iIndexTheFollowingObjects($className, PyStringNode $string)
Assert::assertArrayHasKey($className, $this->entityClasses, 'Entity exists');
Assert::assertNotNull($objectsData);

foreach ($objectsData as $objectData) {
$object = new $this->entityClasses[$className]();
foreach ($objectData as $key => $value) {
$object->$key = $value;
try {
foreach ($objectsData as $objectData) {
$object = new $this->entityClasses[$className]();
foreach ($objectData as $key => $value) {
$object->$key = $value;
}
$this->entities[$object->id] = $object;
$this->getSearchManager()->index($object);
}
$this->entities[$object->id] = $object;
$this->getSearchManager()->index($object);

$this->getSearchManager()->flush();
} catch (\Exception $e) {
$this->lastException = $e;
}

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

Expand Down Expand Up @@ -216,7 +222,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
4 changes: 2 additions & 2 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
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
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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
Expand Down Expand Up @@ -45,17 +45,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
89 changes: 89 additions & 0 deletions Tests/Features/index.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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.
80 changes: 2 additions & 78 deletions Tests/Resources/TestBundle/EntityDist/Product.php
Original file line number Diff line number Diff line change
@@ -1,84 +1,8 @@
<?php

namespace Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Entity;
use Massive\Bundle\SearchBundle\Tests\Resources\TestBundle\Product as BaseProduct;

class Product
class Product extends BaseProduct
{
public $id;
public $title;
public $body;
public $date;
public $url;
public $locale;
public $image;

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getTitle()
{
return $this->title;
}

public function setTitle($title)
{
$this->title = $title;
}

public function getBody()
{
return $this->body;
}

public function setBody($body)
{
$this->body = $body;
}

public function getDate()
{
return $this->date;
}

public function setDate($date)
{
$this->date = $date;
}

public function getUrl()
{
return $this->url;
}

public function setUrl($url)
{
$this->url = $url;
}

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale)
{
$this->locale = $locale;
}

public function getImage()
{
return $this->image;
}

public function setImage($image)
{
$this->image = $image;
}
}
85 changes: 85 additions & 0 deletions Tests/Resources/TestBundle/Product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace Massive\Bundle\SearchBundle\Tests\Resources\TestBundle;

class Product
{
public $id;
public $title;
public $body;
public $date;
public $url;
public $locale;
public $image;

public function getId()
{
return $this->id;
}

public function setId($id)
{
$this->id = $id;
}

public function getTitle()
{
return $this->title;
}

public function setTitle($title)
{
$this->title = $title;
}

public function getBody()
{
return $this->body;
}

public function setBody($body)
{
$this->body = $body;
}

public function getDate()
{
return $this->date;
}

public function setDate($date)
{
$this->date = $date;
}

public function getUrl()
{
return $this->url;
}

public function setUrl($url)
{
$this->url = $url;
}

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale)
{
$this->locale = $locale;
}

public function getImage()
{
return $this->image;
}

public function setImage($image)
{
$this->image = $image;
}
}

1 change: 1 addition & 0 deletions Tests/Resources/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public static function resetEnvironment()
}

$fs->remove(__DIR__ . '/../Resources/app/data');
$fs->remove(__DIR__ . '/cache/jms_serializer');
}

public static function clearData()
Expand Down
Loading

0 comments on commit 224519b

Please sign in to comment.