Passed
Push — develop ( 446210...89516f )
by Schlaefer
02:39
created

TestCase::createPhileCore()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 6.0023

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
ccs 24
cts 25
cp 0.96
rs 8.439
cc 6
eloc 23
nc 8
nop 2
crap 6.0023
1
<?php
2
/**
3
 * @author  PhileCMS
4
 * @link    https://philecms.com
5
 * @license http://opensource.org/licenses/MIT
6
 */
7
8
namespace Phile\Test;
9
10
use Phile\Core\Bootstrap;
11
use Phile\Core\Config;
12
use Phile\Core\Container;
13
use Phile\Core\Event;
14
use Phile\Core\Registry;
15
use Phile\Core\Utility;
16
use Phile\Phile;
17
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
/**
21
 * Class for Phile test-cases
22
 */
23
abstract class TestCase extends PHPUnitTestCase
24
{
25
    /**
26
     * Creates a Phile-core with bootstrap configuration for testing
27
     *
28
     * @param Event $event
29
     * @param Config $config
30
     * @return Phile configured core
31
     */
32 28
    protected function createPhileCore(Event $event = null, Config $config = null)
33
    {
34
        //# setup Config
35 28
        $config = $config ?: new Config;
36 28
        if (!$config->has('encryptionKey')) {
37 27
            $config->set('encryptionKey', 'testing');
38
        }
39 28
        $testConfig = $config->toArray();
40
41
        //# setup container
42 28
        Utility::load($config->get('config_dir') . '/container.php');
43
44 28
        $container = Container::getInstance();
45 28
        if ($event) {
46
            $container->set('Phile_EventBus', $event);
47
        }
48
49
        //# setup bootstrap
50 28
        $core = $container->get('Phile_App');
51 28
        $core->addBootstrap(function ($eventBus, $config) use ($testConfig) {
52 27
            $configDir = $config->get('config_dir');
53 27
            Bootstrap::loadConfiguration($configDir . 'defaults.php', $config);
54 27
            $config->merge($testConfig);
55
56 27
            defined('CONTENT_DIR') || define('CONTENT_DIR', $config->get('content_dir'));
57 27
            defined('CONTENT_EXT') || define('CONTENT_EXT', $config->get('content_ext'));
58
59 27
            Event::setInstance($eventBus);
0 ignored issues
show
Deprecated Code introduced by
The method Phile\Core\Event::setInstance() has been deprecated with message: static use is deprecated

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
60
61 27
            Bootstrap::loadPlugins($eventBus, $config);
62
63 27
            Registry::set('templateVars', []);
64 28
        });
65
66
        //# setup middleware
67 28
        $core->addMiddleware(function ($middleware, $eventBus, $config) use ($core) {
0 ignored issues
show
Unused Code introduced by
The parameter $config is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68 4
            $eventBus->trigger('phile.core.middleware.add', ['middleware' => $middleware]);
69 4
            $middleware->add($core, 0);
70 28
        });
71
72 28
        return $core;
73
    }
74
75
    /**
76
     * Creates ServerRequest
77
     *
78
     * @param array $server $_SERVER environment
79
     * @return ServerRequestInterface
80
     */
81 15
    protected function createServerRequestFromArray(array $server = null)
82
    {
83 15
        $server = $server ?: [];
84 15
        if (!isset($server['REQUEST_URI'])) {
85 12
            $server['REQUEST_URI'] = '/';
86
        }
87 15
        return \Zend\Diactoros\ServerRequestFactory::fromGlobals($server);
88
    }
89
}
90