[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 * @var \TYPO3\CMS\Core\Category\CategoryRegistry
45 */
46 protected $fixture;
47
48 /**
49 * @var array
50 */
51 protected $tables;
52
53 /**
54 * Sets up this test suite.
55 */
56 protected function setUp() {
57 $this->fixture = new \TYPO3\CMS\Core\Category\CategoryRegistry();
58 $this->tables = array(
59 'first' => uniqid('first'),
60 'second' => uniqid('second')
61 );
62 foreach ($this->tables as $tableName) {
63 $GLOBALS['TCA'][$tableName] = array('ctrl' => array());
64 }
65 }
66
67 /**
68 * Tears down this test suite.
69 */
70 protected function tearDown() {
71 unset($this->tables);
72 unset($this->fixture);
73 }
74
75 /**
76 * @test
77 */
78 public function isRegistryEmptyByDefault() {
79 $this->assertEquals(array(), $this->fixture->get());
80 }
81
82 /**
83 * @test
84 */
85 public function doesAddReturnTrueOnDefinedTable() {
86 $this->assertTrue($this->fixture->add('test_extension_a', $this->tables['first'], 'categories'));
87 }
88
89 /**
90 * @test
91 */
92 public function doesAddReturnFalseOnUndefinedTable() {
93 $this->assertFalse($this->fixture->add('test_extension_a', uniqid('undefined'), 'categories'));
94 }
95
96 /**
97 * @test
98 */
99 public function areMultipleElementsOfSameExtensionRegistered() {
100 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
101 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
102 $registry = $this->fixture->get();
103 ob_flush();
104 $this->assertEquals('categories', $registry['test_extension_a'][$this->tables['first']]);
105 $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
106 }
107
108 /**
109 * @test
110 */
111 public function areElementsOfDifferentExtensionsRegistered() {
112 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
113 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
114 $registry = $this->fixture->get();
115 $this->assertEquals('categories', $registry['test_extension_a'][$this->tables['first']]);
116 $this->assertEquals('categories', $registry['test_extension_b'][$this->tables['second']]);
117 }
118
119 /**
120 * @test
121 */
122 public function areElementsOnSameTableOverridden() {
123 $this->fixture->add('test_extension_a', $this->tables['first'], $this->tables['first']);
124 $this->fixture->add('test_extension_b', $this->tables['second'], $this->tables['second']);
125 $registry = $this->fixture->get();
126 $this->assertEquals($this->tables['first'], $registry['test_extension_a'][$this->tables['first']]);
127 }
128
129 /**
130 * @test
131 */
132 public function areDatabaseDefinitionsOfAllElementsAvailable() {
133 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
134 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
135 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
136 $definitions = $this->fixture->getDatabaseTableDefinitions();
137 $matches = array();
138 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
139 $this->assertEquals(2, count($matches[0]));
140 $this->assertEquals($matches[1][0], $this->tables['first']);
141 $this->assertEquals($matches[2][0], 'categories');
142 $this->assertEquals($matches[1][1], $this->tables['second']);
143 $this->assertEquals($matches[2][1], 'categories');
144 }
145
146 /**
147 * @test
148 */
149 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
150 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
151 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
152 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
153 $matches = array();
154 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
155 $this->assertEquals(1, count($matches[0]));
156 $this->assertEquals($matches[1][0], $this->tables['first']);
157 $this->assertEquals($matches[2][0], 'categories');
158 }
159
160 }
161
162 ?>