PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/lib/cake_test_case.php

https://bitbucket.org/webpolis/hurli
PHP | 835 lines | 478 code | 86 blank | 271 comment | 105 complexity | 32fe8978b18c9c85385608ff050af8a2 MD5 | raw file
  1. <?php
  2. /**
  3. * CakeTestCase file
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/view/1196/Testing>
  8. * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The Open Group Test Suite License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/view/1196/Testing CakePHP(tm) Tests
  15. * @package cake
  16. * @subpackage cake.cake.tests.libs
  17. * @since CakePHP(tm) v 1.2.0.4667
  18. * @license http://www.opensource.org/licenses/opengroup.php The Open Group Test Suite License
  19. */
  20. if (!class_exists('dispatcher')) {
  21. require CAKE . 'dispatcher.php';
  22. }
  23. require_once CAKE_TESTS_LIB . 'cake_test_model.php';
  24. require_once CAKE_TESTS_LIB . 'cake_test_fixture.php';
  25. App::import('Vendor', 'simpletest' . DS . 'unit_tester');
  26. /**
  27. * CakeTestDispatcher
  28. *
  29. * @package cake
  30. * @subpackage cake.cake.tests.lib
  31. */
  32. class CakeTestDispatcher extends Dispatcher {
  33. /**
  34. * controller property
  35. *
  36. * @var Controller
  37. * @access public
  38. */
  39. var $controller;
  40. var $testCase;
  41. /**
  42. * testCase method
  43. *
  44. * @param CakeTestCase $testCase
  45. * @return void
  46. * @access public
  47. */
  48. function testCase(&$testCase) {
  49. $this->testCase =& $testCase;
  50. }
  51. /**
  52. * invoke method
  53. *
  54. * @param Controller $controller
  55. * @param array $params
  56. * @param boolean $missingAction
  57. * @return Controller
  58. * @access protected
  59. */
  60. function _invoke(&$controller, $params, $missingAction = false) {
  61. $this->controller =& $controller;
  62. if (array_key_exists('layout', $params)) {
  63. $this->controller->layout = $params['layout'];
  64. }
  65. if (isset($this->testCase) && method_exists($this->testCase, 'startController')) {
  66. $this->testCase->startController($this->controller, $params);
  67. }
  68. $result = parent::_invoke($this->controller, $params, $missingAction);
  69. if (isset($this->testCase) && method_exists($this->testCase, 'endController')) {
  70. $this->testCase->endController($this->controller, $params);
  71. }
  72. return $result;
  73. }
  74. }
  75. /**
  76. * CakeTestCase class
  77. *
  78. * @package cake
  79. * @subpackage cake.cake.tests.lib
  80. */
  81. class CakeTestCase extends UnitTestCase {
  82. /**
  83. * Methods used internally.
  84. *
  85. * @var array
  86. * @access public
  87. */
  88. var $methods = array('start', 'end', 'startcase', 'endcase', 'starttest', 'endtest');
  89. /**
  90. * By default, all fixtures attached to this class will be truncated and reloaded after each test.
  91. * Set this to false to handle manually
  92. *
  93. * @var array
  94. * @access public
  95. */
  96. var $autoFixtures = true;
  97. /**
  98. * Set this to false to avoid tables to be dropped if they already exist
  99. *
  100. * @var boolean
  101. * @access public
  102. */
  103. var $dropTables = true;
  104. /**
  105. * Maps fixture class names to fixture identifiers as included in CakeTestCase::$fixtures
  106. *
  107. * @var array
  108. * @access protected
  109. */
  110. var $_fixtureClassMap = array();
  111. /**
  112. * truncated property
  113. *
  114. * @var boolean
  115. * @access private
  116. */
  117. var $__truncated = true;
  118. /**
  119. * savedGetData property
  120. *
  121. * @var array
  122. * @access private
  123. */
  124. var $__savedGetData = array();
  125. /**
  126. * Called when a test case (group of methods) is about to start (to be overriden when needed.)
  127. *
  128. * @param string $method Test method about to get executed.
  129. * @return void
  130. * @access public
  131. */
  132. function startCase() {
  133. }
  134. /**
  135. * Called when a test case (group of methods) has been executed (to be overriden when needed.)
  136. *
  137. * @param string $method Test method about that was executed.
  138. * @return void
  139. * @access public
  140. */
  141. function endCase() {
  142. }
  143. /**
  144. * Called when a test case method is about to start (to be overriden when needed.)
  145. *
  146. * @param string $method Test method about to get executed.
  147. * @return void
  148. * @access public
  149. */
  150. function startTest($method) {
  151. }
  152. /**
  153. * Called when a test case method has been executed (to be overriden when needed.)
  154. *
  155. * @param string $method Test method about that was executed.
  156. * @return void
  157. * @access public
  158. */
  159. function endTest($method) {
  160. }
  161. /**
  162. * Overrides SimpleTestCase::assert to enable calling of skipIf() from within tests
  163. *
  164. * @param Expectation $expectation
  165. * @param mixed $compare
  166. * @param string $message
  167. * @return boolean|null
  168. * @access public
  169. */
  170. function assert(&$expectation, $compare, $message = '%s') {
  171. if ($this->_should_skip) {
  172. return;
  173. }
  174. return parent::assert($expectation, $compare, $message);
  175. }
  176. /**
  177. * Overrides SimpleTestCase::skipIf to provide a boolean return value
  178. *
  179. * @param boolean $shouldSkip
  180. * @param string $message
  181. * @return boolean
  182. * @access public
  183. */
  184. function skipIf($shouldSkip, $message = '%s') {
  185. parent::skipIf($shouldSkip, $message);
  186. return $shouldSkip;
  187. }
  188. /**
  189. * Callback issued when a controller's action is about to be invoked through testAction().
  190. *
  191. * @param Controller $controller Controller that's about to be invoked.
  192. * @param array $params Additional parameters as sent by testAction().
  193. * @return void
  194. * @access public
  195. */
  196. function startController(&$controller, $params = array()) {
  197. if (isset($params['fixturize']) && ((is_array($params['fixturize']) && !empty($params['fixturize'])) || $params['fixturize'] === true)) {
  198. if (!isset($this->db)) {
  199. $this->_initDb();
  200. }
  201. if ($controller->uses === false) {
  202. $list = array($controller->modelClass);
  203. } else {
  204. $list = is_array($controller->uses) ? $controller->uses : array($controller->uses);
  205. }
  206. $models = array();
  207. ClassRegistry::config(array('ds' => $params['connection']));
  208. foreach ($list as $name) {
  209. if ((is_array($params['fixturize']) && in_array($name, $params['fixturize'])) || $params['fixturize'] === true) {
  210. if (class_exists($name) || App::import('Model', $name)) {
  211. $object =& ClassRegistry::init($name);
  212. //switch back to specified datasource.
  213. $object->setDataSource($params['connection']);
  214. $db =& ConnectionManager::getDataSource($object->useDbConfig);
  215. $db->cacheSources = false;
  216. $models[$object->alias] = array(
  217. 'table' => $object->table,
  218. 'model' => $object->alias,
  219. 'key' => strtolower($name),
  220. );
  221. }
  222. }
  223. }
  224. ClassRegistry::config(array('ds' => 'test_suite'));
  225. if (!empty($models) && isset($this->db)) {
  226. $this->_actionFixtures = array();
  227. foreach ($models as $model) {
  228. $fixture =& new CakeTestFixture($this->db);
  229. $fixture->name = $model['model'] . 'Test';
  230. $fixture->table = $model['table'];
  231. $fixture->import = array('model' => $model['model'], 'records' => true);
  232. $fixture->init();
  233. $fixture->create($this->db);
  234. $fixture->insert($this->db);
  235. $this->_actionFixtures[] =& $fixture;
  236. }
  237. foreach ($models as $model) {
  238. $object =& ClassRegistry::getObject($model['key']);
  239. if ($object !== false) {
  240. $object->setDataSource('test_suite');
  241. $object->cacheSources = false;
  242. }
  243. }
  244. }
  245. }
  246. }
  247. /**
  248. * Callback issued when a controller's action has been invoked through testAction().
  249. *
  250. * @param Controller $controller Controller that has been invoked.
  251. * @param array $params Additional parameters as sent by testAction().
  252. * @return void
  253. * @access public
  254. */
  255. function endController(&$controller, $params = array()) {
  256. if (isset($this->db) && isset($this->_actionFixtures) && !empty($this->_actionFixtures) && $this->dropTables) {
  257. foreach ($this->_actionFixtures as $fixture) {
  258. $fixture->drop($this->db);
  259. }
  260. }
  261. }
  262. /**
  263. * Executes a Cake URL, and can get (depending on the $params['return'] value):
  264. *
  265. * Params:
  266. * - 'return' has several possible values:
  267. * 1. 'result': Whatever the action returns (and also specifies $this->params['requested'] for controller)
  268. * 2. 'view': The rendered view, without the layout
  269. * 3. 'contents': The rendered view, within the layout.
  270. * 4. 'vars': the view vars
  271. *
  272. * - 'fixturize' - Set to true if you want to copy model data from 'connection' to the test_suite connection
  273. * - 'data' - The data you want to insert into $this->data in the controller.
  274. * - 'connection' - Which connection to use in conjunction with fixturize (defaults to 'default')
  275. * - 'method' - What type of HTTP method to simulate (defaults to post)
  276. *
  277. * @param string $url Cake URL to execute (e.g: /articles/view/455)
  278. * @param mixed $params Parameters (see above), or simply a string of what to return
  279. * @return mixed Whatever is returned depending of requested result
  280. * @access public
  281. */
  282. function testAction($url, $params = array()) {
  283. $default = array(
  284. 'return' => 'result',
  285. 'fixturize' => false,
  286. 'data' => array(),
  287. 'method' => 'post',
  288. 'connection' => 'default'
  289. );
  290. if (is_string($params)) {
  291. $params = array('return' => $params);
  292. }
  293. $params = array_merge($default, $params);
  294. $toSave = array(
  295. 'case' => null,
  296. 'group' => null,
  297. 'app' => null,
  298. 'output' => null,
  299. 'show' => null,
  300. 'plugin' => null
  301. );
  302. $this->__savedGetData = (empty($this->__savedGetData))
  303. ? array_intersect_key($_GET, $toSave)
  304. : $this->__savedGetData;
  305. $data = (!empty($params['data'])) ? $params['data'] : array();
  306. if (strtolower($params['method']) == 'get') {
  307. $_GET = array_merge($this->__savedGetData, $data);
  308. $_POST = array();
  309. } else {
  310. $_POST = array('data' => $data);
  311. $_GET = $this->__savedGetData;
  312. }
  313. $return = $params['return'];
  314. $params = array_diff_key($params, array('data' => null, 'method' => null, 'return' => null));
  315. $dispatcher =& new CakeTestDispatcher();
  316. $dispatcher->testCase($this);
  317. if ($return != 'result') {
  318. if ($return != 'contents') {
  319. $params['layout'] = false;
  320. }
  321. ob_start();
  322. @$dispatcher->dispatch($url, $params);
  323. $result = ob_get_clean();
  324. if ($return == 'vars') {
  325. $view =& ClassRegistry::getObject('view');
  326. $viewVars = $view->getVars();
  327. $result = array();
  328. foreach ($viewVars as $var) {
  329. $result[$var] = $view->getVar($var);
  330. }
  331. if (!empty($view->pageTitle)) {
  332. $result = array_merge($result, array('title' => $view->pageTitle));
  333. }
  334. }
  335. } else {
  336. $params['return'] = 1;
  337. $params['bare'] = 1;
  338. $params['requested'] = 1;
  339. $result = @$dispatcher->dispatch($url, $params);
  340. }
  341. if (isset($this->_actionFixtures)) {
  342. unset($this->_actionFixtures);
  343. }
  344. ClassRegistry::flush();
  345. return $result;
  346. }
  347. /**
  348. * Announces the start of a test.
  349. *
  350. * @param string $method Test method just started.
  351. * @return void
  352. * @access public
  353. */
  354. function before($method) {
  355. parent::before($method);
  356. if (isset($this->fixtures) && (!is_array($this->fixtures) || empty($this->fixtures))) {
  357. unset($this->fixtures);
  358. }
  359. // Set up DB connection
  360. if (isset($this->fixtures) && strtolower($method) == 'start') {
  361. $this->_initDb();
  362. $this->_loadFixtures();
  363. }
  364. // Create records
  365. if (isset($this->_fixtures) && isset($this->db) && !in_array(strtolower($method), array('start', 'end')) && $this->__truncated && $this->autoFixtures == true) {
  366. foreach ($this->_fixtures as $fixture) {
  367. $inserts = $fixture->insert($this->db);
  368. }
  369. }
  370. if (!in_array(strtolower($method), $this->methods)) {
  371. $this->startTest($method);
  372. }
  373. }
  374. /**
  375. * Runs as first test to create tables.
  376. *
  377. * @return void
  378. * @access public
  379. */
  380. function start() {
  381. if (isset($this->_fixtures) && isset($this->db)) {
  382. Configure::write('Cache.disable', true);
  383. $cacheSources = $this->db->cacheSources;
  384. $this->db->cacheSources = false;
  385. $sources = $this->db->listSources();
  386. $this->db->cacheSources = $cacheSources;
  387. if (!$this->dropTables) {
  388. return;
  389. }
  390. foreach ($this->_fixtures as $fixture) {
  391. $table = $this->db->config['prefix'] . $fixture->table;
  392. if (in_array($table, $sources)) {
  393. $fixture->drop($this->db);
  394. $fixture->create($this->db);
  395. } elseif (!in_array($table, $sources)) {
  396. $fixture->create($this->db);
  397. }
  398. }
  399. }
  400. }
  401. /**
  402. * Runs as last test to drop tables.
  403. *
  404. * @return void
  405. * @access public
  406. */
  407. function end() {
  408. if (isset($this->_fixtures) && isset($this->db)) {
  409. if ($this->dropTables) {
  410. foreach (array_reverse($this->_fixtures) as $fixture) {
  411. $fixture->drop($this->db);
  412. }
  413. }
  414. $this->db->sources(true);
  415. Configure::write('Cache.disable', false);
  416. }
  417. if (class_exists('ClassRegistry')) {
  418. ClassRegistry::flush();
  419. }
  420. }
  421. /**
  422. * Announces the end of a test.
  423. *
  424. * @param string $method Test method just finished.
  425. * @return void
  426. * @access public
  427. */
  428. function after($method) {
  429. $isTestMethod = !in_array(strtolower($method), array('start', 'end'));
  430. if (isset($this->_fixtures) && isset($this->db) && $isTestMethod) {
  431. foreach ($this->_fixtures as $fixture) {
  432. $fixture->truncate($this->db);
  433. }
  434. $this->__truncated = true;
  435. } else {
  436. $this->__truncated = false;
  437. }
  438. if (!in_array(strtolower($method), $this->methods)) {
  439. $this->endTest($method);
  440. }
  441. $this->_should_skip = false;
  442. parent::after($method);
  443. }
  444. /**
  445. * Gets a list of test names. Normally that will be all internal methods that start with the
  446. * name "test". This method should be overridden if you want a different rule.
  447. *
  448. * @return array List of test names.
  449. * @access public
  450. */
  451. function getTests() {
  452. return array_merge(
  453. array('start', 'startCase'),
  454. array_diff(parent::getTests(), array('testAction', 'testaction')),
  455. array('endCase', 'end')
  456. );
  457. }
  458. /**
  459. * Chooses which fixtures to load for a given test
  460. *
  461. * @param string $fixture Each parameter is a model name that corresponds to a
  462. * fixture, i.e. 'Post', 'Author', etc.
  463. * @return void
  464. * @access public
  465. * @see CakeTestCase::$autoFixtures
  466. */
  467. function loadFixtures() {
  468. $args = func_get_args();
  469. foreach ($args as $class) {
  470. if (isset($this->_fixtureClassMap[$class])) {
  471. $fixture = $this->_fixtures[$this->_fixtureClassMap[$class]];
  472. $fixture->truncate($this->db);
  473. $fixture->insert($this->db);
  474. } else {
  475. trigger_error(sprintf(__('Referenced fixture class %s not found', true), $class), E_USER_WARNING);
  476. }
  477. }
  478. }
  479. /**
  480. * Takes an array $expected and generates a regex from it to match the provided $string.
  481. * Samples for $expected:
  482. *
  483. * Checks for an input tag with a name attribute (contains any non-empty value) and an id
  484. * attribute that contains 'my-input':
  485. * array('input' => array('name', 'id' => 'my-input'))
  486. *
  487. * Checks for two p elements with some text in them:
  488. * array(
  489. * array('p' => true),
  490. * 'textA',
  491. * '/p',
  492. * array('p' => true),
  493. * 'textB',
  494. * '/p'
  495. * )
  496. *
  497. * You can also specify a pattern expression as part of the attribute values, or the tag
  498. * being defined, if you prepend the value with preg: and enclose it with slashes, like so:
  499. * array(
  500. * array('input' => array('name', 'id' => 'preg:/FieldName\d+/')),
  501. * 'preg:/My\s+field/'
  502. * )
  503. *
  504. * Important: This function is very forgiving about whitespace and also accepts any
  505. * permutation of attribute order. It will also allow whitespaces between specified tags.
  506. *
  507. * @param string $string An HTML/XHTML/XML string
  508. * @param array $expected An array, see above
  509. * @param string $message SimpleTest failure output string
  510. * @return boolean
  511. * @access public
  512. */
  513. function assertTags($string, $expected, $fullDebug = false) {
  514. $regex = array();
  515. $normalized = array();
  516. foreach ((array) $expected as $key => $val) {
  517. if (!is_numeric($key)) {
  518. $normalized[] = array($key => $val);
  519. } else {
  520. $normalized[] = $val;
  521. }
  522. }
  523. $i = 0;
  524. foreach ($normalized as $tags) {
  525. if (!is_array($tags)) {
  526. $tags = (string)$tags;
  527. }
  528. $i++;
  529. if (is_string($tags) && $tags{0} == '<') {
  530. $tags = array(substr($tags, 1) => array());
  531. } elseif (is_string($tags)) {
  532. $tagsTrimmed = preg_replace('/\s+/m', '', $tags);
  533. if (preg_match('/^\*?\//', $tags, $match) && $tagsTrimmed !== '//') {
  534. $prefix = array(null, null);
  535. if ($match[0] == '*/') {
  536. $prefix = array('Anything, ', '.*?');
  537. }
  538. $regex[] = array(
  539. sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))),
  540. sprintf('%s<[\s]*\/[\s]*%s[\s]*>[\n\r]*', $prefix[1], substr($tags, strlen($match[0]))),
  541. $i,
  542. );
  543. continue;
  544. }
  545. if (!empty($tags) && preg_match('/^preg\:\/(.+)\/$/i', $tags, $matches)) {
  546. $tags = $matches[1];
  547. $type = 'Regex matches';
  548. } else {
  549. $tags = preg_quote($tags, '/');
  550. $type = 'Text equals';
  551. }
  552. $regex[] = array(
  553. sprintf('%s "%s"', $type, $tags),
  554. $tags,
  555. $i,
  556. );
  557. continue;
  558. }
  559. foreach ($tags as $tag => $attributes) {
  560. $regex[] = array(
  561. sprintf('Open %s tag', $tag),
  562. sprintf('[\s]*<%s', preg_quote($tag, '/')),
  563. $i,
  564. );
  565. if ($attributes === true) {
  566. $attributes = array();
  567. }
  568. $attrs = array();
  569. $explanations = array();
  570. $i = 1;
  571. foreach ($attributes as $attr => $val) {
  572. if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
  573. $attrs[] = $matches[1];
  574. $explanations[] = sprintf('Regex "%s" matches', $matches[1]);
  575. continue;
  576. } else {
  577. $quotes = '["\']';
  578. if (is_numeric($attr)) {
  579. $attr = $val;
  580. $val = '.+?';
  581. $explanations[] = sprintf('Attribute "%s" present', $attr);
  582. } elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) {
  583. $quotes = '["\']?';
  584. $val = $matches[1];
  585. $explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val);
  586. } else {
  587. $explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val);
  588. $val = preg_quote($val, '/');
  589. }
  590. $attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes;
  591. }
  592. $i++;
  593. }
  594. if ($attrs) {
  595. $permutations = $this->__array_permute($attrs);
  596. $permutationTokens = array();
  597. foreach ($permutations as $permutation) {
  598. $permutationTokens[] = implode('', $permutation);
  599. }
  600. $regex[] = array(
  601. sprintf('%s', implode(', ', $explanations)),
  602. $permutationTokens,
  603. $i,
  604. );
  605. }
  606. $regex[] = array(
  607. sprintf('End %s tag', $tag),
  608. '[\s]*\/?[\s]*>[\n\r]*',
  609. $i,
  610. );
  611. }
  612. }
  613. foreach ($regex as $i => $assertation) {
  614. list($description, $expressions, $itemNum) = $assertation;
  615. $matches = false;
  616. foreach ((array)$expressions as $expression) {
  617. if (preg_match(sprintf('/^%s/s', $expression), $string, $match)) {
  618. $matches = true;
  619. $string = substr($string, strlen($match[0]));
  620. break;
  621. }
  622. }
  623. if (!$matches) {
  624. $this->assert(new TrueExpectation(), false, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $i, $description));
  625. if ($fullDebug) {
  626. debug($string, true);
  627. debug($regex, true);
  628. }
  629. return false;
  630. }
  631. }
  632. return $this->assert(new TrueExpectation(), true, '%s');
  633. }
  634. /**
  635. * Initialize DB connection.
  636. *
  637. * @return void
  638. * @access protected
  639. */
  640. function _initDb() {
  641. $testDbAvailable = in_array('test', array_keys(ConnectionManager::enumConnectionObjects()));
  642. $_prefix = null;
  643. if ($testDbAvailable) {
  644. // Try for test DB
  645. restore_error_handler();
  646. @$db =& ConnectionManager::getDataSource('test');
  647. set_error_handler('simpleTestErrorHandler');
  648. $testDbAvailable = $db->isConnected();
  649. }
  650. // Try for default DB
  651. if (!$testDbAvailable) {
  652. $db =& ConnectionManager::getDataSource('default');
  653. $_prefix = $db->config['prefix'];
  654. $db->config['prefix'] = 'test_suite_';
  655. }
  656. ConnectionManager::create('test_suite', $db->config);
  657. $db->config['prefix'] = $_prefix;
  658. // Get db connection
  659. $this->db =& ConnectionManager::getDataSource('test_suite');
  660. $this->db->cacheSources = false;
  661. ClassRegistry::config(array('ds' => 'test_suite'));
  662. }
  663. /**
  664. * Load fixtures specified in var $fixtures.
  665. *
  666. * @return void
  667. * @access protected
  668. */
  669. function _loadFixtures() {
  670. if (!isset($this->fixtures) || empty($this->fixtures)) {
  671. return;
  672. }
  673. if (!is_array($this->fixtures)) {
  674. $this->fixtures = array_map('trim', explode(',', $this->fixtures));
  675. }
  676. $this->_fixtures = array();
  677. foreach ($this->fixtures as $index => $fixture) {
  678. $fixtureFile = null;
  679. if (strpos($fixture, 'core.') === 0) {
  680. $fixture = substr($fixture, strlen('core.'));
  681. foreach (App::core('cake') as $key => $path) {
  682. $fixturePaths[] = $path . 'tests' . DS . 'fixtures';
  683. }
  684. } elseif (strpos($fixture, 'app.') === 0) {
  685. $fixture = substr($fixture, strlen('app.'));
  686. $fixturePaths = array(
  687. TESTS . 'fixtures',
  688. VENDORS . 'tests' . DS . 'fixtures'
  689. );
  690. } elseif (strpos($fixture, 'plugin.') === 0) {
  691. $parts = explode('.', $fixture, 3);
  692. $pluginName = $parts[1];
  693. $fixture = $parts[2];
  694. $fixturePaths = array(
  695. App::pluginPath($pluginName) . 'tests' . DS . 'fixtures',
  696. TESTS . 'fixtures',
  697. VENDORS . 'tests' . DS . 'fixtures'
  698. );
  699. } else {
  700. $fixturePaths = array(
  701. TESTS . 'fixtures',
  702. VENDORS . 'tests' . DS . 'fixtures',
  703. TEST_CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'tests' . DS . 'fixtures'
  704. );
  705. }
  706. foreach ($fixturePaths as $path) {
  707. if (is_readable($path . DS . $fixture . '_fixture.php')) {
  708. $fixtureFile = $path . DS . $fixture . '_fixture.php';
  709. break;
  710. }
  711. }
  712. if (isset($fixtureFile)) {
  713. require_once($fixtureFile);
  714. $fixtureClass = Inflector::camelize($fixture) . 'Fixture';
  715. $this->_fixtures[$this->fixtures[$index]] =& new $fixtureClass($this->db);
  716. $this->_fixtureClassMap[Inflector::camelize($fixture)] = $this->fixtures[$index];
  717. }
  718. }
  719. if (empty($this->_fixtures)) {
  720. unset($this->_fixtures);
  721. }
  722. }
  723. /**
  724. * Generates all permutation of an array $items and returns them in a new array.
  725. *
  726. * @param array $items An array of items
  727. * @return array
  728. * @access private
  729. */
  730. function __array_permute($items, $perms = array()) {
  731. static $permuted;
  732. if (empty($perms)) {
  733. $permuted = array();
  734. }
  735. if (empty($items)) {
  736. $permuted[] = $perms;
  737. } else {
  738. $numItems = count($items) - 1;
  739. for ($i = $numItems; $i >= 0; --$i) {
  740. $newItems = $items;
  741. $newPerms = $perms;
  742. list($tmp) = array_splice($newItems, $i, 1);
  743. array_unshift($newPerms, $tmp);
  744. $this->__array_permute($newItems, $newPerms);
  745. }
  746. return $permuted;
  747. }
  748. }
  749. }