[TASK] FunctionalTestCase refactoring
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / FunctionalTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
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 * Base test case class for functional tests, all TYPO3 CMS
29 * functional tests should extend from this class!
30 *
31 * If functional tests need additional setUp() and tearDown() code,
32 * they *must* call parent::setUp() and parent::tearDown() to properly
33 * set up and destroy the test system.
34 *
35 * The functional test system creates a full new TYPO3 CMS instance
36 * within typo3temp/ of the base system and the bootstraps this TYPO3 instance.
37 * This abstract class takes care of creating this instance with its
38 * folder structure and a LocalConfiguration, creates an own database
39 * for each test run and imports tables of loaded extensions.
40 *
41 * Functional tests must be run standalone (calling native phpunit
42 * directly) and can not be executed by eg. the ext:phpunit backend module.
43 * Additionally, the script must be called from the document root
44 * of the instance, otherwise path calculation is not successfully.
45 *
46 * Call whole functional test suite, example:
47 * - cd /var/www/t3master/foo # Document root of CMS instance, here is index.php of frontend
48 * - ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/FunctionalTests.xml
49 *
50 * Call single test case, example:
51 * - cd /var/www/t3master/foo # Document root of CMS instance, here is index.php of frontend
52 * - ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit \
53 * --process-isolation \
54 * --bootstrap typo3/sysext/core/Build/FunctionalTestsBootstrap.php \
55 * typo3/sysext/core/Tests/Functional/Functional/FunctionalTestCaseTest.php
56 */
57 abstract class FunctionalTestCase extends BaseTestCase {
58
59 /**
60 * Core extensions to load.
61 *
62 * If the test case needs additional core extensions as requirement,
63 * they can be noted here and will be added to LocalConfiguration
64 * extension list and ext_tables.sql of those extensions will be applied.
65 *
66 * This property will stay empty in this abstract, so it is possible
67 * to just overwrite it in extending classes.
68 *
69 * Required core extensions like core, cms, extbase and so on are loaded
70 * automatically, so there is no need to add them here. See constant
71 * REQUIRED_EXTENSIONS for a list of automatically loaded extensions.
72 *
73 * @var array
74 */
75 protected $coreExtensionsToLoad = array();
76
77 /**
78 * Array of test/fixture extensions paths that should be loaded for a test.
79 *
80 * This property will stay empty in this abstract, so it is possible
81 * to just overwrite it in extending classes.
82 *
83 * Given path is expected to be relative to your document root, example:
84 *
85 * array(
86 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
87 * 'typo3conf/ext/base_extension',
88 * );
89 *
90 * Extensions in this array are linked to the test instance, loaded
91 * and their ext_tables.sql will be applied.
92 *
93 * @var array
94 */
95 protected $testExtensionsToLoad = array();
96
97 /**
98 * Private utility class used in setUp() and tearDown(). Do NOT use in test cases!
99 *
100 * @var \TYPO3\CMS\Core\Tests\FunctionalTestCaseBootstrapUtility
101 */
102 private $bootstrapUtility = NULL;
103
104 /**
105 * Set up creates a test instance and database.
106 *
107 * This method should be called with parent::setUp() in your test cases!
108 *
109 * @return void
110 */
111 public function setUp() {
112 if (!defined('ORIGINAL_ROOT')) {
113 $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
114 }
115 $this->bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
116 $this->bootstrapUtility->setUp(get_class($this), $this->coreExtensionsToLoad, $this->testExtensionsToLoad);
117 }
118
119 /**
120 * Tear down destroys the instance and database.
121 *
122 * This method should be called with parent::tearDown() in your test cases!
123 *
124 * @throws Exception
125 * @return void
126 */
127 public function tearDown() {
128 if (!($this->bootstrapUtility instanceof FunctionalTestCaseBootstrapUtility)) {
129 throw new Exception(
130 'Bootstrap utility not set. Is parent::setUp() called in setUp()?',
131 1376826527
132 );
133 }
134 $this->bootstrapUtility->tearDown();
135 }
136
137 /**
138 * Imports a data set represented as XML into the test database,
139 *
140 * @param string $path Absolute path to the XML file containing the data set to load
141 * @return void
142 * @throws Exception
143 */
144 protected function importDataSet($path) {
145 if (!is_file($path)) {
146 throw new Exception(
147 'Fixture file ' . $path . ' not found',
148 1376746261
149 );
150 }
151
152 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
153 $database = $GLOBALS['TYPO3_DB'];
154
155 $xml = simplexml_load_file($path);
156 $foreignKeys = array();
157
158 /** @var $table \SimpleXMLElement */
159 foreach ($xml->children() as $table) {
160 $insertArray = array();
161
162 /** @var $column \SimpleXMLElement */
163 foreach ($table->children() as $column) {
164 $columnName = $column->getName();
165 $columnValue = NULL;
166
167 if (isset($column['ref'])) {
168 list($tableName, $elementId) = explode('#', $column['ref']);
169 $columnValue = $foreignKeys[$tableName][$elementId];
170 } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
171 $columnValue = NULL;
172 } else {
173 $columnValue = $table->$columnName;
174 }
175
176 $insertArray[$columnName] = $columnValue;
177 }
178
179 $tableName = $table->getName();
180 $database->exec_INSERTquery($tableName, $insertArray);
181
182 if (isset($table['id'])) {
183 $elementId = (string) $table['id'];
184 $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();
185 }
186 }
187 }
188 }
189 ?>