|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Phalcon API. |
| 5 | + * |
| 6 | + * (c) Phalcon Team <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Phalcon\Api\Tests\Unit\Domain\Hello; |
| 15 | + |
| 16 | +use PayloadInterop\DomainStatus; |
| 17 | +use Phalcon\Api\Domain\Hello\HelloService; |
| 18 | +use Phalcon\Api\Domain\Services\Container; |
| 19 | +use Phalcon\Api\Domain\Services\Env\EnvManager; |
| 20 | +use Phalcon\Api\Tests\Unit\AbstractUnitTestCase; |
| 21 | +use PHPUnit\Framework\Attributes\BackupGlobals; |
| 22 | + |
| 23 | +use function ob_get_clean; |
| 24 | +use function ob_start; |
| 25 | +use function restore_error_handler; |
| 26 | +use function time; |
| 27 | + |
| 28 | +#[BackupGlobals(true)] |
| 29 | +final class HelloServiceTest extends AbstractUnitTestCase |
| 30 | +{ |
| 31 | + public function testDispatch(): void |
| 32 | + { |
| 33 | + $time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time(); |
| 34 | + $_SERVER = [ |
| 35 | + 'REQUEST_METHOD' => 'GET', |
| 36 | + 'REQUEST_TIME_FLOAT' => $time, |
| 37 | + 'REQUEST_URI' => '/', |
| 38 | + ]; |
| 39 | + |
| 40 | + ob_start(); |
| 41 | + require_once EnvManager::appPath('public/index.php'); |
| 42 | + $response = ob_get_clean(); |
| 43 | + |
| 44 | + $contents = json_decode($response, true); |
| 45 | + |
| 46 | + restore_error_handler(); |
| 47 | + |
| 48 | + $this->assertArrayHasKey('data', $contents); |
| 49 | + $this->assertArrayHasKey('errors', $contents); |
| 50 | + |
| 51 | + $data = $contents['data']; |
| 52 | + $errors = $contents['errors']; |
| 53 | + |
| 54 | + $expected = []; |
| 55 | + $actual = $errors; |
| 56 | + $this->assertSame($expected, $actual); |
| 57 | + |
| 58 | + $expected = 'Hello World!!! - '; |
| 59 | + $actual = $data[0]; |
| 60 | + $this->assertStringContainsString($expected, $actual); |
| 61 | + } |
| 62 | + |
| 63 | + public function testService(): void |
| 64 | + { |
| 65 | + $container = new Container(); |
| 66 | + /** @var HelloService $service */ |
| 67 | + $service = $container->get(Container::HELLO_SERVICE); |
| 68 | + |
| 69 | + $payload = $service->__invoke(); |
| 70 | + |
| 71 | + $expected = DomainStatus::SUCCESS; |
| 72 | + $actual = $payload->getStatus(); |
| 73 | + $this->assertSame($expected, $actual); |
| 74 | + |
| 75 | + $actual = $payload->getResult(); |
| 76 | + $this->assertArrayHasKey('results', $actual); |
| 77 | + |
| 78 | + $expected = 'Hello World!!! - '; |
| 79 | + $actual = $actual['results']; |
| 80 | + $this->assertStringContainsString($expected, $actual); |
| 81 | + } |
| 82 | +} |
0 commit comments