/lib/plugins/sfDoctrinePlugin/lib/test/sfTesterDoctrine.class.php

https://github.com/bheneka/gitta · PHP · 177 lines · 122 code · 19 blank · 36 comment · 27 complexity · 3361c3bf20c2b1402955e320fddc664a MD5 · raw file

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