• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / testing / 16211940240

03 Oct 2024 09:03PM UTC coverage: 100.0%. Remained the same
16211940240

push

github

natanfelles
Export-ignore SECURITY.md

280 of 280 relevant lines covered (100.0%)

4.9 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/AppTesting.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Testing Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Testing;
11

12
use Closure;
13
use Framework\CLI\Streams\Stderr;
14
use Framework\CLI\Streams\Stdout;
15
use Framework\Config\Config;
16
use Framework\HTTP\URL;
17
use Framework\MVC\App;
18

19
/**
20
 * Class AppTesting.
21
 *
22
 * @package testing
23
 */
24
class AppTesting
25
{
26
    protected App $app;
27

28
    /**
29
     * AppTesting constructor.
30
     *
31
     * @param Config $config
32
     */
33
    public function __construct(Config $config)
34
    {
35
        $this->app = new class($config) extends App {
44✔
36
            public function runCliWithExec(string $command) : void
37
            {
38
                $this->prepareToRun();
11✔
39
                static::console()->exec($command);
11✔
40
            }
41
        };
44✔
42
        // We need to restore the error handler to avoid conflict with PHPUnit.
43
        // See: https://github.com/sebastianbergmann/phpunit/issues/5403
44
        //\restore_error_handler();
45
    }
46

47
    /**
48
     * Run App in a Closure suppressing the output buffer.
49
     *
50
     * It avoids buffer to be output in the PHPUnit terminal.
51
     *
52
     * @param Closure $closure
53
     */
54
    protected function suppressOutputBuffer(Closure $closure) : void
55
    {
56
        \ob_start(static fn () => '');
27✔
57
        try {
58
            $closure($this->app);
27✔
59
        } finally {
60
            \ob_end_clean();
27✔
61
        }
62
    }
63

64
    /**
65
     * Simulate a CLI request for tests.
66
     *
67
     * @param string $command Command line
68
     * @param array<string,string> $env Environment variables
69
     *
70
     * @return void
71
     */
72
    public function runCli(string $command, array $env = []) : void
73
    {
74
        App::setIsCli(true);
11✔
75
        Stderr::init();
11✔
76
        Stdout::init();
11✔
77
        $this->suppressOutputBuffer(static function (App $app) use ($command) : void {
11✔
78
            if ($command === '') {
11✔
79
                $command = 'index';
1✔
80
            }
81
            $app->runCliWithExec($command); // @phpstan-ignore-line
11✔
82
        });
11✔
83
    }
84

85
    /**
86
     * Simulate an HTTP Request for tests.
87
     *
88
     * @param URL|string $url
89
     * @param string $method
90
     * @param array<string,string> $headers
91
     * @param string $body
92
     *
93
     * @return void
94
     */
95
    public function runHttp(
96
        URL | string $url,
97
        string $method = 'GET',
98
        array $headers = [],
99
        string $body = ''
100
    ) : void {
101
        App::setIsCli(false);
17✔
102
        if (!$url instanceof URL) {
17✔
103
            $url = new URL($url);
17✔
104
        }
105
        $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
16✔
106
        $_SERVER['REQUEST_METHOD'] = \strtoupper($method);
16✔
107
        $_SERVER['REQUEST_SCHEME'] = $url->getScheme();
16✔
108
        $_SERVER['HTTP_HOST'] = $url->getHost();
16✔
109
        $query = $url->getQuery();
16✔
110
        $query = $query === null ? '' : '?' . $query;
16✔
111
        $_SERVER['REQUEST_URI'] = $url->getPath() . $query;
16✔
112
        foreach ($headers as $name => $value) {
16✔
113
            $name = \strtoupper($name);
2✔
114
            $name = \strtr($name, ['-' => '_']);
2✔
115
            $_SERVER['HTTP_' . $name] = $value;
2✔
116
        }
117
        if (isset($_SERVER['HTTP_COOKIE'])) {
16✔
118
            $cookies = \explode(';', $_SERVER['HTTP_COOKIE']);
1✔
119
            foreach ($cookies as $cookie) {
1✔
120
                $cookie = \explode('=', $cookie, 2);
1✔
121
                $cookie = \array_pad($cookie, 2, '');
1✔
122
                $cookie[0] = \ltrim($cookie[0]);
1✔
123
                $cookie[0] = \strtr($cookie[0], [' ' => '_']);
1✔
124
                $_COOKIE[$cookie[0]] = $cookie[1];
1✔
125
            }
126
        }
127
        $_GET = $url->getQueryData();
16✔
128
        App::request()->setBody($body); // @phpstan-ignore-line
16✔
129
        if ($_SERVER['REQUEST_METHOD'] === 'POST'
16✔
130
            && isset($_SERVER['HTTP_CONTENT_TYPE'])
16✔
131
            && $_SERVER['HTTP_CONTENT_TYPE'] === 'application/x-www-form-urlencoded'
16✔
132
        ) {
133
            \parse_str($body, $_POST);
1✔
134
        }
135
        $this->suppressOutputBuffer(static function (App $app) : void {
16✔
136
            $app->runHttp();
16✔
137
        });
16✔
138
    }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc