[TASK] Namespace and move t3lib unit tests - 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Category / CategoryRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Category;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2012 Oliver Hader <oliver.hader@typo3.org>
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 * Testcase for CategoryRegistry
29 *
30 * @package TYPO3
31 * @subpackage t3lib
32 * @author Oliver Hader <oliver.hader@typo3.org>
33 */
34 class CategoryRegistryTest extends \tx_phpunit_testcase {
35
36 /**
37 * Enable backup of global and system variables
38 *
39 * @var boolean
40 */
41 protected $backupGlobals = TRUE;
42
43 /**
44 * Exclude TYPO3_DB from backup/ restore of $GLOBALS
45 * because resource types cannot be handled during serializing
46 *
47 * @var array
48 */
49 protected $backupGlobalsBlacklist = array('TYPO3_DB');
50
51 /**
52 * @var \TYPO3\CMS\Core\Category\CategoryRegistry
53 */
54 protected $fixture;
55
56 /**
57 * @var array
58 */
59 protected $tables;
60
61 /**
62 * Sets up this test suite.
63 */
64 protected function setUp() {
65 $this->fixture = new \TYPO3\CMS\Core\Category\CategoryRegistry();
66 $this->tables = array(
67 'first' => uniqid('first'),
68 'second' => uniqid('second')
69 );
70 foreach ($this->tables as $tableName) {
71 $GLOBALS['TCA'][$tableName] = array('ctrl' => array());
72 }
73 }
74
75 /**
76 * Tears down this test suite.
77 */
78 protected function tearDown() {
79 unset($this->tables);
80 unset($this->fixture);
81 }
82
83 /**
84 * @test
85 */
86 public function isRegistryEmptyByDefault() {
87 $this->assertEquals(array(), $this->fixture->get());
88 }
89
90 /**
91 * @test
92 */
93 public function doesAddReturnTrueOnDefinedTable() {
94 $this->assertTrue($this->fixture->add('test_extension_a', $this->tables['first'], 'categories'));
95 }
96
97 /**
98 * @test
99 */
100 public function doesAddReturnFalseOnUndefinedTable() {
101 $this->assertFalse($this->fixture->add('test_extension_a', uniqid('undefined'), 'categories'));
102 }
103
104 /**
105 * @test
106 */
107 public function areMultipleElementsOfSameExtensionRegistered() {
108 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
109 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
110 $registry = $this->fixture->get();
111 ob_flush();
112 $this->assertEquals('categories', $registry['test_extension_a'][$this->tables['first']]);
113 $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
114 }
115
116 /**
117 * @test
118 */
119 public function areElementsOfDifferentExtensionsRegistered() {
120 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
121 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
122 $registry = $this->fixture->get();
123 $this->assertEquals('categories', $registry['test_extension_a'][$this->tables['first']]);
124 $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
125 }
126
127 /**
128 * @test
129 */
130 public function areElementsOnSameTableOverridden() {
131 $this->fixture->add('test_extension_a', $this->tables['first'], $this->tables['first']);
132 $this->fixture->add('test_extension_b', $this->tables['second'], $this->tables['second']);
133 $registry = $this->fixture->get();
134 $this->assertEquals($this->tables['first'], $registry['test_extension_a'][$this->tables['first']]);
135 }
136
137 /**
138 * @test
139 */
140 public function areDatabaseDefinitionsOfAllElementsAvailable() {
141 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
142 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
143 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
144 $definitions = $this->fixture->getDatabaseTableDefinitions();
145 $matches = array();
146 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
147 $this->assertEquals(2, count($matches[0]));
148 $this->assertEquals($matches[1][0], $this->tables['first']);
149 $this->assertEquals($matches[2][0], 'categories');
150 $this->assertEquals($matches[1][1], $this->tables['second']);
151 $this->assertEquals($matches[2][1], 'categories');
152 }
153
154 /**
155 * @test
156 */
157 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
158 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
159 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
160 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
161 $matches = array();
162 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
163 $this->assertEquals(1, count($matches[0]));
164 $this->assertEquals($matches[1][0], $this->tables['first']);
165 $this->assertEquals($matches[2][0], 'categories');
166 }
167
168 }
169
170 ?>