PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/tests/lib/cake_test_case.php

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