Fixed bug #9779: t3lib_div::trimExplode limit parameter leads to wrong result with...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_div_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Ingo Renner <ingo@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25
26 /**
27 * Testcase for class t3lib_div
28 *
29 * @author Ingo Renner <ingo@typo3.org>
30 * @package TYPO3
31 * @subpackage t3lib
32 */
33 class t3lib_div_testcase extends tx_phpunit_testcase {
34
35 /**
36 * @test
37 */
38 public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
39 $testString = ' a , b , c ,d ,, e,f,';
40 $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
41 $actualArray = t3lib_div::trimExplode(',', $testString);
42
43 $this->assertEquals($expectedArray, $actualArray);
44 }
45
46 /**
47 * @test
48 */
49 public function checkTrimExplodeRemovesNewLines() {
50 $testString = ' a , b , ' . chr(10) . ' ,d ,, e,f,';
51 $expectedArray = array('a', 'b', 'd', 'e', 'f');
52 $actualArray = t3lib_div::trimExplode(',', $testString, true);
53
54 $this->assertEquals($expectedArray, $actualArray);
55 }
56
57 /**
58 * @test
59 */
60 public function checkTrimExplodeRemovesEmptyElements() {
61 $testString = 'a , b , c , ,d ,, ,e,f,';
62 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
63 $actualArray = t3lib_div::trimExplode(',', $testString, true);
64
65 $this->assertEquals($expectedArray, $actualArray);
66 }
67
68 /**
69 * @test
70 */
71 public function checkTrimExplodeLimitsResultsToFirstXElementsWithPositiveParameter() {
72 $testString = ' a , b , c , , d,, ,e ';
73 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
74 $actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
75
76 $this->assertEquals($expectedArray, $actualArray);
77 }
78
79 /**
80 * @test
81 */
82 public function checkTrimExplodeLimitsResultsToLastXElementsWithNegativeParameter() {
83 $testString = ' a , b , c , d, ,e, f , , ';
84 $expectedArray = array('a', 'b', 'c'); // limiting returns the rest of the string as the last element
85 $actualArray = t3lib_div::trimExplode(',', $testString, true, -3);
86
87 $this->assertEquals($expectedArray, $actualArray);
88 }
89
90 /**
91 * @test
92 */
93 public function checkTrimExplodeKeepsZeroAsString() {
94 $testString = 'a , b , c , ,d ,, ,e,f, 0 ,';
95 $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', '0');
96 $actualArray = t3lib_div::trimExplode(',', $testString, true);
97
98 $this->assertEquals($expectedArray, $actualArray);
99 }
100 }
101
102 ?>