aboutsummaryrefslogtreecommitdiff
path: root/tests/_support/Helper/Jsonapi.php
blob: 8cd78f7324f64a34570e94a9a5a2dad5e2b3d4df (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php

namespace Helper;

use JsonApi\Middlewares\Authentication;
use JsonApi\Middlewares\JsonApi as JsonApiMiddleware;
use Psr\Http\Message\ResponseInterface;
use Slim\Http\Environment;
use Slim\Http\Request;
use Slim\Http\Response;
use WoohooLabs\Yang\JsonApi\Request\JsonApiRequestBuilder;
use WoohooLabs\Yang\JsonApi\Response\JsonApiResponse;

// here you can define custom actions
// all public methods declared in helper class will be available in $I

class Jsonapi extends \Codeception\Module
{
    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function withPHPLib($credentials, $function) {
        // EVIL HACK
        $oldPerm = $GLOBALS['perm'];
        $oldUser = $GLOBALS['user'];
        $GLOBALS['perm'] = new \Seminar_Perm();
        $GLOBALS['user'] = new \Seminar_User(\User::find($credentials['id']));

        $result = $function($credentials);

        // EVIL HACK
        $GLOBALS['user'] = $oldUser;
        $GLOBALS['perm'] = $oldPerm;

        return $result;
    }

    public function createApp($credentials, $method, $pattern, $callable, $name = null)
    {
        return $this->createApp0(
            $credentials,
            function () use ($method, $pattern, $callable, $name) {
                $route = $this->map([$method], $pattern, $callable);
                if (isset($name)) {
                    $route->setName($name);
                }
            }
        );
    }

    public function createRequestBuilder($credentials = null)
    {
        $env = [];
        if ($credentials) {
            $env = [
                'PHP_AUTH_USER' => $credentials['username'],
                'PHP_AUTH_PW' => $credentials['password'],
            ];
        }

        $requestBuilder = new JsonApiRequestBuilder(
            Request::createFromEnvironment(
                Environment::mock($env)
            )
        );

        $requestBuilder
            ->setProtocolVersion('1.0')
            ->setHeader('Accept-Charset', 'utf-8');

        return $requestBuilder;
    }

    public function sendMockRequest($app, Request $request)
    {
        $container = $app->getContainer();
        $container['request'] = function ($container) use ($request) {
            return $request;
        };
        $response = $app($request, new Response());

        return new JsonApiResponse($response);
    }

    private function createApp0($credentials, $routerFn)
    {
        $app = $this->appFactory();

        $authenticator = function ($username, $password) use ($credentials) {
            // must return a \User
            if ($username === $credentials['username'] && $password === $credentials['password']) {
                return \User::find($credentials['id']);
            }

            return null;
        };

        $group = $app->group('', $routerFn);

        if ($credentials) {
            $group->add(function ($request, $response, $next) {
                $user = $request->getAttribute(Authentication::USER_KEY, null);

                $GLOBALS['auth'] = new \Seminar_Auth();
                $GLOBALS['auth']->auth = array(
                    'uid' => $user->user_id,
                    'uname' => $user->username,
                    'perm' => $user->perms,
                );

                $GLOBALS['user'] = new \Seminar_User($user->user_id);

                $GLOBALS['perm'] = new \Seminar_Perm();
                $GLOBALS['MAIL_VALIDATE_BOX'] = false;

                return $next($request, $response);
            })->add(new Authentication($authenticator));
        }

        $group->add(new JsonApiMiddleware($app));

        return $app;
    }

    private function appFactory()
    {
        $factory = new \JsonApi\AppFactory();

        return $factory->makeApp();
    }

    public function storeJsonMD(
        $filename,
        ResponseInterface $response,
        $limit = null,
        $ellipsis = null
    ) {
        $body = "{$response->getBody()}";
        $body = preg_replace(
            '!plugins.php\\\\/argonautsplugin!',
            'https:\\/\\/example.com',
            $body
        );
        $body = preg_replace('!\\\\/!', '/', $body);
        $body = preg_replace(['!%5B!', '!%5D!'], ['[', ']'], $body);

        $jsonBody = json_decode($body, true);

        if ($limit && isset($jsonBody['data']) && is_array($jsonBody['data'])) {
            $jsonBody['data'] = array_slice($jsonBody['data'], 0, $limit);
            if ($ellipsis) {
                $jsonBody['data'][] = $ellipsis;
            }
        }

        $jsonPretty = new \Camspiers\JsonPretty\JsonPretty();
        $json = $jsonPretty->prettify($jsonBody, JSON_UNESCAPED_SLASHES, '  ');

        $dirname = codecept_output_dir().'json-for-slate/';
        if (!file_exists($dirname)) {
            @mkdir($dirname);
        }

        if (file_exists($dirname)) {
            if (substr($filename, -3) !== '.md') {
                $filename .= '.md';
            }
            if ($filename[0] !== '_') {
                $filename = '_'.$filename;
            }
            file_put_contents($dirname.$filename, "```json\n".$json."\n```\n");
        }

        return $json;
    }
}