|
| 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\Services\User; |
| 15 | + |
| 16 | +use Phalcon\Api\Domain\Components\Cache\Cache; |
| 17 | +use Phalcon\Api\Domain\Components\Container; |
| 18 | +use Phalcon\Api\Domain\Components\DataSource\TransportRepository; |
| 19 | +use Phalcon\Api\Domain\Components\Enums\Http\RoutesEnum; |
| 20 | +use Phalcon\Api\Domain\Components\Env\EnvManager; |
| 21 | +use Phalcon\Api\Tests\AbstractUnitTestCase; |
| 22 | +use Phalcon\Api\Tests\Fixtures\Domain\Migrations\UsersMigration; |
| 23 | +use PHPUnit\Framework\Attributes\BackupGlobals; |
| 24 | + |
| 25 | +#[BackupGlobals(true)] |
| 26 | +final class UserServiceDispatchTest extends AbstractUnitTestCase |
| 27 | +{ |
| 28 | + public function testDispatchGet(): void |
| 29 | + { |
| 30 | + /** @var EnvManager $env */ |
| 31 | + $env = $this->container->getShared(Container::ENV); |
| 32 | + /** @var Cache $cache */ |
| 33 | + $cache = $this->container->getShared(Container::CACHE); |
| 34 | + /** @var TransportRepository $transport */ |
| 35 | + $transport = $this->container->get(Container::REPOSITORY_TRANSPORT); |
| 36 | + |
| 37 | + $migration = new UsersMigration($this->getConnection()); |
| 38 | + $dbUser = $this->getNewUser($migration); |
| 39 | + $userId = $dbUser['usr_id']; |
| 40 | + $token = $this->getUserToken($dbUser); |
| 41 | + $domainUser = $transport->newUser($dbUser); |
| 42 | + |
| 43 | + /** |
| 44 | + * Store the token in the cache |
| 45 | + */ |
| 46 | + $cache->storeTokenInCache($env, $domainUser, $token); |
| 47 | + |
| 48 | + $time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time(); |
| 49 | + $_SERVER = [ |
| 50 | + 'REQUEST_METHOD' => 'GET', |
| 51 | + 'REQUEST_TIME_FLOAT' => $time, |
| 52 | + 'HTTP_AUTHORIZATION' => 'Bearer ' . $token, |
| 53 | + 'REQUEST_URI' => RoutesEnum::userGet->endpoint(), |
| 54 | + ]; |
| 55 | + |
| 56 | + $_GET = [ |
| 57 | + 'id' => $userId, |
| 58 | + ]; |
| 59 | + |
| 60 | + ob_start(); |
| 61 | + require_once $env->appPath('public/index.php'); |
| 62 | + $response = ob_get_clean(); |
| 63 | + |
| 64 | + $contents = json_decode($response, true); |
| 65 | + |
| 66 | + restore_error_handler(); |
| 67 | + |
| 68 | + $this->assertArrayHasKey('data', $contents); |
| 69 | + $this->assertArrayHasKey('errors', $contents); |
| 70 | + |
| 71 | + $data = $contents['data']; |
| 72 | + $errors = $contents['errors']; |
| 73 | + |
| 74 | + $expected = []; |
| 75 | + $actual = $errors; |
| 76 | + $this->assertSame($expected, $actual); |
| 77 | + |
| 78 | + $user = $transport->newUser($dbUser); |
| 79 | + $expected = $user->toArray(); |
| 80 | + $actual = $data; |
| 81 | + $this->assertSame($expected, $actual); |
| 82 | + } |
| 83 | +} |
0 commit comments