From 2ab2ea712b18e4487bab1fc957301ec3dec0c3e3 Mon Sep 17 00:00:00 2001 From: Linas Mockus Date: Wed, 21 Jan 2015 16:44:19 +0200 Subject: [PATCH 1/2] Replace document_dir slashes. --- Mapping/DocumentFinder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Mapping/DocumentFinder.php b/Mapping/DocumentFinder.php index 05741ab9..bf9d784e 100644 --- a/Mapping/DocumentFinder.php +++ b/Mapping/DocumentFinder.php @@ -106,7 +106,7 @@ public function getBundleDocumentPaths($bundle) return glob( dirname($bundleReflection->getFileName()) . - DIRECTORY_SEPARATOR . $this->getDocumentDir() . + DIRECTORY_SEPARATOR . str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $this->getDocumentDir()) . DIRECTORY_SEPARATOR . '*.php' ); } From df4dd94e968e2c1c031160c93acf7b89dd5c6fa0 Mon Sep 17 00:00:00 2001 From: Linas Mockus Date: Wed, 21 Jan 2015 16:44:36 +0200 Subject: [PATCH 2/2] Updated tests. --- .../Functional/Mapping/DocumentFinderTest.php | 50 ++++++++++++ Tests/Unit/Mapping/DocumentFinderTest.php | 41 ++++++++-- .../Acme/TestBundle/Document/Test/Item.php | 79 +++++++++++++++++++ 3 files changed, 162 insertions(+), 8 deletions(-) create mode 100644 Tests/Functional/Mapping/DocumentFinderTest.php create mode 100644 Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php diff --git a/Tests/Functional/Mapping/DocumentFinderTest.php b/Tests/Functional/Mapping/DocumentFinderTest.php new file mode 100644 index 00000000..f9bee17e --- /dev/null +++ b/Tests/Functional/Mapping/DocumentFinderTest.php @@ -0,0 +1,50 @@ +getContainer()->getParameter('kernel.bundles')); + $this->assertGreaterThan(0, count($finder->getBundleDocumentPaths('AcmeTestBundle'))); + } + + /** + * Tests if exception is thrown for unregistered bundle. + * + * @expectedException \LogicException + * @expectedExceptionMessage Bundle 'DemoBundle' does not exist. + */ + public function testGetBundleClassException() + { + $finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles')); + $finder->getBundleClass('DemoBundle'); + } + + /** + * Returns service container. + * + * @return object + */ + public function getContainer() + { + return $this->createClient()->getContainer(); + } +} diff --git a/Tests/Unit/Mapping/DocumentFinderTest.php b/Tests/Unit/Mapping/DocumentFinderTest.php index 2f6a26db..bdfda49e 100644 --- a/Tests/Unit/Mapping/DocumentFinderTest.php +++ b/Tests/Unit/Mapping/DocumentFinderTest.php @@ -16,7 +16,7 @@ class DocumentFinderTest extends \PHPUnit_Framework_TestCase { /** - * Data provider for getNamespace tests. + * Data provider for testDocumentDir tests. * * @return array $out */ @@ -27,19 +27,39 @@ public function getTestData() // Case #0 one level directory. $out[] = [ 'Document', - 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Product' + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Product', + 'AcmeTestBundle:Product', + true, ]; // Case #1 two levels directory, `\` directory separator. $out[] = [ 'Document\Document', - 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product' + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product', + 'AcmeTestBundle:Product', ]; // Case #2 two levels directory, `/` directory separator. $out[] = [ 'Document/Document', - 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product' + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product', + 'AcmeTestBundle:Product', + ]; + + // Case #3 two levels directory, `/` directory separator. + $out[] = [ + 'Document/Test', + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test\Item', + 'AcmeTestBundle:Item', + true, + ]; + + // Case #4 two levels directory, `\` directory separator. + $out[] = [ + 'Document\Test', + 'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test\Item', + 'AcmeTestBundle:Item', + true, ]; return $out; @@ -48,17 +68,22 @@ public function getTestData() /** * Tests if correct namespace is returned. * - * @param $documentDir - * @param $expectedNamespace + * @param string $documentDir + * @param string $expectedNamespace + * @param string $document + * @param boolean $testPath * * @dataProvider getTestData */ - public function testGetNamespace($documentDir, $expectedNamespace) + public function testDocumentDir($documentDir, $expectedNamespace, $document, $testPath = false) { $finder = new DocumentFinder($this->getBundles()); $finder->setDocumentDir($documentDir); - $this->assertEquals($expectedNamespace, $finder->getNamespace('AcmeTestBundle:Product')); + $this->assertEquals($expectedNamespace, $finder->getNamespace($document)); + if ($testPath) { + $this->assertGreaterThan(0, count($finder->getBundleDocumentPaths('AcmeTestBundle'))); + } } /** diff --git a/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php b/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php new file mode 100644 index 00000000..45fc3db2 --- /dev/null +++ b/Tests/app/fixture/Acme/TestBundle/Document/Test/Item.php @@ -0,0 +1,79 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test; + +use ONGR\ElasticsearchBundle\Annotation as ES; +use ONGR\ElasticsearchBundle\Document\DocumentInterface; +use ONGR\ElasticsearchBundle\Document\DocumentTrait; + +/** + * Document class Item. + * + * @ES\Document(create=false) + */ +class Item implements DocumentInterface +{ + use DocumentTrait; + + /** + * @var string + * + * @ES\Property(name="name", type="string") + */ + public $name; + + /** + * @var float + * + * @ES\Property(type="float", name="price") + */ + protected $price; + + /** + * @var \DateTime + * + * @ES\Property(name="created_at", type="date") + */ + private $createdAt; + + /** + * @return float + */ + public function getPrice() + { + return $this->price; + } + + /** + * @param float $price + */ + public function setPrice($price) + { + $this->price = $price; + } + + /** + * @return \DateTime + */ + public function getCreatedAt() + { + return $this->createdAt; + } + + /** + * @param \DateTime $createdAt + */ + public function setCreatedAt($createdAt) + { + $this->createdAt = $createdAt; + } +}