Issues (29)

lib/Phile/Test/TestCase.php (1 issue)

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 Laminas\Diactoros\ServerRequestFactory;
11
use Phile\Core\Bootstrap;
12
use Phile\Core\Config;
13
use Phile\Core\Container;
14
use Phile\Core\Event;
15
use Phile\Core\Registry;
16
use Phile\Core\Utility;
17
use Phile\Http\Server;
18
use Phile\Phile;
19
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
23
/**
24
 * Class for Phile test-cases
25
 */
26
abstract class TestCase extends PHPUnitTestCase
27
{
28
    /**
29
     * Creates a Phile-core with bootstrap configuration for testing
30
     *
31
     * @param Event $event
32
     * @param Config $config
33
     * @return Phile configured core
34
     */
35 35
    protected function createPhileCore(Event $event = null, Config $config = null)
36
    {
37
        //# setup Config
38 35
        $config = $config ?: new Config;
39 35
        if (!$config->has('encryptionKey')) {
40 34
            $config->set('encryptionKey', 'testing');
41
        }
42 35
        $testConfig = $config->toArray();
43
44
        //# setup container
45 35
        Utility::load($config->get('config_dir') . '/container.php');
46
47 35
        $container = Container::getInstance();
48 35
        if ($event) {
49 1
            $container->set('Phile_EventBus', $event);
50
        }
51
52
        //# setup bootstrap
53 35
        $core = $container->get('Phile_App');
54 35
        $core->addBootstrap(function ($eventBus, $config) use ($testConfig, $container) {
55 34
            $configDir = $config->get('config_dir');
56 34
            Bootstrap::loadConfiguration($configDir . 'defaults.php', $config);
57 34
            $config->merge($testConfig);
58
59 34
            defined('CONTENT_DIR') || define('CONTENT_DIR', $config->get('content_dir'));
60 34
            defined('CONTENT_EXT') || define('CONTENT_EXT', $config->get('content_ext'));
61
62 34
            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

62
            /** @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...
63
64 34
            $plugins = $container->get('Phile_Plugins');
65 34
            $plugins->addDirectory(ROOT_DIR . 'plugins/');
66 34
            $plugins->load($config);
67
68 34
            Registry::set('templateVars', []);
69
        });
70
71
        //# setup middleware
72 35
        $core->addMiddleware(function ($middleware, $eventBus, $config) use ($core) {
73 8
            $eventBus->trigger('phile.core.middleware.add', ['middleware' => $middleware]);
74 8
            $middleware->add($core);
75
        });
76
77
        //# additional test setup
78
        // clears out warnings of inefficient/multiple calls
79 35
        \Phpfastcache\CacheManager::clearInstances();
80
81 35
        return $core;
82
    }
83
84
    /**
85
     * Run Phile and create response
86
     */
87 8
    protected function createPhileResponse(Phile $app, ServerRequestInterface $request): ResponseInterface
88
    {
89 8
        $server = new Server($app);
90 8
        return $server->run($request);
91
    }
92
93
    /**
94
     * Creates ServerRequest
95
     *
96
     * @param array $server $_SERVER environment
97
     * @return ServerRequestInterface
98
     */
99 19
    protected function createServerRequestFromArray(array $server = null): ServerRequestInterface
100
    {
101 19
        $server = $server ?: [];
102 19
        if (!isset($server['REQUEST_URI'])) {
103 12
            $server['REQUEST_URI'] = '/';
104
        }
105 19
        return ServerRequestFactory::fromGlobals($server);
106
    }
107
}
108