Skip to content

Commit a5493ff

Browse files
committed
Merge pull request #144 from linasmo/patch_document-finder-tests
DocumentFinder fix and tests.
2 parents 53ef50b + df4dd94 commit a5493ff

File tree

4 files changed

+163
-9
lines changed

4 files changed

+163
-9
lines changed

Mapping/DocumentFinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function getBundleDocumentPaths($bundle)
106106

107107
return glob(
108108
dirname($bundleReflection->getFileName()) .
109-
DIRECTORY_SEPARATOR . $this->getDocumentDir() .
109+
DIRECTORY_SEPARATOR . str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $this->getDocumentDir()) .
110110
DIRECTORY_SEPARATOR . '*.php'
111111
);
112112
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* Copyright (c) 2014-2015 NFQ Technologies UAB
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\Functional\Mapping;
13+
14+
15+
use ONGR\ElasticsearchBundle\Mapping\DocumentFinder;
16+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
17+
18+
class DocumentFinderTest extends WebTestCase
19+
{
20+
/**
21+
* Tests if document paths are returned for fixture bundle.
22+
*/
23+
public function testGetBundleDocumentPaths()
24+
{
25+
$finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles'));
26+
$this->assertGreaterThan(0, count($finder->getBundleDocumentPaths('AcmeTestBundle')));
27+
}
28+
29+
/**
30+
* Tests if exception is thrown for unregistered bundle.
31+
*
32+
* @expectedException \LogicException
33+
* @expectedExceptionMessage Bundle 'DemoBundle' does not exist.
34+
*/
35+
public function testGetBundleClassException()
36+
{
37+
$finder = new DocumentFinder($this->getContainer()->getParameter('kernel.bundles'));
38+
$finder->getBundleClass('DemoBundle');
39+
}
40+
41+
/**
42+
* Returns service container.
43+
*
44+
* @return object
45+
*/
46+
public function getContainer()
47+
{
48+
return $this->createClient()->getContainer();
49+
}
50+
}

Tests/Unit/Mapping/DocumentFinderTest.php

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class DocumentFinderTest extends \PHPUnit_Framework_TestCase
1717
{
1818
/**
19-
* Data provider for getNamespace tests.
19+
* Data provider for testDocumentDir tests.
2020
*
2121
* @return array $out
2222
*/
@@ -27,19 +27,39 @@ public function getTestData()
2727
// Case #0 one level directory.
2828
$out[] = [
2929
'Document',
30-
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Product'
30+
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Product',
31+
'AcmeTestBundle:Product',
32+
true,
3133
];
3234

3335
// Case #1 two levels directory, `\` directory separator.
3436
$out[] = [
3537
'Document\Document',
36-
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product'
38+
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product',
39+
'AcmeTestBundle:Product',
3740
];
3841

3942
// Case #2 two levels directory, `/` directory separator.
4043
$out[] = [
4144
'Document/Document',
42-
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product'
45+
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Document\Product',
46+
'AcmeTestBundle:Product',
47+
];
48+
49+
// Case #3 two levels directory, `/` directory separator.
50+
$out[] = [
51+
'Document/Test',
52+
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test\Item',
53+
'AcmeTestBundle:Item',
54+
true,
55+
];
56+
57+
// Case #4 two levels directory, `\` directory separator.
58+
$out[] = [
59+
'Document\Test',
60+
'ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test\Item',
61+
'AcmeTestBundle:Item',
62+
true,
4363
];
4464

4565
return $out;
@@ -48,17 +68,22 @@ public function getTestData()
4868
/**
4969
* Tests if correct namespace is returned.
5070
*
51-
* @param $documentDir
52-
* @param $expectedNamespace
71+
* @param string $documentDir
72+
* @param string $expectedNamespace
73+
* @param string $document
74+
* @param boolean $testPath
5375
*
5476
* @dataProvider getTestData
5577
*/
56-
public function testGetNamespace($documentDir, $expectedNamespace)
78+
public function testDocumentDir($documentDir, $expectedNamespace, $document, $testPath = false)
5779
{
5880
$finder = new DocumentFinder($this->getBundles());
5981
$finder->setDocumentDir($documentDir);
6082

61-
$this->assertEquals($expectedNamespace, $finder->getNamespace('AcmeTestBundle:Product'));
83+
$this->assertEquals($expectedNamespace, $finder->getNamespace($document));
84+
if ($testPath) {
85+
$this->assertGreaterThan(0, count($finder->getBundleDocumentPaths('AcmeTestBundle')));
86+
}
6287
}
6388

6489
/**
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <info@nfq.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace ONGR\ElasticsearchBundle\Tests\app\fixture\Acme\TestBundle\Document\Test;
13+
14+
use ONGR\ElasticsearchBundle\Annotation as ES;
15+
use ONGR\ElasticsearchBundle\Document\DocumentInterface;
16+
use ONGR\ElasticsearchBundle\Document\DocumentTrait;
17+
18+
/**
19+
* Document class Item.
20+
*
21+
* @ES\Document(create=false)
22+
*/
23+
class Item implements DocumentInterface
24+
{
25+
use DocumentTrait;
26+
27+
/**
28+
* @var string
29+
*
30+
* @ES\Property(name="name", type="string")
31+
*/
32+
public $name;
33+
34+
/**
35+
* @var float
36+
*
37+
* @ES\Property(type="float", name="price")
38+
*/
39+
protected $price;
40+
41+
/**
42+
* @var \DateTime
43+
*
44+
* @ES\Property(name="created_at", type="date")
45+
*/
46+
private $createdAt;
47+
48+
/**
49+
* @return float
50+
*/
51+
public function getPrice()
52+
{
53+
return $this->price;
54+
}
55+
56+
/**
57+
* @param float $price
58+
*/
59+
public function setPrice($price)
60+
{
61+
$this->price = $price;
62+
}
63+
64+
/**
65+
* @return \DateTime
66+
*/
67+
public function getCreatedAt()
68+
{
69+
return $this->createdAt;
70+
}
71+
72+
/**
73+
* @param \DateTime $createdAt
74+
*/
75+
public function setCreatedAt($createdAt)
76+
{
77+
$this->createdAt = $createdAt;
78+
}
79+
}

0 commit comments

Comments
 (0)