TYPO3 API  SVNRelease
ArgumentTest.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
00006 *  All rights reserved
00007 *
00008 *  This class is a backport of the corresponding class of FLOW3.
00009 *  All credits go to the v5 team.
00010 *
00011 *  This script is part of the TYPO3 project. The TYPO3 project is
00012 *  free software; you can redistribute it and/or modify
00013 *  it under the terms of the GNU General Public License as published by
00014 *  the Free Software Foundation; either version 2 of the License, or
00015 *  (at your option) any later version.
00016 *
00017 *  The GNU General Public License can be found at
00018 *  http://www.gnu.org/copyleft/gpl.html.
00019 *
00020 *  This script is distributed in the hope that it will be useful,
00021 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 *  GNU General Public License for more details.
00024 *
00025 *  This copyright notice MUST APPEAR in all copies of the script!
00026 ***************************************************************/
00027 
00028 class Tx_Extbase_Tests_Unit_MVC_Controller_ArgumentTest extends Tx_Extbase_Tests_Unit_BaseTestCase {
00029 
00030     /**
00031      * @test
00032      * @expectedException InvalidArgumentException
00033      */
00034     public function constructingArgumentWithoutNameThrowsException() {
00035         new Tx_Extbase_MVC_Controller_Argument(NULL, 'Text');
00036     }
00037 
00038     /**
00039      * @test
00040      * @expectedException InvalidArgumentException
00041      */
00042     public function constructingArgumentWithInvalidNameThrowsException() {
00043         new Tx_Extbase_MVC_Controller_Argument(new ArrayObject(), 'Text');
00044     }
00045 
00046     /**
00047      * @test
00048      */
00049     public function passingDataTypeToConstructorReallySetsTheDataType() {
00050         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Number');
00051         $this->assertEquals('Number', $argument->getDataType(), 'The specified data type has not been set correctly.');
00052     }
00053 
00054     /**
00055      * @test
00056      */
00057     public function setShortNameProvidesFluentInterface() {
00058         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
00059         $returnedArgument = $argument->setShortName('x');
00060         $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
00061     }
00062 
00063     /**
00064      * @test
00065      */
00066     public function setValueProvidesFluentInterface() {
00067         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
00068         $returnedArgument = $argument->setValue('x');
00069         $this->assertSame($argument, $returnedArgument, 'The returned argument is not the original argument.');
00070     }
00071 
00072     /**
00073      * @test
00074      */
00075     public function setValueTriesToConvertAnUidIntoTheRealObjectIfTheDataTypeClassSchemaIsSet() {
00076         $object = new StdClass();
00077 
00078         $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('findObjectByUid'), array(), '', FALSE);
00079         $argument->expects($this->once())->method('findObjectByUid')->with('42')->will($this->returnValue($object));
00080         $argument->_set('dataTypeClassSchema', 'stdClass');
00081         $argument->_set('dataType', 'stdClass');
00082         // $argument->_set('queryFactory', $mockQueryFactory);
00083         $argument->setValue('42');
00084 
00085         $this->assertSame($object, $argument->_get('value'));
00086         $this->assertSame(Tx_Extbase_MVC_Controller_Argument::ORIGIN_PERSISTENCE, $argument->getOrigin());
00087     }
00088 
00089 
00090     /**
00091      * @test
00092      */
00093     public function toStringReturnsTheStringVersionOfTheArgumentsValue() {
00094         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
00095         $argument->setValue(123);
00096 
00097         $this->assertSame((string)$argument, '123', 'The returned argument is not a string.');
00098         $this->assertNotSame((string)$argument, 123, 'The returned argument is identical to the set value.');
00099     }
00100 
00101     /**
00102      * @test
00103      */
00104     public function dataTypeValidatorCanBeAFullClassName() {
00105         $this->markTestIncomplete();
00106 
00107         $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
00108         $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
00109 
00110         $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Tx_Extbase_Validation_Validator_TextValidator');
00111         $argument->injectObjectManager($this->mockObjectManager);
00112 
00113         $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
00114     }
00115 
00116     /**
00117      * @test
00118      */
00119     public function dataTypeValidatorCanBeAShortName() {
00120         $this->markTestIncomplete();
00121 
00122         $this->mockObjectManager->expects($this->once())->method('isObjectRegistered')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue(TRUE));
00123         $this->mockObjectManager->expects($this->any())->method('get')->with('Tx_Extbase_Validation_Validator_TextValidator')->will($this->returnValue($this->getMock('Tx_Extbase_Validation_Validator_TextValidator')));
00124 
00125         $argument = new Tx_Extbase_MVC_Controller_Argument('SomeArgument', 'Text');
00126         $argument->injectObjectManager($this->mockObjectManager);
00127 
00128         $this->assertType('Tx_Extbase_Validation_Validator_TextValidator', $argument->getDatatypeValidator(), 'The returned datatype validator is not a text validator as expected.');
00129     }
00130 
00131     /**
00132      * @test
00133      */
00134     public function setNewValidatorConjunctionCreatesANewValidatorConjunctionObject() {
00135         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
00136         $mockConjunctionValidator = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator');
00137         $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
00138         $mockObjectManager->expects($this->once())->method('create')->with('Tx_Extbase_Validation_Validator_ConjunctionValidator')->will($this->returnValue($mockConjunctionValidator));
00139         $argument->injectObjectManager($mockObjectManager);
00140         $argument->setNewValidatorConjunction(array());
00141         $this->assertSame($mockConjunctionValidator, $argument->getValidator());
00142     }
00143 
00144     /**
00145      * @test
00146      */
00147     public function setNewValidatorConjunctionAddsThePassedValidatorsToTheCreatedValidatorChain() {
00148         eval('class Validator1 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
00149             public function isValid($value) {}
00150             public function setOptions(array $validationOptions) {}
00151             public function getErrors() {}
00152         }');
00153         eval('class Validator2 implements Tx_Extbase_Validation_Validator_ValidatorInterface {
00154             public function isValid($value) {}
00155             public function setOptions(array $validationOptions) {}
00156             public function getErrors() {}
00157         }');
00158 
00159         $validator1 = new Validator1;
00160         $validator2 = new Validator2;
00161 
00162         $mockValidatorConjunction = $this->getMock('Tx_Extbase_Validation_Validator_ConjunctionValidator');
00163         $mockValidatorConjunction->expects($this->at(0))->method('addValidator')->with($validator1);
00164         $mockValidatorConjunction->expects($this->at(1))->method('addValidator')->with($validator2);
00165 
00166         $argument = $this->getMock($this->buildAccessibleProxy('Tx_Extbase_MVC_Controller_Argument'), array('dummy'), array(), '', FALSE);
00167         $mockObjectManager = $this->getMock('Tx_Extbase_Object_ObjectManagerInterface');
00168         $mockObjectManager->expects($this->never())->method('create');
00169         $mockObjectManager->expects($this->at(0))->method('get')->with('Validator1')->will($this->returnValue($validator1));
00170         $mockObjectManager->expects($this->at(1))->method('get')->with('Validator2')->will($this->returnValue($validator2));
00171         $argument->injectObjectManager($mockObjectManager);
00172         $argument->_set('validator', $mockValidatorConjunction);
00173         $argument->setNewValidatorConjunction(array('Validator1', 'Validator2'));
00174     }
00175 
00176     /**
00177      * @test
00178      */
00179     public function settingDefaultValueReallySetsDefaultValue() {
00180         $argument = new Tx_Extbase_MVC_Controller_Argument('dummy', 'Text');
00181         $argument->setDefaultValue(42);
00182 
00183         $this->assertEquals(42, $argument->getValue(), 'The default value was not stored in the Argument.');
00184     }
00185 
00186 }
00187 ?>