Passed
Push — develop ( bcf814...d07230 )
by Schlaefer
02:57
created

TestCase::createPhileCore()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 46
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 6.002

Importance

Changes 0
Metric Value
dl 0
loc 46
ccs 25
cts 26
cp 0.9615
rs 8.4751
c 0
b 0
f 0
cc 6
eloc 24
nc 8
nop 2
crap 6.002
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\Http\Server;
17
use Phile\Phile;
18
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
19
use Psr\Http\Message\ResponseInterface;
20
use Psr\Http\Message\ServerRequestInterface;
21
22
/**
23
 * Class for Phile test-cases
24
 */
25
abstract class TestCase extends PHPUnitTestCase
26
{
27
    /**
28
     * Creates a Phile-core with bootstrap configuration for testing
29
     *
30
     * @param Event $event
31
     * @param Config $config
32
     * @return Phile configured core
33
     */
34 28
    protected function createPhileCore(Event $event = null, Config $config = null)
35
    {
36
        //# setup Config
37 28
        $config = $config ?: new Config;
38 28
        if (!$config->has('encryptionKey')) {
39 27
            $config->set('encryptionKey', 'testing');
40
        }
41 28
        $testConfig = $config->toArray();
42
43
        //# setup container
44 28
        Utility::load($config->get('config_dir') . '/container.php');
45
46 28
        $container = Container::getInstance();
47 28
        if ($event) {
48
            $container->set('Phile_EventBus', $event);
49
        }
50
51
        //# setup bootstrap
52 28
        $core = $container->get('Phile_App');
53 28
        $core->addBootstrap(function ($eventBus, $config) use ($testConfig) {
54 27
            $configDir = $config->get('config_dir');
55 27
            Bootstrap::loadConfiguration($configDir . 'defaults.php', $config);
56 27
            $config->merge($testConfig);
57
58 27
            defined('CONTENT_DIR') || define('CONTENT_DIR', $config->get('content_dir'));
59 27
            defined('CONTENT_EXT') || define('CONTENT_EXT', $config->get('content_ext'));
60
61 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...
62
63 27
            Bootstrap::loadPlugins($eventBus, $config);
64
65 27
            Registry::set('templateVars', []);
66 28
        });
67
68
        //# setup middleware
69 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...
70 4
            $eventBus->trigger('phile.core.middleware.add', ['middleware' => $middleware]);
71 4
            $middleware->add($core, 0);
72 28
        });
73
        
74
        //# additional test setup
75
        // clears out warnings of inefficient/multiple calls
76 28
        \phpFastCache\CacheManager::clearInstances();
77
78 28
        return $core;
79
    }
80
81
    /**
82
     * Run Phile and create response
83
     */
84 4
    protected function createPhileResponse(Phile $app, ServerRequestInterface $request): ResponseInterface
85
    {
86 4
        $server = new Server($app);
87 4
        return $server->run($request);
88
    }
89
90
    /**
91
     * Creates ServerRequest
92
     *
93
     * @param array $server $_SERVER environment
94
     * @return ServerRequestInterface
95
     */
96 15
    protected function createServerRequestFromArray(array $server = null): ServerRequestInterface
97
    {
98 15
        $server = $server ?: [];
99 15
        if (!isset($server['REQUEST_URI'])) {
100 11
            $server['REQUEST_URI'] = '/';
101
        }
102 15
        return \Zend\Diactoros\ServerRequestFactory::fromGlobals($server);
103
    }
104
}
105