[FEATURE] Add several testcases for validators
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Validation / Helper.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Validation;
3 /***************************************************************
4 * Copyright notice
5 *
6 * (c) 2012 Andreas Lappe <a.lappe@kuehlhaus.com>, kuehlhaus AG
7 *
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Small helper class to DRY the code.
29 *
30 * @author Andreas Lappe <a.lappe@kuehlhaus.com>
31 * @package TYPO3
32 * @subpackage form
33 */
34 class Helper extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
35 /**
36 * @var array
37 */
38 protected $mockData = array();
39
40 /**
41 * Helper method to get a mock of a requestHandler returning
42 * defined values if asked.
43 *
44 * @param array $data $key => $value pairs to return if asked for key
45 * @return \TYPO3\CMS\Form\Request|\PHPUnit_Framework_MockObject_MockObject
46 */
47 public function getRequestHandler(array $data) {
48 $this->mockData = $data;
49
50 $requestHandlerMock = $this->getMock('TYPO3\\CMS\\Form\\Request', array('has', 'getByMethod'));
51 $requestHandlerMock->expects($this->any())->method('has')->will($this->returnCallback(array($this, 'has')));
52 $requestHandlerMock->expects($this->any())->method('getByMethod')
53 ->will($this->returnCallback(array($this, 'getByMethod')));
54
55 return $requestHandlerMock;
56 }
57
58 /**
59 * Callback for \TYPO3\CMS\Form\Request::getByMethod.
60 *
61 * Returns the value stored for $key
62 *
63 * @param string $key the key of the value to retrieve, must not be empty
64 * @return mixed the stored value for $key or FALSE if there is no value for $key stored
65 */
66 public function getByMethod($key) {
67 if (!$this->has($key)) {
68 return FALSE;
69 }
70
71 return $this->mockData[$key];
72 }
73
74 /**
75 * Callback for tx_form_System_Request::has.
76 *
77 * Checks whether a value for $key has been stored.
78 *
79 * @param string $key the key to check, must not be empty
80 * @return boolean whether a value for $key has been stored.
81 */
82 public function has($key) {
83 return isset($this->mockData[$key]);
84 }
85 }
86 ?>