-
Notifications
You must be signed in to change notification settings - Fork 14
/
AppTestCase.php
112 lines (93 loc) · 3.1 KB
/
AppTestCase.php
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
112
<?php declare(strict_types=1);
namespace Tests;
use kissj\Application\ApplicationGetter;
use kissj\Event\EventRepository;
use kissj\User\User;
use kissj\User\UserLoginType;
use kissj\User\UserRepository;
use kissj\User\UserRole;
use Phinx\Console\PhinxApplication;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Slim\App;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Headers;
use Slim\Psr7\Request;
use Slim\Psr7\Uri;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
class AppTestCase extends TestCase
{
protected function getTestApp(bool $freshInit = true): App
{
if ($freshInit) {
$this->clearTempFolder();
$arguments = [
'command' => 'migrate',
'--configuration' => __DIR__ . '/phinxConfiguration.php',
];
$phinx = new PhinxApplication();
$phinx->setAutoExit(false);
$phinx->run(new ArrayInput($arguments), new BufferedOutput());
}
$app = (new ApplicationGetter())->getApp(
__DIR__ . '/',
'env.testing',
__DIR__ . '/temp'
);
return $app;
}
/**
* @param array<string,string> $body
* @param array<string,string> $serverParams
* @param array<string,string> $cookies
*/
protected function createRequest(
string $path,
string $method = 'GET',
array $body = [],
array $serverParams = [],
array $cookies = []
): Request {
$uri = new Uri('', '', 80, $path);
$handle = fopen('php://temp', 'wb+');
if ($handle === false) {
throw new \RuntimeException('opening php://temp failed');
}
$stream = (new StreamFactory())->createStreamFromResource($handle);
$request = new Request($method, $uri, new Headers(), $cookies, $serverParams, $stream);
if (count($body) > 0) {
return $request->withParsedBody($body);
}
return $request;
}
protected function clearTempFolder(): void
{
$files = glob(__DIR__ . '/temp/*'); // skipping hidden files
if ($files === false) {
throw new \RuntimeException('glob function fails');
}
foreach ($files as $file) {
if (is_file($file)) {
unlink($file);
}
}
}
protected function createAdminUser(App $app): User
{
/** @var ContainerInterface $container */
$container = $app->getContainer();
/** @var UserRepository $userRepository */
$userRepository = $container->get(UserRepository::class);
/** @var EventRepository $eventRepository */
$eventRepository = $container->get(EventRepository::class);
$testEvent = $eventRepository->get(1);
$user = new User();
$user->event = $testEvent;
$user->role = UserRole::Admin;
$user->email = 'admin@example.com';
$user->loginType = UserLoginType::Email;
$userRepository->persist($user);
return $user;
}
}