TYPO3 CMS  TYPO3_6-2
ArgumentsTest.php
Go to the documentation of this file.
1 <?php
3 
21 
26  $arguments1 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
27  $arguments2 = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
28  $this->assertNotSame($arguments1, $arguments2, 'The arguments object is not of scope prototype!');
29  }
30 
34  public function addingAnArgumentManuallyWorks() {
35  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
36  $newArgument = new \TYPO3\CMS\Extbase\Mvc\Controller\Argument('argumentName1234', 'dummyValue');
37  $arguments->addArgument($newArgument);
38  $this->assertSame($newArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
39  }
40 
45  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
46  $mockFirstArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
47  $mockFirstArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
48  $arguments->addArgument($mockFirstArgument);
49  $mockSecondArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
50  $mockSecondArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
51  $arguments->addArgument($mockSecondArgument);
52  $this->assertSame($mockSecondArgument, $arguments->getArgument('argumentName1234'), 'The added and retrieved argument is not the same.');
53  }
54 
59  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array(), array(), '', FALSE);
60  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
61  $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
62  $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
63  $arguments->_set('objectManager', $mockObjectManager);
64  $newArgument = $arguments->addNewArgument('someArgument');
65  $this->assertSame($newArgument, $mockArgument);
66  }
67 
72  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
73  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
74  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
75  $arguments[] = $mockArgument;
76  $this->assertTrue($arguments->hasArgument('argumentName1234'), 'Added argument does not exist.');
77  $this->assertSame($mockArgument, $arguments->getArgument('argumentName1234'), 'Added and retrieved arguments are not the same.');
78  }
79 
84  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
85  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
86  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
87  $arguments[] = $mockArgument;
88  $this->assertSame($mockArgument, $arguments['argumentName1234'], 'Argument retrieved by array access is not the one we added.');
89  }
90 
95  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
96  try {
97  $arguments->getArgument('someArgument');
98  $this->fail('getArgument() did not throw an exception although the specified argument does not exist.');
99  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\NoSuchArgumentException $exception) {
100  }
101  }
102 
106  public function issetReturnsCorrectResult() {
107  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
108  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('argumentName1234'));
109  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
110  $this->assertFalse(isset($arguments['argumentName1234']), 'isset() did not return FALSE.');
111  $arguments[] = $mockArgument;
112  $this->assertTrue(isset($arguments['argumentName1234']), 'isset() did not return TRUE.');
113  }
114 
119  $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
120  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
121  $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
122  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
123  $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
124  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
125  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
126  $arguments[] = $mockArgument1;
127  $arguments[] = $mockArgument2;
128  $arguments[] = $mockArgument3;
129  $expectedArgumentNames = array('argumentName1', 'argumentName2', 'argumentName3');
130  $this->assertEquals($expectedArgumentNames, $arguments->getArgumentNames(), 'Returned argument names were not as expected.');
131  }
132 
137  $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
138  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
139  $mockArgument1->expects($this->any())->method('getShortName')->will($this->returnValue('a'));
140  $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
141  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
142  $mockArgument2->expects($this->any())->method('getShortName')->will($this->returnValue('b'));
143  $mockArgument3 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
144  $mockArgument3->expects($this->any())->method('getName')->will($this->returnValue('argumentName3'));
145  $mockArgument3->expects($this->any())->method('getShortName')->will($this->returnValue('c'));
146  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
147  $arguments[] = $mockArgument1;
148  $arguments[] = $mockArgument2;
149  $arguments[] = $mockArgument3;
150  $expectedShortNames = array('a', 'b', 'c');
151  $this->assertEquals($expectedShortNames, $arguments->getArgumentShortNames(), 'Returned argument short names were not as expected.');
152  }
153 
158  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
159  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
160  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
161  $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument')->will($this->returnValue($mockArgument));
162  $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
163  $arguments->_set('objectManager', $mockObjectManager);
164  $addedArgument = $arguments->addNewArgument('dummyName');
165  $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', $addedArgument, 'addNewArgument() either did not add a new argument or did not return it.');
166  $retrievedArgument = $arguments['dummyName'];
167  $this->assertSame($addedArgument, $retrievedArgument, 'The added and the retrieved argument are not the same.');
168  }
169 
174  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName'), array(), '', FALSE);
175  $mockArgument->expects($this->any())->method('getName')->will($this->returnValue('dummyName'));
176  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
177  $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
178  $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
179  $arguments->_set('objectManager', $mockObjectManager);
180  $arguments->addNewArgument('dummyName');
181  }
182 
187  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired'), array(), '', FALSE);
188  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
189  $mockArgument->expects($this->once())->method('setRequired')->with(TRUE);
190  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
191  $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
192  $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
193  $arguments->_set('objectManager', $mockObjectManager);
194  $arguments->addNewArgument('dummyName', 'Text', TRUE);
195  }
196 
201  $mockArgument = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'setRequired', 'setDefaultValue'), array(), '', FALSE);
202  $mockArgument->expects($this->once())->method('getName')->will($this->returnValue('dummyName'));
203  $mockArgument->expects($this->once())->method('setRequired')->with(FALSE);
204  $mockArgument->expects($this->once())->method('setDefaultValue')->with('someDefaultValue');
205  $mockObjectManager = $this->getMock('TYPO3\\CMS\\Extbase\\Object\\ObjectManagerInterface');
206  $mockObjectManager->expects($this->once())->method('get')->with('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', 'dummyName', 'Text')->will($this->returnValue($mockArgument));
207  $arguments = $this->getAccessibleMock('TYPO3\CMS\Extbase\Mvc\Controller\Arguments', array('dummy'));
208  $arguments->_set('objectManager', $mockObjectManager);
209  $arguments->addNewArgument('dummyName', 'Text', FALSE, 'someDefaultValue');
210  }
211 
218  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
219  $arguments->nonExistingMethod();
220  }
221 
226  public function removeAllClearsAllArguments() {
227  $mockArgument1 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
228  $mockArgument1->expects($this->any())->method('getName')->will($this->returnValue('argumentName1'));
229  $mockArgument2 = $this->getMock('TYPO3\\CMS\\Extbase\\Mvc\\Controller\\Argument', array('getName', 'getShortName'), array(), '', FALSE);
230  $mockArgument2->expects($this->any())->method('getName')->will($this->returnValue('argumentName2'));
231  $arguments = new \TYPO3\CMS\Extbase\Mvc\Controller\Arguments();
232  $arguments[] = $mockArgument1;
233  $arguments[] = $mockArgument2;
234  $this->assertTrue($arguments->hasArgument('argumentName2'));
235  $arguments->removeAll();
236  $this->assertFalse($arguments->hasArgument('argumentName2'));
237  }
238 }
getAccessibleMock( $originalClassName, array $methods=array(), array $arguments=array(), $mockClassName='', $callOriginalConstructor=TRUE, $callOriginalClone=TRUE, $callAutoload=TRUE)