Passed
Push — develop ( 9ae090...ce9409 )
by Schlaefer
42s
created

TestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 11
dl 0
loc 80
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A createPhileResponse() 0 5 1
A createServerRequestFromArray() 0 8 3
B createPhileCore() 0 46 6
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 30
    protected function createPhileCore(Event $event = null, Config $config = null)
35
    {
36
        //# setup Config
37 30
        $config = $config ?: new Config;
38 30
        if (!$config->has('encryptionKey')) {
39 29
            $config->set('encryptionKey', 'testing');
40
        }
41 30
        $testConfig = $config->toArray();
42
43
        //# setup container
44 30
        Utility::load($config->get('config_dir') . '/container.php');
45
46 30
        $container = Container::getInstance();
47 30
        if ($event) {
48 1
            $container->set('Phile_EventBus', $event);
49
        }
50
51
        //# setup bootstrap
52 30
        $core = $container->get('Phile_App');
53 30
        $core->addBootstrap(function ($eventBus, $config) use ($testConfig) {
54 29
            $configDir = $config->get('config_dir');
55 29
            Bootstrap::loadConfiguration($configDir . 'defaults.php', $config);
56 29
            $config->merge($testConfig);
57
58 29
            defined('CONTENT_DIR') || define('CONTENT_DIR', $config->get('content_dir'));
59 29
            defined('CONTENT_EXT') || define('CONTENT_EXT', $config->get('content_ext'));
60
61 29
            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 29
            Bootstrap::loadPlugins($eventBus, $config);
64
65 29
            Registry::set('templateVars', []);
66 30
        });
67
68
        //# setup middleware
69 30
        $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 6
            $eventBus->trigger('phile.core.middleware.add', ['middleware' => $middleware]);
71 6
            $middleware->add($core);
72 30
        });
73
        
74
        //# additional test setup
75
        // clears out warnings of inefficient/multiple calls
76 30
        \phpFastCache\CacheManager::clearInstances();
77
78 30
        return $core;
79
    }
80
81
    /**
82
     * Run Phile and create response
83
     */
84 6
    protected function createPhileResponse(Phile $app, ServerRequestInterface $request): ResponseInterface
85
    {
86 6
        $server = new Server($app);
87 6
        return $server->run($request);
88
    }
89
90
    /**
91
     * Creates ServerRequest
92
     *
93
     * @param array $server $_SERVER environment
94
     * @return ServerRequestInterface
95
     */
96 17
    protected function createServerRequestFromArray(array $server = null): ServerRequestInterface
97
    {
98 17
        $server = $server ?: [];
99 17
        if (!isset($server['REQUEST_URI'])) {
100 12
            $server['REQUEST_URI'] = '/';
101
        }
102 17
        return \Zend\Diactoros\ServerRequestFactory::fromGlobals($server);
103
    }
104
}
105