|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the FOSElasticaBundle project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au> |
|
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 FOS\ElasticaBundle\Tests\Provider; |
|
13
|
|
|
|
|
14
|
|
|
use FOS\ElasticaBundle\Provider\Indexable; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
16
|
|
|
|
|
17
|
|
|
class IndexableTest extends \PHPUnit_Framework_TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
public $container; |
|
20
|
|
|
|
|
21
|
|
View Code Duplication |
public function testIndexableUnknown() |
|
|
|
|
|
|
22
|
|
|
{ |
|
23
|
|
|
$indexable = new Indexable(array(), $this->container); |
|
24
|
|
|
$index = $indexable->isObjectIndexable('index', 'type', new Entity()); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertTrue($index); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @dataProvider provideIsIndexableCallbacks |
|
31
|
|
|
*/ |
|
32
|
|
View Code Duplication |
public function testValidIndexableCallbacks($callback, $return) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$indexable = new Indexable(array( |
|
35
|
|
|
'index/type' => $callback, |
|
36
|
|
|
), $this->container); |
|
37
|
|
|
$index = $indexable->isObjectIndexable('index', 'type', new Entity()); |
|
38
|
|
|
|
|
39
|
|
|
$this->assertEquals($return, $index); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @dataProvider provideInvalidIsIndexableCallbacks |
|
44
|
|
|
* @expectedException \InvalidArgumentException |
|
45
|
|
|
*/ |
|
46
|
|
View Code Duplication |
public function testInvalidIsIndexableCallbacks($callback) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$indexable = new Indexable(array( |
|
49
|
|
|
'index/type' => $callback, |
|
50
|
|
|
), $this->container); |
|
51
|
|
|
$indexable->isObjectIndexable('index', 'type', new Entity()); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function provideInvalidIsIndexableCallbacks() |
|
55
|
|
|
{ |
|
56
|
|
|
return array( |
|
57
|
|
|
array('nonexistentEntityMethod'), |
|
58
|
|
|
array(array('@indexableService', 'internalMethod')), |
|
59
|
|
|
array(array(new IndexableDecider(), 'internalMethod')), |
|
60
|
|
|
array(42), |
|
61
|
|
|
array('entity.getIsIndexable() && nonexistentEntityFunction()'), |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
View Code Duplication |
public function testObjectIsNotIndexableIfIndexingDisabled() |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
|
|
$indexable = new Indexable(array(), $this->container); |
|
68
|
|
|
$indexable->setIndexingEnabled(false); |
|
69
|
|
|
$index = $indexable->isObjectIndexable('index', 'type', new Entity()); |
|
70
|
|
|
|
|
71
|
|
|
$this->assertFalse($index); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function provideIsIndexableCallbacks() |
|
75
|
|
|
{ |
|
76
|
|
|
return array( |
|
77
|
|
|
array('isIndexable', false), |
|
78
|
|
|
array(array(new IndexableDecider(), 'isIndexable'), true), |
|
79
|
|
|
array(array('@indexableService', 'isIndexable'), true), |
|
80
|
|
|
array(array('@indexableService'), true), |
|
81
|
|
|
array(function (Entity $entity) { return $entity->maybeIndex(); }, true), |
|
82
|
|
|
array('entity.maybeIndex()', true), |
|
83
|
|
|
array('!object.isIndexable() && entity.property == "abc"', true), |
|
84
|
|
|
array('entity.property != "abc"', false), |
|
85
|
|
|
array('["array", "values"]', true), |
|
86
|
|
|
array('[]', false) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
protected function setUp() |
|
91
|
|
|
{ |
|
92
|
|
|
$this->container = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerInterface') |
|
93
|
|
|
->getMock(); |
|
94
|
|
|
|
|
95
|
|
|
$this->container->expects($this->any()) |
|
96
|
|
|
->method('get') |
|
97
|
|
|
->with('indexableService') |
|
98
|
|
|
->will($this->returnValue(new IndexableDecider())); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
class Entity |
|
103
|
|
|
{ |
|
104
|
|
|
public $property = 'abc'; |
|
105
|
|
|
|
|
106
|
|
|
public function isIndexable() |
|
107
|
|
|
{ |
|
108
|
|
|
return false; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function maybeIndex() |
|
112
|
|
|
{ |
|
113
|
|
|
return true; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
class IndexableDecider |
|
118
|
|
|
{ |
|
119
|
|
|
public function isIndexable(Entity $entity) |
|
120
|
|
|
{ |
|
121
|
|
|
return !$entity->isIndexable(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function internalMethod() |
|
125
|
|
|
{ |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
public function __invoke($object) |
|
129
|
|
|
{ |
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.