AbstractTestCase   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 126
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 31 5
A cleanUp() 0 20 5
A getSerializedErrors() 0 3 1
A tearDown() 0 6 1
1
<?php
2
3
/**
4
 * Test case
5
 *
6
 * Copyright (c) 2007-2009, Mayflower GmbH
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright
14
 *     notice, this list of conditions and the following disclaimer.
15
 *
16
 *   * Redistributions in binary form must reproduce the above copyright
17
 *     notice, this list of conditions and the following disclaimer in
18
 *     the documentation and/or other materials provided with the
19
 *     distribution.
20
 *
21
 *   * Neither the name of Mayflower GmbH nor the names of his
22
 *     contributors may be used to endorse or promote products derived
23
 *     from this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 *
38
 * @category PHP_CodeBrowser
39
 *
40
 * @author Elger Thiele <[email protected]>
41
 *
42
 * @copyright 2007-2009 Mayflower GmbH
43
 *
44
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
45
 *
46
 * @version SVN: $Id$
47
 *
48
 * @link http://www.phpunit.de/
49
 *
50
 * @since File available since  0.1.0
51
 */
52
53
namespace PHPCodeBrowser\Tests;
54
55
use PHPUnit\Framework\TestCase;
56
57
/**
58
 * AbstractTests
59
 *
60
 * @category PHP_CodeBrowser
61
 *
62
 * @author Elger Thiele <[email protected]>
63
 *
64
 * @copyright 2007-2009 Mayflower GmbH
65
 *
66
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
67
 *
68
 * @version Release: @package_version@
69
 *
70
 * @link http://www.phpunit.de/
71
 *
72
 * @since Class available since  0.1.0
73
 */
74
class AbstractTestCase extends TestCase
75
{
76
    /**
77
     * PHP_CodeBrowser test output dir
78
     *
79
     * @var string
80
     */
81
    protected static $testOutputDir;
82
83
    /**
84
     * PHP_CodeBrowser source root dir
85
     *
86
     * @var string
87
     */
88
    protected static $phpcbSourceDir;
89
90
    /**
91
     * PHP_CodeBrowser error file
92
     *
93
     * @var string
94
     */
95
    protected static $xmlFile;
96
97
    /**
98
     * Basic XML file with valid headers
99
     *
100
     * @var string
101
     */
102
    protected static $xmlBasic;
103
104
    /**
105
     * File of serialized error list
106
     *
107
     * @var string
108
     */
109
    protected static $serializedErrors;
110
111
    /**
112
     * Global setup method for all test cases. Basic variables are initialized.
113
     *
114
     * @return void
115
     */
116
    protected function setUp(): void
117
    {
118
        parent::setUp();
119
120
        if (!\defined('PHPCB_SOURCE_DIR')) {
121
            \define('PHPCB_SOURCE_DIR', \realpath(__DIR__.'/../'));
122
        }
123
124
        if (!\defined('PHPCB_TEST_DIR')) {
125
            \define(
126
                'PHPCB_TEST_DIR',
127
                \realpath(PHPCB_SOURCE_DIR).DIRECTORY_SEPARATOR.'Tests'.DIRECTORY_SEPARATOR.'testData'
128
            );
129
        }
130
131
        if (!\defined('PHPCB_TEST_LOGS')) {
132
            \define('PHPCB_TEST_LOGS', PHPCB_TEST_DIR.'/logs');
133
        }
134
135
        self::$xmlBasic = PHPCB_TEST_LOGS.'/basic.xml';
136
137
        self::$phpcbSourceDir = \realpath(__DIR__.'/Fixtures');
138
139
        self::$testOutputDir = PHPCB_TEST_DIR.DIRECTORY_SEPARATOR.'output';
140
141
        if (\is_dir(self::$testOutputDir)) {
142
            $this->cleanUp(self::$testOutputDir);
143
            \rmdir(self::$testOutputDir);
144
        }
145
146
        \mkdir(self::$testOutputDir);
147
    }
148
149
    /**
150
     * Global tear down method for all test cases.
151
     * Cleaning up generated data and output.
152
     *
153
     * @return void
154
     */
155
    protected function tearDown(): void
156
    {
157
        parent::tearDown();
158
159
        $this->cleanUp(self::$testOutputDir);
160
        \rmdir(self::$testOutputDir);
161
    }
162
163
    /**
164
     * Load the cb error list
165
     *
166
     * @return array List of cb errors
167
     */
168
    protected function getSerializedErrors(): array
169
    {
170
        return \unserialize(\file_get_contents(self::$serializedErrors));
171
    }
172
173
    /**
174
     * Cleanup the test directory output folder
175
     *
176
     * @param string $dir The directory to clean up
177
     *
178
     * @return void
179
     */
180
    protected function cleanUp(string $dir): void
181
    {
182
        $iterator = new \DirectoryIterator($dir);
183
184
        while ($iterator->valid()) {
185
            // delete file
186
            if ($iterator->isFile()) {
187
                \unlink($dir.'/'.$iterator->current());
188
            }
189
190
            // delete folder recursive
191
            if (!$iterator->isDot() && $iterator->isDir()) {
192
                $this->cleanUp($dir.'/'.$iterator->current());
193
                \rmdir($dir.'/'.$iterator->current());
194
            }
195
196
            $iterator->next();
197
        }
198
199
        unset($iterator);
200
    }
201
}
202