Skip to content

Commit

Permalink
Add isser for boolean properties. (#782)
Browse files Browse the repository at this point in the history
* Add isser for boolean properties.

* Fix namespace subdirectory in document mapping.

* Add tests for a subdir used in document dir configuration option.

* Use product for testing subdirectory fix.
  • Loading branch information
fattouchsquall authored and saimaz committed Apr 13, 2017
1 parent 162bbe1 commit aa75c61
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
24 changes: 22 additions & 2 deletions Generator/DocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace ONGR\ElasticsearchBundle\Generator;

use Doctrine\Common\Inflector\Inflector;

/**
* Document Generator
*/
Expand All @@ -33,6 +35,20 @@ class DocumentGenerator
public function get<methodName>()
{
<spaces>return $this-><fieldName>;
}';

/**
* @var string
*/
private $isMethodTemplate =
'/**
* Returns <fieldName>
*
* @return string
*/
public function is<methodName>()
{
<spaces>return $this-><fieldName>;
}';

/**
Expand Down Expand Up @@ -141,6 +157,10 @@ private function generateDocumentMethods(array $metadata)

foreach ($metadata['properties'] as $property) {
$lines[] = $this->generateDocumentMethod($property, $this->setMethodTemplate) . "\n";
if (isset($property['property_type']) && $property['property_type'] === 'boolean') {
$lines[] = $this->generateDocumentMethod($property, $this->isMethodTemplate) . "\n";
}

$lines[] = $this->generateDocumentMethod($property, $this->getMethodTemplate) . "\n";
}

Expand Down Expand Up @@ -249,8 +269,8 @@ private function generateDocumentDocBlock(array $metadata)
[
$this->getClassName($metadata),
ucfirst($metadata['annotation']),
$metadata['type'] != lcfirst($this->getClassName($metadata))
? sprintf('type="%s"', $metadata['type']) : '',
$metadata['annotation'] != 'object' ? ($metadata['type'] != lcfirst($this->getClassName($metadata))
? sprintf('type="%s"', $metadata['type']) : '') : '',
],
'/**
* <className>
Expand Down
2 changes: 1 addition & 1 deletion Mapping/DocumentFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function getBundleDocumentClasses($bundle, $documentsDirectory = null)

$bundleReflection = new \ReflectionClass($this->getBundleClass($bundle));

$documentsDirectory = DIRECTORY_SEPARATOR . $documentsDirectory . DIRECTORY_SEPARATOR;
$documentsDirectory = DIRECTORY_SEPARATOR . str_replace('\\', '/', $documentsDirectory) . DIRECTORY_SEPARATOR;
$directory = dirname($bundleReflection->getFileName()) . $documentsDirectory;

if (!is_dir($directory)) {
Expand Down
35 changes: 35 additions & 0 deletions Tests/Unit/Mapping/DocumentFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,22 @@ public function getTestGetNamespaceData()
];
}

/**
* Data provider for testGetNamespaceWithSubDirInDocumentDirectory().
*
* @return array
*/
public function getTestGetNamespaceDataWithSubDirInDocumentDir()
{
return [
[
'ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\Store\Product',
'TestBundle:Product',
'Document\Store'
],
];
}

/**
* Tests for getNamespace().
*
Expand All @@ -52,6 +68,25 @@ public function testGetNamespace($expectedNamespace, $className)
$this->assertEquals($expectedNamespace, $finder->getNamespace($className));
}

/**
* Tests for getNamespace() with a configured document directory.
*
* @param string $expectedNamespace
* @param string $className
* @param string $documentDir
*
* @dataProvider getTestGetNamespaceDataWithSubDirInDocumentDir()
*/
public function testGetNamespaceWithSubDirInDocumentDirectory($expectedNamespace, $className, $documentDir)
{
$bundles = [
'TestBundle' => 'ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\TestBundle'
];
$finder = new DocumentFinder($bundles);

$this->assertEquals($expectedNamespace, $finder->getNamespace($className, $documentDir));
}

/**
* Test for getBundleDocumentClasses().
*/
Expand Down

0 comments on commit aa75c61

Please sign in to comment.