Skip to content

Commit 73093be

Browse files
committed
Merge pull request #823 from merk/more-tests
More test coverage
2 parents bb4618c + 5181b02 commit 73093be

File tree

17 files changed

+843
-390
lines changed

17 files changed

+843
-390
lines changed

CHANGELOG-3.1.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ https://github.com/FriendsOfSymfony/FOSElasticaBundle/compare/v3.0.4...v3.1.0
4444
* New events `PRE_INDEX_RESET`, `POST_INDEX_RESET`, `PRE_TYPE_RESET` and
4545
`POST_TYPE_RESET` are run before and after operations that will reset an
4646
index. #744
47+
* Added indexable callback support for the __invoke method of a service. #823

Command/PopulateCommand.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
9090
$index = $input->getOption('index');
9191
$type = $input->getOption('type');
9292
$reset = !$input->getOption('no-reset');
93-
$options = $input->getOptions();
94-
$options['ignore-errors'] = $input->getOption('ignore-errors');
93+
$options = array(
94+
'batch_size' => $input->getOption('batch-size'),
95+
'ignore_errors' => $input->getOption('ignore-errors'),
96+
'offset' => $input->getOption('offset'),
97+
'sleep' => $input->getOption('sleep')
98+
);
9599

96100
if ($input->isInteractive() && $reset && $input->getOption('offset')) {
97101
/** @var DialogHelper $dialog */

Doctrine/AbstractProvider.php

Lines changed: 75 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use FOS\ElasticaBundle\Persister\ObjectPersisterInterface;
88
use FOS\ElasticaBundle\Provider\AbstractProvider as BaseAbstractProvider;
99
use FOS\ElasticaBundle\Provider\IndexableInterface;
10+
use Symfony\Component\OptionsResolver\OptionsResolver;
1011

1112
abstract class AbstractProvider extends BaseAbstractProvider
1213
{
@@ -26,79 +27,114 @@ abstract class AbstractProvider extends BaseAbstractProvider
2627
* @param ObjectPersisterInterface $objectPersister
2728
* @param IndexableInterface $indexable
2829
* @param string $objectClass
29-
* @param array $options
30+
* @param array $baseOptions
3031
* @param ManagerRegistry $managerRegistry
3132
* @param SliceFetcherInterface $sliceFetcher
3233
*/
3334
public function __construct(
3435
ObjectPersisterInterface $objectPersister,
3536
IndexableInterface $indexable,
3637
$objectClass,
37-
array $options,
38+
array $baseOptions,
3839
ManagerRegistry $managerRegistry,
3940
SliceFetcherInterface $sliceFetcher = null
4041
) {
41-
parent::__construct($objectPersister, $indexable, $objectClass, array_merge(array(
42-
'clear_object_manager' => true,
43-
'debug_logging' => false,
44-
'ignore_errors' => false,
45-
'query_builder_method' => 'createQueryBuilder',
46-
), $options));
42+
parent::__construct($objectPersister, $indexable, $objectClass, $baseOptions);
4743

4844
$this->managerRegistry = $managerRegistry;
4945
$this->sliceFetcher = $sliceFetcher;
5046
}
5147

48+
/**
49+
* Counts objects that would be indexed using the query builder.
50+
*
51+
* @param object $queryBuilder
52+
*
53+
* @return integer
54+
*/
55+
abstract protected function countObjects($queryBuilder);
56+
57+
/**
58+
* Creates the query builder, which will be used to fetch objects to index.
59+
*
60+
* @param string $method
61+
*
62+
* @return object
63+
*/
64+
abstract protected function createQueryBuilder($method);
65+
66+
/**
67+
* Fetches a slice of objects using the query builder.
68+
*
69+
* @param object $queryBuilder
70+
* @param integer $limit
71+
* @param integer $offset
72+
*
73+
* @return array
74+
*/
75+
abstract protected function fetchSlice($queryBuilder, $limit, $offset);
76+
5277
/**
5378
* {@inheritDoc}
5479
*/
55-
public function populate(\Closure $loggerClosure = null, array $options = array())
80+
protected function doPopulate($options, \Closure $loggerClosure = null)
5681
{
57-
if (!$this->options['debug_logging']) {
58-
$logger = $this->disableLogging();
59-
}
82+
$manager = $this->managerRegistry->getManagerForClass($this->objectClass);
6083

61-
$queryBuilder = $this->createQueryBuilder();
84+
$queryBuilder = $this->createQueryBuilder($options['query_builder_method']);
6285
$nbObjects = $this->countObjects($queryBuilder);
63-
$offset = isset($options['offset']) ? intval($options['offset']) : 0;
64-
$sleep = isset($options['sleep']) ? intval($options['sleep']) : 0;
65-
$batchSize = isset($options['batch-size']) ? intval($options['batch-size']) : $this->options['batch_size'];
66-
$ignoreErrors = isset($options['ignore-errors']) ? $options['ignore-errors'] : $this->options['ignore_errors'];
67-
$manager = $this->managerRegistry->getManagerForClass($this->objectClass);
86+
$offset = $options['offset'];
6887

6988
$objects = array();
70-
for (; $offset < $nbObjects; $offset += $batchSize) {
71-
$objects = $this->getSlice($queryBuilder, $batchSize, $offset, $objects);
72-
$objects = array_filter($objects, array($this, 'isObjectIndexable'));
89+
for (; $offset < $nbObjects; $offset += $options['batch_size']) {
90+
try {
91+
$objects = $this->getSlice($queryBuilder, $options['batch_size'], $offset, $objects);
92+
$objects = $this->filterObjects($options, $objects);
7393

74-
if (!empty($objects)) {
75-
if (!$ignoreErrors) {
94+
if (!empty($objects)) {
7695
$this->objectPersister->insertMany($objects);
77-
} else {
78-
try {
79-
$this->objectPersister->insertMany($objects);
80-
} catch (BulkResponseException $e) {
81-
if ($loggerClosure) {
82-
$loggerClosure($batchSize, $nbObjects, sprintf('<error>%s</error>', $e->getMessage()));
83-
}
84-
}
96+
}
97+
} catch (BulkResponseException $e) {
98+
if (!$options['ignore_errors']) {
99+
throw $e;
100+
}
101+
102+
if (null !== $loggerClosure) {
103+
$loggerClosure(
104+
$options['batch_size'],
105+
$nbObjects,
106+
sprintf('<error>%s</error>', $e->getMessage())
107+
);
85108
}
86109
}
87110

88-
if ($this->options['clear_object_manager']) {
111+
if ($options['clear_object_manager']) {
89112
$manager->clear();
90113
}
91114

92-
usleep($sleep);
115+
usleep($options['sleep']);
93116

94-
if ($loggerClosure) {
95-
$loggerClosure($batchSize, $nbObjects);
117+
if (null !== $loggerClosure) {
118+
$loggerClosure($options['batch_size'], $nbObjects);
96119
}
97120
}
121+
}
98122

99-
if (!$this->options['debug_logging']) {
100-
$this->enableLogging($logger);
101-
}
123+
/**
124+
* {@inheritDoc}
125+
*/
126+
protected function configureOptions()
127+
{
128+
parent::configureOptions();
129+
130+
$this->resolver->setDefaults(array(
131+
'clear_object_manager' => true,
132+
'debug_logging' => false,
133+
'ignore_errors' => false,
134+
'offset' => 0,
135+
'query_builder_method' => 'createQueryBuilder',
136+
'sleep' => 0
137+
));
102138
}
103139

104140
/**
@@ -112,7 +148,7 @@ public function populate(\Closure $loggerClosure = null, array $options = array(
112148
*
113149
* @return array
114150
*/
115-
protected function getSlice($queryBuilder, $limit, $offset, $lastSlice)
151+
private function getSlice($queryBuilder, $limit, $offset, $lastSlice)
116152
{
117153
if (!$this->sliceFetcher) {
118154
return $this->fetchSlice($queryBuilder, $limit, $offset);
@@ -131,47 +167,4 @@ protected function getSlice($queryBuilder, $limit, $offset, $lastSlice)
131167
$identifierFieldNames
132168
);
133169
}
134-
135-
/**
136-
* Counts objects that would be indexed using the query builder.
137-
*
138-
* @param object $queryBuilder
139-
*
140-
* @return integer
141-
*/
142-
abstract protected function countObjects($queryBuilder);
143-
144-
/**
145-
* Disables logging and returns the logger that was previously set.
146-
*
147-
* @return mixed
148-
*/
149-
abstract protected function disableLogging();
150-
151-
/**
152-
* Reenables the logger with the previously returned logger from disableLogging();.
153-
*
154-
* @param mixed $logger
155-
*
156-
* @return mixed
157-
*/
158-
abstract protected function enableLogging($logger);
159-
160-
/**
161-
* Fetches a slice of objects using the query builder.
162-
*
163-
* @param object $queryBuilder
164-
* @param integer $limit
165-
* @param integer $offset
166-
*
167-
* @return array
168-
*/
169-
abstract protected function fetchSlice($queryBuilder, $limit, $offset);
170-
171-
/**
172-
* Creates the query builder, which will be used to fetch objects to index.
173-
*
174-
* @return object
175-
*/
176-
abstract protected function createQueryBuilder();
177170
}

Doctrine/MongoDB/Provider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function enableLogging($logger)
4444
}
4545

4646
/**
47-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::countObjects()
47+
* {@inheritDoc}
4848
*/
4949
protected function countObjects($queryBuilder)
5050
{
@@ -58,7 +58,7 @@ protected function countObjects($queryBuilder)
5858
}
5959

6060
/**
61-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::fetchSlice()
61+
* {@inheritDoc}
6262
*/
6363
protected function fetchSlice($queryBuilder, $limit, $offset)
6464
{
@@ -75,13 +75,13 @@ protected function fetchSlice($queryBuilder, $limit, $offset)
7575
}
7676

7777
/**
78-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::createQueryBuilder()
78+
* {@inheritDoc}
7979
*/
80-
protected function createQueryBuilder()
80+
protected function createQueryBuilder($method)
8181
{
8282
return $this->managerRegistry
8383
->getManagerForClass($this->objectClass)
8484
->getRepository($this->objectClass)
85-
->{$this->options['query_builder_method']}();
85+
->{$method}();
8686
}
8787
}

Doctrine/ORM/Provider.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function enableLogging($logger)
4646
}
4747

4848
/**
49-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::countObjects()
49+
* {@inheritDoc}
5050
*/
5151
protected function countObjects($queryBuilder)
5252
{
@@ -69,7 +69,9 @@ protected function countObjects($queryBuilder)
6969
}
7070

7171
/**
72-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::fetchSlice()
72+
* This method should remain in sync with SliceFetcher::fetch until it is deprecated and removed.
73+
*
74+
* {@inheritDoc}
7375
*/
7476
protected function fetchSlice($queryBuilder, $limit, $offset)
7577
{
@@ -103,14 +105,14 @@ protected function fetchSlice($queryBuilder, $limit, $offset)
103105
}
104106

105107
/**
106-
* @see FOS\ElasticaBundle\Doctrine\AbstractProvider::createQueryBuilder()
108+
* {@inheritDoc}
107109
*/
108-
protected function createQueryBuilder()
110+
protected function createQueryBuilder($method)
109111
{
110112
return $this->managerRegistry
111113
->getManagerForClass($this->objectClass)
112114
->getRepository($this->objectClass)
113115
// ORM query builders require an alias argument
114-
->{$this->options['query_builder_method']}(static::ENTITY_ALIAS);
116+
->{$method}(static::ENTITY_ALIAS);
115117
}
116118
}

Doctrine/ORM/SliceFetcher.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
class SliceFetcher implements SliceFetcherInterface
1515
{
1616
/**
17+
* This method should remain in sync with Provider::fetchSlice until that method is deprecated and
18+
* removed.
19+
*
1720
* {@inheritdoc}
1821
*/
1922
public function fetch($queryBuilder, $limit, $offset, array $previousSlice, array $identifierFieldNames)

Exception/AliasIsIndexException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class AliasIsIndexException extends \Exception
66
{
77
public function __construct($indexName)
88
{
9-
parent::__construct(sprintf('Expected alias %s instead of index', $indexName));
9+
parent::__construct(sprintf('Expected %s to be an alias but it is an index.', $indexName));
1010
}
1111
}

0 commit comments

Comments
 (0)