| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- <?php
- /**
- * ObjectTest file
- *
- * PHP 5
- *
- * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
- * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
- * @since 1.2.0
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\App;
- use Cake\Core\Configure;
- use Cake\Core\Object;
- use Cake\Core\Plugin;
- use Cake\Log\Log;
- use Cake\Routing\Router;
- use Cake\TestSuite\TestCase;
- /**
- * TestObject class
- *
- */
- class TestObject extends Object {
- /**
- * firstName property
- *
- * @var string 'Joel'
- */
- public $firstName = 'Joel';
- /**
- * lastName property
- *
- * @var string 'Moss'
- */
- public $lastName = 'Moss';
- /**
- * methodCalls property
- *
- * @var array
- */
- public $methodCalls = array();
- /**
- * emptyMethod method
- *
- * @return void
- */
- public function emptyMethod() {
- $this->methodCalls[] = 'emptyMethod';
- }
- /**
- * oneParamMethod method
- *
- * @param mixed $param
- * @return void
- */
- public function oneParamMethod($param) {
- $this->methodCalls[] = array('oneParamMethod' => array($param));
- }
- /**
- * twoParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @return void
- */
- public function twoParamMethod($param, $paramTwo) {
- $this->methodCalls[] = array('twoParamMethod' => array($param, $paramTwo));
- }
- /**
- * threeParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @return void
- */
- public function threeParamMethod($param, $paramTwo, $paramThree) {
- $this->methodCalls[] = array('threeParamMethod' => array($param, $paramTwo, $paramThree));
- }
- /**
- * fourParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @return void
- */
- public function fourParamMethod($param, $paramTwo, $paramThree, $paramFour) {
- $this->methodCalls[] = array('fourParamMethod' => array($param, $paramTwo, $paramThree, $paramFour));
- }
- /**
- * fiveParamMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @param mixed $paramFive
- * @return void
- */
- public function fiveParamMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive) {
- $this->methodCalls[] = array('fiveParamMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive));
- }
- /**
- * crazyMethod method
- *
- * @param mixed $param
- * @param mixed $paramTwo
- * @param mixed $paramThree
- * @param mixed $paramFour
- * @param mixed $paramFive
- * @param mixed $paramSix
- * @param mixed $paramSeven
- * @return void
- */
- public function crazyMethod($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven = null) {
- $this->methodCalls[] = array('crazyMethod' => array($param, $paramTwo, $paramThree, $paramFour, $paramFive, $paramSix, $paramSeven));
- }
- /**
- * methodWithOptionalParam method
- *
- * @param mixed $param
- * @return void
- */
- public function methodWithOptionalParam($param = null) {
- $this->methodCalls[] = array('methodWithOptionalParam' => array($param));
- }
- /**
- * undocumented function
- *
- * @param array $properties
- * @return void
- */
- public function set($properties = array()) {
- return parent::_set($properties);
- }
- }
- /**
- * Object Test class
- *
- */
- class ObjectTest extends TestCase {
- /**
- * fixtures
- *
- * @var string
- */
- public $fixtures = array('core.post', 'core.test_plugin_comment', 'core.comment');
- /**
- * setUp method
- *
- * @return void
- */
- public function setUp() {
- parent::setUp();
- $this->object = new TestObject();
- Configure::write('App.namespace', 'TestApp');
- Configure::write('Security.salt', 'not-the-default');
- Log::drop('stdout');
- Log::drop('stderr');
- }
- /**
- * tearDown method
- *
- * @return void
- */
- public function tearDown() {
- parent::tearDown();
- Plugin::unload();
- Log::reset();
- unset($this->object);
- }
- /**
- * testLog method
- *
- * @return void
- */
- public function testLog() {
- if (file_exists(LOGS . 'error.log')) {
- unlink(LOGS . 'error.log');
- }
- $this->assertTrue($this->object->log('Test warning 1'));
- $this->assertTrue($this->object->log(array('Test' => 'warning 2')));
- $result = file(LOGS . 'error.log');
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Test warning 1$/', $result[0]);
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Error: Array$/', $result[1]);
- $this->assertRegExp('/^\($/', $result[2]);
- $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
- $this->assertRegExp('/^\)$/', $result[4]);
- unlink(LOGS . 'error.log');
- $this->assertTrue($this->object->log('Test warning 1', LOG_WARNING));
- $this->assertTrue($this->object->log(array('Test' => 'warning 2'), LOG_WARNING));
- $result = file(LOGS . 'error.log');
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Test warning 1$/', $result[0]);
- $this->assertRegExp('/^2[0-9]{3}-[0-9]+-[0-9]+ [0-9]+:[0-9]+:[0-9]+ Warning: Array$/', $result[1]);
- $this->assertRegExp('/^\($/', $result[2]);
- $this->assertRegExp('/\[Test\] => warning 2$/', $result[3]);
- $this->assertRegExp('/^\)$/', $result[4]);
- unlink(LOGS . 'error.log');
- }
- /**
- * testSet method
- *
- * @return void
- */
- public function testSet() {
- $this->object->set('a string');
- $this->assertEquals('Joel', $this->object->firstName);
- $this->object->set(array('firstName'));
- $this->assertEquals('Joel', $this->object->firstName);
- $this->object->set(array('firstName' => 'Ashley'));
- $this->assertEquals('Ashley', $this->object->firstName);
- $this->object->set(array('firstName' => 'Joel', 'lastName' => 'Moose'));
- $this->assertEquals('Joel', $this->object->firstName);
- $this->assertEquals('Moose', $this->object->lastName);
- }
- /**
- * testToString method
- *
- * @return void
- */
- public function testToString() {
- $result = strtolower($this->object->toString());
- $this->assertEquals(strtolower(__NAMESPACE__) . '\testobject', $result);
- }
- /**
- * testMethodDispatching method
- *
- * @return void
- */
- public function testMethodDispatching() {
- $this->object->emptyMethod();
- $expected = array('emptyMethod');
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->oneParamMethod('Hello');
- $expected[] = array('oneParamMethod' => array('Hello'));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->twoParamMethod(true, false);
- $expected[] = array('twoParamMethod' => array(true, false));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->threeParamMethod(true, false, null);
- $expected[] = array('threeParamMethod' => array(true, false, null));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->crazyMethod(1, 2, 3, 4, 5, 6, 7);
- $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object = new TestObject();
- $this->assertSame($this->object->methodCalls, array());
- $this->object->dispatchMethod('emptyMethod');
- $expected = array('emptyMethod');
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('oneParamMethod', array('Hello'));
- $expected[] = array('oneParamMethod' => array('Hello'));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('twoParamMethod', array(true, false));
- $expected[] = array('twoParamMethod' => array(true, false));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('threeParamMethod', array(true, false, null));
- $expected[] = array('threeParamMethod' => array(true, false, null));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('fourParamMethod', array(1, 2, 3, 4));
- $expected[] = array('fourParamMethod' => array(1, 2, 3, 4));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('fiveParamMethod', array(1, 2, 3, 4, 5));
- $expected[] = array('fiveParamMethod' => array(1, 2, 3, 4, 5));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('crazyMethod', array(1, 2, 3, 4, 5, 6, 7));
- $expected[] = array('crazyMethod' => array(1, 2, 3, 4, 5, 6, 7));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('methodWithOptionalParam', array('Hello'));
- $expected[] = array('methodWithOptionalParam' => array("Hello"));
- $this->assertSame($this->object->methodCalls, $expected);
- $this->object->dispatchMethod('methodWithOptionalParam');
- $expected[] = array('methodWithOptionalParam' => array(null));
- $this->assertSame($this->object->methodCalls, $expected);
- }
- }
|