ObjectTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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
  35. */
  36. public $firstName = 'Joel';
  37. /**
  38. * lastName property
  39. *
  40. * @var string
  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. /**
  138. * Object Test class
  139. *
  140. */
  141. class ObjectTest extends TestCase {
  142. /**
  143. * fixtures
  144. *
  145. * @var string
  146. */
  147. public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
  148. /**
  149. * setUp method
  150. *
  151. * @return void
  152. */
  153. public function setUp() {
  154. parent::setUp();
  155. $this->object = new TestObject();
  156. Configure::write('App.namespace', 'TestApp');
  157. Configure::write('Security.salt', 'not-the-default');
  158. Log::drop('stdout');
  159. Log::drop('stderr');
  160. }
  161. /**
  162. * tearDown method
  163. *
  164. * @return void
  165. */
  166. public function tearDown() {
  167. parent::tearDown();
  168. Plugin::unload();
  169. Log::reset();
  170. unset($this->object);
  171. }
  172. /**
  173. * testLog method
  174. *
  175. * @return void
  176. */
  177. public function testLog() {
  178. if (file_exists(LOGS . 'error.log')) {
  179. unlink(LOGS . 'error.log');
  180. }
  181. $this->assertTrue($this->object->log('Test warning 1'));
  182. $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
  183. $result = file(LOGS . 'error.log');
  184. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
  185. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
  186. $this->assertRegExp('/^\($/', $result[2]);
  187. $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
  188. $this->assertRegExp('/^\)$/', $result[4]);
  189. unlink(LOGS . 'error.log');
  190. $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
  191. $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
  192. $result = file(LOGS . 'error.log');
  193. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
  194. $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: 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. }
  200. /**
  201. * testToString method
  202. *
  203. * @return void
  204. */
  205. public function testToString() {
  206. $result = strtolower($this->object->toString());
  207. $this->assertEquals(strtolower(__NAMESPACE__) . '\testobject', $result);
  208. }
  209. }