1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Petkopara\MultiSearchBundle\Tests; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Annotations\IndexedReader; |
7
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
8
|
|
|
use Doctrine\ORM\Configuration; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; |
11
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
12
|
|
|
use Petkopara\MultiSearchBundle\Condition\EntityConditionBuilder; |
13
|
|
|
use Petkopara\MultiSearchBundle\Condition\FormConditionBuilder; |
14
|
|
|
use PHPUnit_Framework_TestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Test of EntityCondition class |
18
|
|
|
* |
19
|
|
|
* @author Petkov Petkov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class EntityConditionTest extends PHPUnit_Framework_TestCase |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
protected static $em; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @return EntityManager |
28
|
|
|
*/ |
29
|
|
|
protected static function getEntityManager() |
30
|
|
|
{ |
31
|
|
|
if (null === self::$em) { |
32
|
|
|
// 'path' is commented out but can be very helpful |
33
|
|
|
// if used instead of 'memory' for debugging |
34
|
|
|
$connection = array( |
35
|
|
|
'driver' => 'pdo_sqlite', |
36
|
|
|
'memory' => true, |
37
|
|
|
// 'path' => 'database.sqlite', |
|
|
|
|
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
$cache = new ArrayCache; |
41
|
|
|
$config = new Configuration(); |
42
|
|
|
$config->setMetadataCacheImpl($cache); |
43
|
|
|
$config->setQueryCacheImpl($cache); |
44
|
|
|
$config->setResultCacheImpl($cache); |
45
|
|
|
$config->setProxyDir(sys_get_temp_dir()); |
46
|
|
|
$config->setProxyNamespace('DoctrineProxies'); |
47
|
|
|
$config->setAutoGenerateProxyClasses(true); |
48
|
|
|
$config->setMetadataDriverImpl(new AnnotationDriver( |
49
|
|
|
new IndexedReader(new AnnotationReader()), __DIR__ |
50
|
|
|
)); |
51
|
|
|
|
52
|
|
|
// $config->setSQLLogger(new EchoSQLLogger()); |
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
self::$em = EntityManager::create($connection, $config); |
56
|
|
|
|
57
|
|
|
$schemaTool = new SchemaTool(self::$em); |
58
|
|
|
|
59
|
|
|
$cmf = self::$em->getMetadataFactory(); |
60
|
|
|
$classes = $cmf->getAllMetadata(); |
61
|
|
|
|
62
|
|
|
$schemaTool->createSchema($classes); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return static::$em; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testEntityBuilder() |
69
|
|
|
{ |
70
|
|
|
$queryBuilder = self::getEntityManager()->createQueryBuilder(); |
71
|
|
|
$entityName = __NAMESPACE__ . '\\Post'; |
72
|
|
|
|
73
|
|
|
$queryBuilder->from($entityName, 'p'); |
74
|
|
|
$searchTerm = 'search test'; |
75
|
|
|
$searchFields = array('name'); |
76
|
|
|
$comparisonType = 'wildcard'; |
77
|
|
|
|
78
|
|
|
$conditionBuilder = new EntityConditionBuilder($queryBuilder, $entityName, $searchTerm, $searchFields, $comparisonType); |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
$resultQuery = $conditionBuilder->getQueryBuilderWithConditions(); |
82
|
|
|
|
83
|
|
|
$this->assertEquals( |
84
|
|
|
"SELECT p FROM $entityName p WHERE p.id IN(SELECT c.id FROM $entityName c WHERE c.name LIKE ?2 AND c.id IN(SELECT b.id FROM $entityName b WHERE b.name LIKE ?1))", $resultQuery->getDQL() |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->assertEquals('%search%', $resultQuery->getParameter(1)->getValue()); |
88
|
|
|
$this->assertEquals('%test%', $resultQuery->getParameter(2)->getValue()); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testFormBuilder() |
92
|
|
|
{ |
93
|
|
|
$queryBuilder = self::getEntityManager()->createQueryBuilder(); |
94
|
|
|
$entityName = __NAMESPACE__ . '\\Post'; |
95
|
|
|
|
96
|
|
|
$queryBuilder->from($entityName, 'p'); |
97
|
|
|
|
98
|
|
|
$searchTerm = 'search test'; |
99
|
|
|
$searchFields = array('name'); |
100
|
|
|
$comparisonType = 'wildcard'; |
101
|
|
|
|
102
|
|
|
$form = $this->getMockBuilder('Symfony\Component\Form\Form') |
103
|
|
|
->disableOriginalConstructor() |
104
|
|
|
->setMethods(array('getData', 'getConfig', 'getOption')) |
105
|
|
|
->getMock(); |
106
|
|
|
$form->expects($this->any())->method('getData')->will($this->returnValue($searchTerm)); |
107
|
|
|
$form->expects($this->any())->method('getOption') |
108
|
|
|
->with($this->logicalOr( |
109
|
|
|
$this->equalTo('search_comparison_type'), $this->equalTo('class'), $this->equalTo('search_fields') |
110
|
|
|
)) |
111
|
|
|
->will($this->returnCallback(function($param) use ($entityName, $comparisonType, $searchFields) { |
112
|
|
|
if ($param === 'search_comparison_type') { |
113
|
|
|
return $comparisonType; |
114
|
|
|
} |
115
|
|
|
if ($param === 'class') { |
116
|
|
|
return $entityName; |
117
|
|
|
} |
118
|
|
|
if ($param === 'search_fields') { |
119
|
|
|
return $searchFields; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
)); |
123
|
|
|
|
124
|
|
|
$form->expects($this->any()) |
125
|
|
|
->method($this->anything()) // all other calls return self |
126
|
|
|
->will($this->returnSelf()); |
127
|
|
|
|
128
|
|
|
|
129
|
|
|
$conditionBuilder = new FormConditionBuilder($queryBuilder, $form); |
130
|
|
|
|
131
|
|
|
$resultQuery = $conditionBuilder->getQueryBuilderWithConditions(); |
132
|
|
|
|
133
|
|
|
$this->assertEquals( |
134
|
|
|
"SELECT p FROM $entityName p WHERE p.id IN(SELECT c.id FROM $entityName c WHERE c.name LIKE ?2 AND c.id IN(SELECT b.id FROM $entityName b WHERE b.name LIKE ?1))", $resultQuery->getDQL() |
135
|
|
|
); |
136
|
|
|
|
137
|
|
|
$this->assertEquals('%search%', $resultQuery->getParameter(1)->getValue()); |
138
|
|
|
$this->assertEquals('%test%', $resultQuery->getParameter(2)->getValue()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.