ObjectTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * ObjectTest file
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  15. * @since 1.2.0
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. namespace Cake\Test\TestCase\Core;
  19. use Cake\Core\App;
  20. use Cake\Core\Configure;
  21. use Cake\Core\Object;
  22. use Cake\Core\Plugin;
  23. use Cake\Log\Log;
  24. use Cake\Routing\Router;
  25. use Cake\TestSuite\TestCase;
  26. /**
  27. * TestObject class
  28. *
  29. */
  30. class TestObject extends Object {
  31. /**
  32. * firstName property
  33. *
  34. * @var string 'Joel'
  35. */
  36. public $firstName = 'Joel';
  37. /**
  38. * lastName property
  39. *
  40. * @var string 'Moss'
  41. */
  42. public $lastName = 'Moss';
  43. /**
  44. * methodCalls property
  45. *
  46. * @var array
  47. */
  48. public $methodCalls = array();
  49. /**
  50. * emptyMethod method
  51. *
  52. * @return void
  53. */
  54. public function emptyMethod() {
  55. $this->methodCalls[] = 'emptyMethod';
  56. }
  57. /**
  58. * oneParamMethod method
  59. *
  60. * @param mixed $param
  61. * @return void
  62. */
  63. public function oneParamMethod($param) {
  64. $this->methodCalls[] = array('oneParamMethod' => array($param));
  65. }
  66. /**
  67. * twoParamMethod method
  68. *
  69. * @param mixed $param
  70. * @param mixed $paramTwo
  71. * @return void
  72. */
  73. public function twoParamMethod($param, $paramTwo) {
  74. $this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
  75. }
  76. /**
  77. * threeParamMethod method
  78. *
  79. * @param mixed $param
  80. * @param mixed $paramTwo
  81. * @param mixed $paramThree
  82. * @return void
  83. */
  84. public function threeParamMethod($param, $paramTwo, $paramThree) {
  85. $this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
  86. }
  87. /**
  88. * fourParamMethod method
  89. *
  90. * @param mixed $param
  91. * @param mixed $paramTwo
  92. * @param mixed $paramThree
  93. * @param mixed $paramFour
  94. * @return void
  95. */
  96. public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
  97. $this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
  98. }
  99. /**
  100. * fiveParamMethod method
  101. *
  102. * @param mixed $param
  103. * @param mixed $paramTwo
  104. * @param mixed $paramThree
  105. * @param mixed $paramFour
  106. * @param mixed $paramFive
  107. * @return void
  108. */
  109. public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
  110. $this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
  111. }
  112. /**
  113. * crazyMethod method
  114. *
  115. * @param mixed $param
  116. * @param mixed $paramTwo
  117. * @param mixed $paramThree
  118. * @param mixed $paramFour
  119. * @param mixed $paramFive
  120. * @param mixed $paramSix
  121. * @param mixed $paramSeven
  122. * @return void
  123. */
  124. public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
  125. $this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
  126. }
  127. /**
  128. * methodWithOptionalParam method
  129. *
  130. * @param mixed $param
  131. * @return void
  132. */
  133. public function methodWithOptionalParam($param = null) {
  134. $this->methodCalls[] = array('methodWithOptionalParam' => array($param));
  135. }
  136. /**
  137. * undocumented function
  138. *
  139. * @param array $properties
  140. * @return void
  141. */
  142. public function set($properties = array()) {
  143. return parent::_set($properties);
  144. }
  145. }
  146. /**
  147. * Object Test class
  148. *
  149. */
  150. class ObjectTest extends TestCase {
  151. /**
  152. * fixtures
  153. *
  154. * @var string
  155. */
  156. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  157. /**
  158. * setUp method
  159. *
  160. * @return void
  161. */
  162. public function setUp() {
  163. parent::setUp();
  164. $this->object = new TestObject();
  165. Configure::write('App.namespace', 'TestApp');
  166. Configure::write('Security.salt', 'not-the-default');
  167. Log::drop('stdout');
  168. Log::drop('stderr');
  169. }
  170. /**
  171. * tearDown method
  172. *
  173. * @return void
  174. */
  175. public function tearDown() {
  176. parent::tearDown();
  177. Plugin::unload();
  178. Log::reset();
  179. unset($this->object);
  180. }
  181. /**
  182. * testLog method
  183. *
  184. * @return void
  185. */
  186. public function testLog() {
  187. if (file_exists(LOGS . 'error.log')) {
  188. unlink(LOGS . 'error.log');
  189. }
  190. $this->assertTrue($this->object->log('Test warning 1'));
  191. $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
  192. $result = file(LOGS . 'error.log');
  193. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
  194. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
  195. $this->assertRegExp('/^\($/', $result[2]);
  196. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  197. $this->assertRegExp('/^\)$/', $result[4]);
  198. unlink(LOGS . 'error.log');
  199. $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
  200. $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
  201. $result = file(LOGS . 'error.log');
  202. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
  203. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
  204. $this->assertRegExp('/^\($/', $result[2]);
  205. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  206. $this->assertRegExp('/^\)$/', $result[4]);
  207. unlink(LOGS . 'error.log');
  208. }
  209. /**
  210. * testSet method
  211. *
  212. * @return void
  213. */
  214. public function testSet() {
  215. $this->object->set('a string');
  216. $this->assertEquals('Joel', $this->object->firstName);
  217. $this->object->set(array('firstName'));
  218. $this->assertEquals('Joel', $this->object->firstName);
  219. $this->object->set(array('firstName' => 'Ashley'));
  220. $this->assertEquals('Ashley', $this->object->firstName);
  221. $this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
  222. $this->assertEquals('Joel', $this->object->firstName);
  223. $this->assertEquals('Moose', $this->object->lastName);
  224. }
  225. /**
  226. * testToString method
  227. *
  228. * @return void
  229. */
  230. public function testToString() {
  231. $result = strtolower($this->object->toString());
  232. $this->assertEquals(strtolower(__NAMESPACE__) . '\testobject', $result);
  233. }
  234. /**
  235. * testMethodDispatching method
  236. *
  237. * @return void
  238. */
  239. public function testMethodDispatching() {
  240. $this->object->emptyMethod();
  241. $expected = array('emptyMethod');
  242. $this->assertSame($this->object->methodCalls, $expected);
  243. $this->object->oneParamMethod('Hello');
  244. $expected[] = array('oneParamMethod' => array('Hello'));
  245. $this->assertSame($this->object->methodCalls, $expected);
  246. $this->object->twoParamMethod(true, false);
  247. $expected[] = array('twoParamMethod' => array(true, false));
  248. $this->assertSame($this->object->methodCalls, $expected);
  249. $this->object->threeParamMethod(true, false, null);
  250. $expected[] = array('threeParamMethod' => array(true, false, null));
  251. $this->assertSame($this->object->methodCalls, $expected);
  252. $this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
  253. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  254. $this->assertSame($this->object->methodCalls, $expected);
  255. $this->object = new TestObject();
  256. $this->assertSame($this->object->methodCalls, array());
  257. $this->object->dispatchMethod('emptyMethod');
  258. $expected = array('emptyMethod');
  259. $this->assertSame($this->object->methodCalls, $expected);
  260. $this->object->dispatchMethod('oneParamMethod', array('Hello'));
  261. $expected[] = array('oneParamMethod' => array('Hello'));
  262. $this->assertSame($this->object->methodCalls, $expected);
  263. $this->object->dispatchMethod('twoParamMethod', array(true, false));
  264. $expected[] = array('twoParamMethod' => array(true, false));
  265. $this->assertSame($this->object->methodCalls, $expected);
  266. $this->object->dispatchMethod('threeParamMethod', array(true, false, null));
  267. $expected[] = array('threeParamMethod' => array(true, false, null));
  268. $this->assertSame($this->object->methodCalls, $expected);
  269. $this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
  270. $expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
  271. $this->assertSame($this->object->methodCalls, $expected);
  272. $this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
  273. $expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
  274. $this->assertSame($this->object->methodCalls, $expected);
  275. $this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
  276. $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
  277. $this->assertSame($this->object->methodCalls, $expected);
  278. $this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
  279. $expected[] = array('methodWithOptionalParam' => array("Hello"));
  280. $this->assertSame($this->object->methodCalls, $expected);
  281. $this->object->dispatchMethod('methodWithOptionalParam');
  282. $expected[] = array('methodWithOptionalParam' => array(null));
  283. $this->assertSame($this->object->methodCalls, $expected);
  284. }
  285. }