CommonComponentTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. App::uses('CommonComponent', 'Tools.Controller/Component');
  3. App::uses('Component', 'Controller');
  4. App::uses('AppController', 'Controller');
  5. /**
  6. * 2010-11-10 ms
  7. */
  8. class CommonComponentTest extends CakeTestCase {
  9. /**
  10. * setUp method
  11. *
  12. * @access public
  13. * @return void
  14. */
  15. public function setUp() {
  16. $this->Controller = new CommonComponentTestController(new CakeRequest, new CakeResponse);
  17. $this->Controller->constructClasses();
  18. $this->Controller->startupProcess();
  19. }
  20. /**
  21. * Tear-down method. Resets environment state.
  22. *
  23. * @access public
  24. * @return void
  25. */
  26. public function tearDown() {
  27. unset($this->Controller->Common);
  28. unset($this->Controller);
  29. }
  30. public function testLoadHelper() {
  31. $this->assertTrue(!in_array('Text', $this->Controller->helpers));
  32. $this->Controller->Common->loadHelper('Text');
  33. $this->assertTrue(in_array('Text', $this->Controller->helpers));
  34. }
  35. public function testLoadComponent() {
  36. $this->assertTrue(!isset($this->Controller->Test));
  37. $this->Controller->Common->loadComponent('Test');
  38. $this->assertTrue(isset($this->Controller->Test));
  39. # with plugin
  40. $this->Controller->Calendar = null;
  41. $this->assertTrue(!isset($this->Controller->Calendar));
  42. $this->Controller->Common->loadComponent('Tools.Calendar');
  43. $this->assertTrue(isset($this->Controller->Calendar));
  44. # with options
  45. $this->Controller->Test = null;
  46. $this->assertTrue(!isset($this->Controller->Test));
  47. $this->Controller->Common->loadComponent(array('RequestHandler', 'Test'=>array('x'=>'y')));
  48. $this->assertTrue(isset($this->Controller->Test));
  49. $this->assertTrue($this->Controller->Test->isInit);
  50. $this->assertTrue($this->Controller->Test->isStartup);
  51. }
  52. public function testLoadLib() {
  53. $this->assertTrue(!isset($this->Controller->RandomLib));
  54. $this->Controller->Common->loadLib('Tools.RandomLib');
  55. $this->assertTrue(isset($this->Controller->RandomLib));
  56. $res = $this->Controller->RandomLib->pwd(null, 10);
  57. $this->assertTrue(!empty($res));
  58. # with options
  59. $this->assertTrue(!isset($this->Controller->TestLib));
  60. $this->Controller->Common->loadLib(array('Tools.RandomLib', 'TestLib'=>array('x'=>'y')));
  61. $this->assertTrue(isset($this->Controller->TestLib));
  62. $this->assertTrue($this->Controller->TestLib->hasOptions);
  63. }
  64. public function testGetParams() {
  65. $is = $this->Controller->Common->getQueryParam('case');
  66. $this->assertTrue(strpos($is, 'CommonComponent') > 0 || $is === 'AllComponentTests' || $is === 'AllPluginTests');
  67. $is = $this->Controller->Common->getQueryParam('x');
  68. $this->assertSame(null, $is);
  69. $is = $this->Controller->Common->getQueryParam('x', 'y');
  70. $this->assertSame($is, 'y');
  71. $is = $this->Controller->Common->getNamedParam('plugin');
  72. $this->assertSame(null, $is);
  73. $is = $this->Controller->Common->getNamedParam('x');
  74. $this->assertSame(null, $is);
  75. $is = $this->Controller->Common->getNamedParam('x', 'y');
  76. $this->assertSame($is, 'y');
  77. }
  78. public function testGetDefaultUrlParams() {
  79. $is = $this->Controller->Common->defaultUrlParams();
  80. debug($is);
  81. $this->assertNotEmpty($is);
  82. }
  83. public function testTransientFlashMessage() {
  84. $is = $this->Controller->Common->transientFlashMessage('xyz', 'success');
  85. //$this->assertTrue($is);
  86. $res = Configure::read('messages');
  87. debug($res);
  88. $this->assertTrue(!empty($res));
  89. $this->assertTrue(isset($res['success'][0]) && $res['success'][0] === 'xyz');
  90. }
  91. public function testFlashMessage() {
  92. $this->Controller->Session->delete('messages');
  93. $is = $this->Controller->Common->flashMessage('efg');
  94. //$this->assertTrue($is);
  95. $res = $this->Controller->Session->read('messages');
  96. debug($res);
  97. $this->assertTrue(!empty($res));
  98. $this->assertTrue(isset($res['info'][0]) && $res['info'][0] === 'efg');
  99. }
  100. }
  101. /*** additional helper classes ***/
  102. /**
  103. * Short description for class.
  104. *
  105. * @package cake.tests
  106. * @subpackage cake.tests.cases.libs.controller.components
  107. */
  108. class CommonComponentTestController extends AppController {
  109. /**
  110. * name property
  111. *
  112. * @var string 'SecurityTest'
  113. * @access public
  114. */
  115. /**
  116. * components property
  117. *
  118. * @var array
  119. * @access public
  120. */
  121. public $components = array('Tools.Common');
  122. /**
  123. * failed property
  124. *
  125. * @var bool false
  126. * @access public
  127. */
  128. public $failed = false;
  129. /**
  130. * Used for keeping track of headers in test
  131. *
  132. * @var array
  133. * @access public
  134. */
  135. public $testHeaders = array();
  136. /**
  137. * fail method
  138. *
  139. * @access public
  140. * @return void
  141. */
  142. public function fail() {
  143. $this->failed = true;
  144. }
  145. /**
  146. * redirect method
  147. *
  148. * @param mixed $option
  149. * @param mixed $code
  150. * @param mixed $exit
  151. * @access public
  152. * @return void
  153. */
  154. public function redirect($url, $status = null, $exit = true) {
  155. return $status;
  156. }
  157. /**
  158. * Conveinence method for header()
  159. *
  160. * @param string $status
  161. * @return void
  162. * @access public
  163. */
  164. public function header($status) {
  165. $this->testHeaders[] = $status;
  166. }
  167. }
  168. class TestComponent extends Component {
  169. public $Controller;
  170. public $isInit = false;
  171. public $isStartup = false;
  172. public function initialize(Controller $Controller) {
  173. //$this->Controller = $Controller;
  174. $this->isInit = true;
  175. }
  176. public function startup(Controller $Controller) {
  177. //$this->Controller = $Controller;
  178. $this->isStartup = true;
  179. }
  180. }
  181. class TestHelper extends Object {
  182. }
  183. class TestLib {
  184. public $hasOptions = false;
  185. public function __construct($options = array()) {
  186. if (!empty($options)) {
  187. $this->hasOptions = true;
  188. }
  189. }
  190. }