PageRenderTime 125ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/Croogo/Lib/TestSuite/CroogoControllerTestCase.php

https://github.com/kareypowell/croogo
PHP | 133 lines | 85 code | 14 blank | 34 comment | 5 complexity | 506a4f5e0d6a3a3c3d6cff2ab90c9c53 MD5 | raw file
  1. <?php
  2. App::uses('CakeSession', 'Model/Datasource');
  3. App::uses('CroogoTestFixture', 'Croogo.TestSuite');
  4. /**
  5. * CroogoTestCase class
  6. *
  7. * @category TestSuite
  8. * @package Croogo
  9. * @version 1.4
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @author Rachman Chavik <rchavik@gmail.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class CroogoControllerTestCase extends ControllerTestCase {
  16. public static function setUpBeforeClass() {
  17. self::_restoreSettings();
  18. Configure::write('Config.language', 'eng');
  19. }
  20. public static function tearDownAfterClass() {
  21. self::_restoreSettings();
  22. Configure::write('Config.language', Configure::read('Site.locale'));
  23. }
  24. protected static function _restoreSettings() {
  25. $configDir = CakePlugin::path('Croogo') . 'Test' . DS . 'test_app' . DS . 'Config' . DS;
  26. $source = $configDir . 'settings.default';
  27. $target = $configDir . 'settings.json';
  28. copy($source, $target);
  29. }
  30. /**
  31. * setUp
  32. *
  33. * @return void
  34. */
  35. public function setUp() {
  36. parent::setUp();
  37. $appDir = CakePlugin::path('Croogo') . 'Test' . DS . 'test_app' . DS;
  38. App::build(array(
  39. 'Plugin' => array($appDir . 'Plugin' . DS),
  40. 'View' => array($appDir . 'View' . DS),
  41. ), App::PREPEND);
  42. if (!isset($_SERVER['REMOTE_ADDR'])) {
  43. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  44. }
  45. CakePlugin::unload('Install');
  46. CakePlugin::load(array('Users'), array('bootstrap' => true));
  47. CakePlugin::load('Example');
  48. Configure::write('Acl.database', 'test');
  49. $Setting = ClassRegistry::init('Settings.Setting');
  50. $Setting->settingsPath = $appDir . 'Config' . DS . 'settings.json';
  51. Configure::drop('settings');
  52. Configure::config('settings', new CroogoJsonReader(dirname($Setting->settingsPath) . DS ));
  53. CakeLog::drop('stdout');
  54. CakeLog::drop('stderr');
  55. $Setting->writeConfiguration();
  56. }
  57. /**
  58. * tearDown
  59. *
  60. * @return void
  61. */
  62. public function tearDown() {
  63. parent::tearDown();
  64. if (CakeSession::started()) {
  65. CakeSession::clear();
  66. CakeSession::destroy();
  67. }
  68. ClassRegistry::flush();
  69. }
  70. /**
  71. * authUserCallback
  72. *
  73. * @param type $key
  74. * @return mixed
  75. */
  76. public function authUserCallback($key) {
  77. $auth = array(
  78. 'id' => 1,
  79. 'username' => 'admin',
  80. 'role_id' => 1,
  81. 'name' => 'Administrator',
  82. 'email' => 'you@your-site.com',
  83. 'website' => '/about'
  84. );
  85. if (empty($key) || !isset($auth[$key])) {
  86. return $auth;
  87. }
  88. return $auth[$key];
  89. }
  90. /**
  91. * Helper to expect a Session->setFlash and redirect
  92. *
  93. * @param string $message expected message that will be passed to setFlash()
  94. * @param string $class class name, when null current class will be used
  95. * @param array $flashOptions expected SessionComponent::setFlash arguments
  96. */
  97. public function expectFlashAndRedirect($message = '', $class = false, $flashOptions = array()) {
  98. if (!$class) {
  99. $class = substr(get_class($this), 0, -4);
  100. }
  101. $flashOptions = Hash::merge(array(
  102. 'element' => 'default',
  103. 'params' => array(
  104. 'class' => 'success',
  105. ),
  106. ), $flashOptions);
  107. $this->{$class}->Session
  108. ->expects($this->once())
  109. ->method('setFlash')
  110. ->with(
  111. $this->equalTo($message),
  112. $this->equalTo($flashOptions['element']),
  113. $this->equalTo($flashOptions['params'])
  114. );
  115. $this->{$class}
  116. ->expects($this->once())
  117. ->method('redirect');
  118. }
  119. }