-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathSlim.php
More file actions
111 lines (94 loc) · 2.87 KB
/
Copy pathSlim.php
File metadata and controls
111 lines (94 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace DoclerLabs\CodeceptionSlimModule\Module;
use Codeception\Configuration;
use Codeception\Exception\ConfigurationException;
use Codeception\Exception\ModuleConfigException;
use Codeception\Lib\Framework;
use Codeception\TestInterface;
use DoclerLabs\CodeceptionSlimModule\Lib\Connector\SlimPsr7;
use Psr\Container\ContainerInterface;
use Slim\App;
/**
* This module uses Slim App to emulate requests and test response.
*
* ## Configuration
*
* ### Slim 4.x
*
* * application - Relative path to file which bootstrap and returns your `Slim\App` instance.
* * headers - Default headers to be sent with every request (optional).
*
* #### Example (`test/suite/functional.suite.yml`)
* ```yaml
* modules:
* config:
* DoclerLabs\CodeceptionSlimModule\Module\Slim:
* application: 'app/bootstrap.php'
* headers:
* Content-Type: application/json
* ```
*
* ## Public Properties
*
* * app - Slim App instance
*
* Usage example:
*
* ```yaml
* actor: FunctionalTester
* modules:
* enabled:
* - REST:
* depends: DoclerLabs\CodeceptionSlimModule\Module\Slim
*
* config:
* DoclerLabs\CodeceptionSlimModule\Module\Slim:
* application: 'app/bootstrap.php'
* headers:
* Content-Type: application/json
* ```
*/
class Slim extends Framework
{
/** @var App<ContainerInterface|null> */
public App $app;
protected array $requiredFields = ['application'];
protected array $config = ['headers' => []];
private string $applicationPath;
public function _initialize(): void
{
/** @var string $configApplication */
$configApplication = $this->config['application'];
$applicationPath = Configuration::projectDir() . $configApplication;
if (!is_readable($applicationPath)) {
throw new ModuleConfigException(
static::class,
"Application file does not exist or is not readable.\nPlease, check path for php file: `$applicationPath`"
);
}
$this->applicationPath = $applicationPath;
parent::_initialize();
}
public function _before(TestInterface $test): void
{
$app = require $this->applicationPath;
// Check if app instance is ready.
if (!$app instanceof App) {
throw new ConfigurationException(
sprintf(
"Unable to bootstrap slim application.\n Application file must return with `%s` instance.",
App::class
)
);
}
$this->app = $app;
$connector = new SlimPsr7();
$connector->setApp($this->app);
$this->client = $connector;
/** @var array<string, string> $headers */
$headers = $this->config['headers'];
$this->headers = $headers;
parent::_before($test);
}
}