[TASK] Refactor functional base test class
[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 * # cd /var/www/t3master/foo # Document root of CMS instance, fileadmin/ directory and frontend index.php are here
32 * # ./typo3conf/ext/phpunit/Composer/vendor/bin/phpunit -c typo3/sysext/core/Build/FunctionalTests.xml # Call functional tests
33 */
34 abstract class FunctionalTestCase extends BaseTestCase {
35
36 /**
37 * Core extensions to load.
38 *
39 * If the test case needs additional core extensions as requirement,
40 * they can be noted here and will be added to LocalConfiguration
41 * extension list and ext_tables.sql of those extensions will be applied.
42 *
43 * Required core extensions like core, cms, extbase and so on are loaded
44 * automatically, so there is no need to add them here. See constant
45 * REQUIRED_EXTENSIONS for a list of automatically loaded extensions.
46 *
47 * @var array
48 */
49 protected $coreExtensionsToLoad = array();
50
51 /**
52 * Array of test/fixture extensions paths that should be loaded for a test.
53 *
54 * Given path is expected to be relative to your document root, example:
55 *
56 * array(
57 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
58 * 'typo3conf/ext/base_extension',
59 * );
60 *
61 * Extensions in this array are linked to the test instance, loaded
62 * and their ext_tables.sql will be applied.
63 *
64 * @var array
65 */
66 protected $testExtensionsToLoad = array();
67
68 /**
69 * Set up creates a test database and fills with data.
70 *
71 * This method should be called with parent::setUp() in your test cases!
72 *
73 * @return void
74 */
75 public function setUp() {
76 if (!defined('ORIGINAL_ROOT')) {
77 $this->markTestSkipped('Functional tests must be called through phpunit on CLI');
78 }
79 $bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
80 $bootstrapUtility->setUp(get_class($this), $this->coreExtensionsToLoad, $this->testExtensionsToLoad);
81 }
82
83 /**
84 * Tear down.
85 *
86 * This method should be called with parent::tearDown() in your test cases!
87 *
88 * @return void
89 */
90 public function tearDown() {
91 $bootstrapUtility = new FunctionalTestCaseBootstrapUtility();
92 $bootstrapUtility->tearDown();
93 }
94
95 /**
96 * Imports a data set represented as XML into the test database,
97 *
98 * @param string $path Absolute path to the XML file containing the data set to load
99 * @return void
100 * @throws \TYPO3\CMS\Core\Tests\Exception
101 */
102 protected function importDataSet($path) {
103 if (!is_file($path)) {
104 throw new Exception(
105 'Fixture file ' . $path . ' not found',
106 1376746261
107 );
108 }
109
110 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
111 $database = $GLOBALS['TYPO3_DB'];
112
113 $xml = simplexml_load_file($path);
114 $foreignKeys = array();
115
116 /** @var $table \SimpleXMLElement */
117 foreach ($xml->children() as $table) {
118 $insertArray = array();
119
120 /** @var $column \SimpleXMLElement */
121 foreach ($table->children() as $column) {
122 $columnName = $column->getName();
123 $columnValue = NULL;
124
125 if (isset($column['ref'])) {
126 list($tableName, $elementId) = explode('#', $column['ref']);
127 $columnValue = $foreignKeys[$tableName][$elementId];
128 } elseif (isset($column['is-NULL']) && ($column['is-NULL'] === 'yes')) {
129 $columnValue = NULL;
130 } else {
131 $columnValue = $table->$columnName;
132 }
133
134 $insertArray[$columnName] = $columnValue;
135 }
136
137 $tableName = $table->getName();
138 $database->exec_INSERTquery($tableName, $insertArray);
139
140 if (isset($table['id'])) {
141 $elementId = (string) $table['id'];
142 $foreignKeys[$tableName][$elementId] = $database->sql_insert_id();
143 }
144 }
145 }
146 }
147 ?>