Skip to content

Commit 191b00d

Browse files
author
Simonas Šerlinskas
committed
Merge 5.2 branch to 6.0-dev
2 parents ceea665 + 11b116e commit 191b00d

File tree

6 files changed

+128
-9
lines changed

6 files changed

+128
-9
lines changed

Event/PrePersistEvent.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
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\Event;
13+
14+
use Symfony\Component\EventDispatcher\Event;
15+
16+
class PrePersistEvent extends Event
17+
{
18+
/**
19+
* @var object
20+
*/
21+
private $document;
22+
23+
/**
24+
* PrePersistEvent constructor.
25+
* @param $document
26+
*/
27+
public function __construct($document)
28+
{
29+
$this->document = $document;
30+
}
31+
32+
/**
33+
* @return object
34+
*/
35+
public function getDocument()
36+
{
37+
return $this->document;
38+
}
39+
40+
/**
41+
* @param object $document
42+
*/
43+
public function setDocument($document)
44+
{
45+
$this->document = $document;
46+
}
47+
}

Generator/DocumentGenerator.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private function generateDocumentConstructor(array $metadata)
171171

172172
foreach ($metadata['properties'] as $prop) {
173173
if ($prop['annotation'] == 'embedded' && isset($prop['property_multiple']) && $prop['property_multiple']) {
174-
$fields[] = sprintf('%s$this->%s = new Collection();', $this->spaces, $prop['field_name']);
174+
$fields[] = sprintf('%s$this->%s = new ArrayCollection();', $this->spaces, $prop['field_name']);
175175
}
176176
}
177177

@@ -239,7 +239,8 @@ private function generatePropertyDocBlock(array $metadata)
239239
$column[] = 'options={' . $metadata['property_options'] . '}';
240240
}
241241

242-
$lines[] = $this->spaces . ' * @ES\\' . ucfirst($metadata['annotation']) . '(' . implode(', ', $column) . ')';
242+
$lines[] = $this->spaces . ' * @ES\\' . Inflector::classify($metadata['annotation'])
243+
. '(' . implode(', ', $column) . ')';
243244

244245
$lines[] = $this->spaces . ' */';
245246

@@ -259,9 +260,8 @@ private function generateDocumentDocBlock(array $metadata)
259260
['<className>', '<annotation>', '<options>'],
260261
[
261262
$this->getClassName($metadata),
262-
ucfirst($metadata['annotation']),
263-
$metadata['annotation'] != 'object' ? ($metadata['type'] != lcfirst($this->getClassName($metadata))
264-
? sprintf('type="%s"', $metadata['type']) : '') : '',
263+
Inflector::classify($metadata['annotation']),
264+
$this->getAnnotationOptions($metadata),
265265
],
266266
'/**
267267
* <className>
@@ -302,6 +302,26 @@ private function getClassName(array $metadata)
302302
? substr($metadata['name'], $pos + 1, strlen($metadata['name'])) : $metadata['name'];
303303
}
304304

305+
/**
306+
* Returns annotation options
307+
*
308+
* @param array $metadata
309+
*
310+
* @return string
311+
*/
312+
private function getAnnotationOptions(array $metadata)
313+
{
314+
if (in_array($metadata['annotation'], ['object', 'object_type'])) {
315+
return '';
316+
}
317+
318+
if ($metadata['type'] === Inflector::tableize($this->getClassName($metadata))) {
319+
return '';
320+
}
321+
322+
return sprintf('type="%s"', $metadata['type']);
323+
}
324+
305325
/**
306326
* Generates document use statements
307327
*
@@ -314,7 +334,7 @@ private function generateDocumentUse(array $metadata)
314334
$uses = ['use ONGR\ElasticsearchBundle\Annotation as ES;'];
315335

316336
if ($this->hasMultipleEmbedded($metadata)) {
317-
$uses[] = 'use ONGR\ElasticsearchBundle\Collection\Collection;';
337+
$uses[] = 'use Doctrine\Common\Collections\ArrayCollection;';
318338
}
319339

320340
return implode("\n", $uses) . "\n";

Tests/Functional/Command/GenerateDocumentCommandTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
namespace ONGR\ElasticsearchBundle\Tests\Functional\Command;
44

55
use ONGR\ElasticsearchBundle\Command\DocumentGenerateCommand;
6-
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7-
use Symfony\Component\Console\Application;
8-
use Symfony\Component\Console\Tester\CommandTester;
6+
use ONGR\ElasticsearchBundle\Tests\WebTestCase;
97

108
class GenerateDocumentCommandTest extends WebTestCase
119
{

Tests/Unit/DependencyInjection/Compiler/MappingPassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace ONGR\ElasticsearchBundle\Tests\Unit\DependencyInjection\Compiler;
1313

1414
use ONGR\ElasticsearchBundle\DependencyInjection\Compiler\MappingPass;
15+
use Symfony\Component\DependencyInjection\Alias;
1516

1617
/**
1718
* Unit tests for MappingPass.
@@ -103,6 +104,21 @@ function ($parameter) use ($metadataCollectorMock) {
103104
)
104105
->willReturn(null);
105106

107+
$containerMock
108+
->expects($this->exactly(1))
109+
->method('setAlias')
110+
->withConsecutive(
111+
[$this->equalTo('es.manager')],
112+
[$this->equalTo('es.manager.default')]
113+
)
114+
->willReturn(new Alias('es.manager', 'es.manager.default'));
115+
116+
$containerMock
117+
->expects($this->any())
118+
->method('getAlias')
119+
->with('es.manager')
120+
->willReturn(new Alias('es.manager', 'es.manager.default'));
121+
106122
return $containerMock;
107123
}
108124

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ONGR package.
5+
*
6+
* (c) NFQ Technologies UAB <[email protected]>
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\Unit\Event;
13+
14+
use ONGR\ElasticsearchBundle\Event\PrePersistEvent;
15+
use ONGR\ElasticsearchBundle\Tests\app\fixture\TestBundle\Document\Product;
16+
17+
class PrePersistEventTest extends \PHPUnit_Framework_TestCase
18+
{
19+
public function testGetters()
20+
{
21+
$entity = new Product();
22+
$event = new PrePersistEvent($entity);
23+
24+
$this->assertInstanceOf(Product::class, $event->getDocument());
25+
}
26+
}

Tests/WebTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
namespace ONGR\ElasticsearchBundle\Tests;
3+
4+
class WebTestCase extends \Symfony\Bundle\FrameworkBundle\Test\WebTestCase
5+
{
6+
public static function getKernelClass()
7+
{
8+
require_once __DIR__.'/app/AppKernel.php';
9+
10+
return \AppKernel::class;
11+
}
12+
}

0 commit comments

Comments
 (0)