1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/container |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\Container\PhpUnit\Test; |
7
|
|
|
|
8
|
|
|
use Zend\Loader\AutoloaderFactory; |
9
|
|
|
use Zend\Loader\StandardAutoloader; |
10
|
|
|
use RuntimeException; |
11
|
|
|
|
12
|
|
|
error_reporting(E_ALL | E_STRICT); |
13
|
|
|
chdir(__DIR__); |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Bootstrap |
17
|
|
|
* |
18
|
|
|
* @package Nnx\Container\PhpUnit\Test |
19
|
|
|
*/ |
20
|
|
|
class Bootstrap |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Настройка тестов |
24
|
|
|
* |
25
|
|
|
* @throws \RuntimeException |
26
|
|
|
*/ |
27
|
|
|
public static function init() |
28
|
|
|
{ |
29
|
|
|
static::initAutoloader(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Инициализация автозагрузчика |
35
|
|
|
* |
36
|
|
|
* @return void |
37
|
|
|
* |
38
|
|
|
* @throws RuntimeException |
39
|
|
|
*/ |
40
|
|
|
protected static function initAutoloader() |
41
|
|
|
{ |
42
|
|
|
$vendorPath = static::findParentPath('vendor'); |
43
|
|
|
if (is_readable($vendorPath . '/autoload.php')) { |
44
|
|
|
|
45
|
|
|
/** @noinspection PhpIncludeInspection */ |
46
|
|
|
include $vendorPath . '/autoload.php'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!class_exists(AutoloaderFactory::class)) { |
50
|
|
|
$errMsg = sprintf('Error init autoloader. Autoloader class %s not exists', AutoloaderFactory::class); |
51
|
|
|
throw new RuntimeException($errMsg); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
AutoloaderFactory::factory([ |
56
|
|
|
StandardAutoloader::class => [ |
57
|
|
|
'autoregister_zf' => true, |
58
|
|
|
'namespaces' => [ |
59
|
|
|
'Nnx\\Container' => __DIR__ . '/../../src/', |
60
|
|
|
__NAMESPACE__ => __DIR__ . '/tests/', |
61
|
|
|
'Nnx\\Container\\PhpUnit\\TestData' => __DIR__ . '/_files' |
62
|
|
|
] |
63
|
|
|
] |
64
|
|
|
]); |
65
|
|
|
} catch (\Exception $e) { |
66
|
|
|
$errMsg = 'Error init autoloader'; |
67
|
|
|
throw new RuntimeException($errMsg, $e->getCode(), $e); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $path |
73
|
|
|
* |
74
|
|
|
* @return bool|string |
75
|
|
|
*/ |
76
|
|
|
protected static function findParentPath($path) |
77
|
|
|
{ |
78
|
|
|
$dir = __DIR__; |
79
|
|
|
$previousDir = '.'; |
80
|
|
|
while (!is_dir($dir . '/' . $path)) { |
81
|
|
|
$dir = dirname($dir); |
82
|
|
|
if ($previousDir === $dir) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
$previousDir = $dir; |
86
|
|
|
} |
87
|
|
|
return $dir . '/' . $path; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
Bootstrap::init(); |
92
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.