Completed
Push — master ( 748c89...d24f56 )
by Schlaefer
06:53 queued 03:34
created

TestCase::createServerRequestFromArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 4
nc 2
nop 1
dl 0
loc 7
c 0
b 0
f 0
cc 3
ccs 5
cts 5
cp 1
crap 3
rs 9.4285
1
<?php
2
/**
3
 * @author  PhileCMS
4
 * @link    https://philecms.github.io
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 32
    protected function createPhileCore(Event $event = null, Config $config = null)
35
    {
36
        //# setup Config
37 32
        $config = $config ?: new Config;
38 32
        if (!$config->has('encryptionKey')) {
39 31
            $config->set('encryptionKey', 'testing');
40
        }
41 32
        $testConfig = $config->toArray();
42
43
        //# setup container
44 32
        Utility::load($config->get('config_dir') . '/container.php');
45
46 32
        $container = Container::getInstance();
47 32
        if ($event) {
48 1
            $container->set('Phile_EventBus', $event);
49
        }
50
51
        //# setup bootstrap
52 32
        $core = $container->get('Phile_App');
53 32
        $core->addBootstrap(function ($eventBus, $config) use ($testConfig, $container) {
54 31
            $configDir = $config->get('config_dir');
55 31
            Bootstrap::loadConfiguration($configDir . 'defaults.php', $config);
56 31
            $config->merge($testConfig);
57
58 31
            defined('CONTENT_DIR') || define('CONTENT_DIR', $config->get('content_dir'));
59 31
            defined('CONTENT_EXT') || define('CONTENT_EXT', $config->get('content_ext'));
60
61 31
            Event::setInstance($eventBus);
0 ignored issues
show
Deprecated Code introduced by
The function Phile\Core\Event::setInstance() has been deprecated: static use is deprecated ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            /** @scrutinizer ignore-deprecated */ Event::setInstance($eventBus);

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

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

Loading history...
62
63 31
            $plugins = $container->get('Phile_Plugins');
64 31
            $plugins->addDirectory(ROOT_DIR . 'plugins/');
65 31
            $plugins->load($config);
66
67 31
            Registry::set('templateVars', []);
68 32
        });
69
70
        //# setup middleware
71 32
        $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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

71
        $core->addMiddleware(function ($middleware, $eventBus, /** @scrutinizer ignore-unused */ $config) use ($core) {

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

Loading history...
72 8
            $eventBus->trigger('phile.core.middleware.add', ['middleware' => $middleware]);
73 8
            $middleware->add($core);
74 32
        });
75
        
76
        //# additional test setup
77
        // clears out warnings of inefficient/multiple calls
78 32
        \phpFastCache\CacheManager::clearInstances();
79
80 32
        return $core;
81
    }
82
83
    /**
84
     * Run Phile and create response
85
     */
86 8
    protected function createPhileResponse(Phile $app, ServerRequestInterface $request): ResponseInterface
87
    {
88 8
        $server = new Server($app);
89 8
        return $server->run($request);
90
    }
91
92
    /**
93
     * Creates ServerRequest
94
     *
95
     * @param array $server $_SERVER environment
96
     * @return ServerRequestInterface
97
     */
98 19
    protected function createServerRequestFromArray(array $server = null): ServerRequestInterface
99
    {
100 19
        $server = $server ?: [];
101 19
        if (!isset($server['REQUEST_URI'])) {
102 12
            $server['REQUEST_URI'] = '/';
103
        }
104 19
        return \Zend\Diactoros\ServerRequestFactory::fromGlobals($server);
105
    }
106
}
107