-
Notifications
You must be signed in to change notification settings - Fork 6
/
FixturePathTrait.php
94 lines (75 loc) · 2.31 KB
/
FixturePathTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace TestTools\TestCase;
/**
* @author Michael Mayer <michael@lastzero.net>
* @package TestTools
* @license MIT
*/
trait FixturePathTrait
{
private $testBasePath = '';
private $fixturePath = '';
private $testsDirectory = '/tests/';
private $fixturesDirectory = '_fixtures/';
private $testsPostfix = 'Test.php';
protected function getTestFilename()
{
$reflector = new \ReflectionClass($this);
$testFilename = $reflector->getFileName();
return $testFilename;
}
protected function getTestDirectory()
{
$testFilename = $this->getTestFilename();
$testDirectory = dirname($testFilename);
return $testDirectory;
}
protected function setFixturePath($path)
{
// @codeCoverageIgnoreStart
if (!is_dir($path)) {
mkdir($path, 0755, true);
}
// @codeCoverageIgnoreEnd
$this->fixturePath = (string)$path;
}
protected function getFixturePath()
{
if ($this->fixturePath == '') {
$testFilename = $this->getTestFilename();
$subDirectory = substr(
$testFilename,
strripos($testFilename, $this->testsDirectory) + strlen($this->testsDirectory),
strlen($this->testsPostfix) * -1
);
$fixturePath = $this->getTestBasePath() . $this->fixturesDirectory . $subDirectory;
$this->setFixturePath($fixturePath);
}
return $this->fixturePath;
}
protected function setTestBasePath($path)
{
$this->testBasePath = (string)$path;
}
protected function getTestBasePath()
{
if ($this->testBasePath == '') {
$testDirectory = $this->getTestDirectory();
if (strtolower(basename($testDirectory)) == 'tests') {
$basePath = $testDirectory . '/';
} else {
$basePath = substr(
$testDirectory,
0,
strripos($testDirectory, $this->testsDirectory) + strlen($this->testsDirectory)
);
}
$this->setTestBasePath($basePath);
}
return $this->testBasePath;
}
protected function getBasePath()
{
return realpath($this->getTestBasePath() . '/..');
}
}