| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
*
|
|
|
7 |
* For the full copyright and license information, please view the LICENSE
|
|
|
8 |
* file that was distributed with this source code.
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
/**
|
|
|
12 |
* sfTesterDoctrine implements tests for Doctrine classes.
|
|
|
13 |
*
|
|
|
14 |
* @package symfony
|
|
|
15 |
* @subpackage test
|
|
|
16 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
17 |
* @version SVN: $Id: sfTesterDoctrine.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
|
|
|
18 |
*/
|
|
|
19 |
class sfTesterDoctrine extends sfTester
|
|
|
20 |
{
|
|
|
21 |
/**
|
|
|
22 |
* Prepares the tester.
|
|
|
23 |
*/
|
|
|
24 |
public function prepare()
|
|
|
25 |
{
|
|
|
26 |
}
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Initializes the tester.
|
|
|
30 |
*/
|
|
|
31 |
public function initialize()
|
|
|
32 |
{
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* Tests a model.
|
|
|
37 |
*
|
|
|
38 |
* @param string $model The model class name
|
|
|
39 |
* @param array|Doctrine_Query $query A Doctrine_Query object or an array of conditions
|
|
|
40 |
* @param string $value The value to test
|
|
|
41 |
*
|
|
|
42 |
* @return sfTestFunctionalBase|sfTester
|
|
|
43 |
*/
|
|
|
44 |
public function check($model, $query, $value = true)
|
|
|
45 |
{
|
|
|
46 |
if (null === $query)
|
|
|
47 |
{
|
|
|
48 |
$query = Doctrine_Core::getTable($model)
|
|
|
49 |
->createQuery('a');
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
if (is_array($query))
|
|
|
53 |
{
|
|
|
54 |
$conditions = $query;
|
|
|
55 |
$query = $query = Doctrine_Core::getTable($model)
|
|
|
56 |
->createQuery('a');
|
|
|
57 |
foreach ($conditions as $column => $condition)
|
|
|
58 |
{
|
|
|
59 |
$column = Doctrine_Core::getTable($model)->getFieldName($column);
|
|
|
60 |
|
|
|
61 |
if (null === $condition)
|
|
|
62 |
{
|
|
|
63 |
$query->andWhere('a.'.$column.' IS NULL');
|
|
|
64 |
continue;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
$operator = '=';
|
|
|
68 |
if ('!' == $condition[0])
|
|
|
69 |
{
|
|
|
70 |
$operator = false !== strpos($condition, '%') ? 'NOT LIKE' : '!=';
|
|
|
71 |
$condition = substr($condition, 1);
|
|
|
72 |
}
|
|
|
73 |
else if (false !== strpos($condition, '%'))
|
|
|
74 |
{
|
|
|
75 |
$operator = 'LIKE';
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
$query->andWhere('a.' . $column . ' ' . $operator . ' ?', $condition);
|
|
|
79 |
}
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
$objects = $query->execute();
|
|
|
83 |
|
|
|
84 |
if (false === $value)
|
|
|
85 |
{
|
|
|
86 |
$this->tester->is(count($objects), 0, sprintf('no %s object that matches the criteria has been found', $model));
|
|
|
87 |
}
|
|
|
88 |
else if (true === $value)
|
|
|
89 |
{
|
|
|
90 |
$this->tester->cmp_ok(count($objects), '>', 0, sprintf('%s objects that matches the criteria have been found', $model));
|
|
|
91 |
}
|
|
|
92 |
else if (is_int($value))
|
|
|
93 |
{
|
|
|
94 |
$this->tester->is(count($objects), $value, sprintf('"%s" %s objects have been found', $value, $model));
|
|
|
95 |
}
|
|
|
96 |
else
|
|
|
97 |
{
|
|
|
98 |
throw new InvalidArgumentException('The "check()" method does not takes this kind of argument.');
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
return $this->getObjectToReturn();
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Outputs some debug information about queries run during the current request.
|
|
|
106 |
*
|
|
|
107 |
* @param integer|string $limit Either an integer to return the last many queries, a regular expression or a substring to search for
|
|
|
108 |
*/
|
|
|
109 |
public function debug($limit = null)
|
|
|
110 |
{
|
|
|
111 |
if (!$databaseManager = $this->browser->getContext()->getDatabaseManager())
|
|
|
112 |
{
|
|
|
113 |
throw new LogicConnection('The current context does not include a database manager.');
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
$events = array();
|
|
|
117 |
foreach ($databaseManager->getNames() as $name)
|
|
|
118 |
{
|
|
|
119 |
$database = $databaseManager->getDatabase($name);
|
|
|
120 |
if ($database instanceof sfDoctrineDatabase && $profiler = $database->getProfiler())
|
|
|
121 |
{
|
|
|
122 |
foreach ($profiler->getQueryExecutionEvents() as $event)
|
|
|
123 |
{
|
|
|
124 |
$events[$event->getSequence()] = $event;
|
|
|
125 |
}
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
|
|
|
129 |
// sequence events
|
|
|
130 |
ksort($events);
|
|
|
131 |
|
|
|
132 |
if (is_integer($limit))
|
|
|
133 |
{
|
|
|
134 |
$events = array_slice($events, $limit * -1);
|
|
|
135 |
}
|
|
|
136 |
else if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $limit, $match))
|
|
|
137 |
{
|
|
|
138 |
if ($match[1] == '!')
|
|
|
139 |
{
|
|
|
140 |
$pattern = substr($limit, 1);
|
|
|
141 |
$match = false;
|
|
|
142 |
}
|
|
|
143 |
else
|
|
|
144 |
{
|
|
|
145 |
$pattern = $limit;
|
|
|
146 |
$match = true;
|
|
|
147 |
}
|
|
|
148 |
}
|
|
|
149 |
else if ($limit)
|
|
|
150 |
{
|
|
|
151 |
$substring = $limit;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
echo "\nDumping SQL executed in the current context:\n\n";
|
|
|
155 |
|
|
|
156 |
foreach ($events as $event)
|
|
|
157 |
{
|
|
|
158 |
if (
|
|
|
159 |
(!isset($pattern) && !isset($substring))
|
|
|
160 |
||
|
|
|
161 |
(isset($pattern) && $match == preg_match($pattern, $event->getQuery()))
|
|
|
162 |
||
|
|
|
163 |
(isset($substring) && false !== stripos($event->getQuery(), $substring))
|
|
|
164 |
)
|
|
|
165 |
{
|
|
|
166 |
$conn = $event->getInvoker() instanceof Doctrine_Connection ? $event->getInvoker() : $event->getInvoker()->getConnection();
|
|
|
167 |
|
|
|
168 |
echo $event->getQuery()."\n";
|
|
|
169 |
echo ' Parameters: '.sfYaml::dump(sfDoctrineConnectionProfiler::fixParams($event->getParams()), 0)."\n";
|
|
|
170 |
echo ' Connection: '.$conn->getName()."\n";
|
|
|
171 |
echo ' Time: '.number_format($event->getElapsedSecs(), 2)."s\n\n";
|
|
|
172 |
}
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
exit(1);
|
|
|
176 |
}
|
|
|
177 |
}
|