CakeTestModel.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice
  8. *
  9. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @package Cake.TestSuite.Fixture
  12. * @since CakePHP(tm) v 1.2.0.4667
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. App::uses('Model', 'Model');
  16. /**
  17. * A model to extend from to help you during testing.
  18. *
  19. * @package Cake.TestSuite.Fixture
  20. */
  21. class CakeTestModel extends Model {
  22. public $useDbConfig = 'test';
  23. public $cacheSources = false;
  24. /**
  25. * Sets default order for the model to avoid failing tests caused by
  26. * incorrect order when no order has been defined in the finds.
  27. * Postgres can return the results in any order it considers appropriate if none is specified
  28. *
  29. * @param array $queryData
  30. * @return array $queryData
  31. */
  32. public function beforeFind($queryData) {
  33. $pk = $this->primaryKey;
  34. $aliasedPk = $this->alias . '.' . $this->primaryKey;
  35. switch (true) {
  36. case !$pk:
  37. case !$this->useTable:
  38. case !$this->schema('id'):
  39. case !empty($queryData['order'][0]):
  40. case !empty($queryData['group']):
  41. case
  42. (is_string($queryData['fields']) && !($queryData['fields'] == $pk || $queryData['fields'] == $aliasedPk)) ||
  43. (is_array($queryData['fields']) && !(array_key_exists($pk, $queryData['fields']) || array_key_exists($aliasedPk, $queryData['fields']))):
  44. break;
  45. default:
  46. $queryData['order'] = array($this->alias . '.' . $this->primaryKey => 'ASC');
  47. break;
  48. }
  49. return $queryData;
  50. }
  51. /**
  52. * Overriding save() to set CakeTestSuiteDispatcher::date() as formatter for created, modified and updated fields
  53. *
  54. * @param array $data
  55. * @param boolean|array $validate
  56. * @param array $fieldList
  57. */
  58. public function save($data = null, $validate = true, $fieldList = array()) {
  59. $db = $this->getDataSource();
  60. $db->columns['datetime']['formatter'] = 'CakeTestSuiteDispatcher::date';
  61. return parent::save($data, $validate, $fieldList);
  62. }
  63. }