Passed
Pull Request — develop (#326)
by Schlaefer
04:07
created

TestCase::createPhileResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
crap 1
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 28
        return $core;
75
    }
76
77
    /**
78
     * Run Phile and create response
79
     */
80 4
    protected function createPhileResponse(Phile $app, ServerRequestInterface $request): ResponseInterface
81
    {
82 4
        $server = new Server($app);
83 4
        return $server->run($request);
84
    }
85
86
    /**
87
     * Creates ServerRequest
88
     *
89
     * @param array $server $_SERVER environment
90
     * @return ServerRequestInterface
91
     */
92 15
    protected function createServerRequestFromArray(array $server = null): ServerRequestInterface
93
    {
94 15
        $server = $server ?: [];
95 15
        if (!isset($server['REQUEST_URI'])) {
96 11
            $server['REQUEST_URI'] = '/';
97
        }
98 15
        return \Zend\Diactoros\ServerRequestFactory::fromGlobals($server);
99
    }
100
}
101