Skip to content

Commit 309784a

Browse files
committed
[#.x] - reworking tests - moving dispatch tests to own file
1 parent 76dfc8c commit 309784a

File tree

2 files changed

+86
-53
lines changed

2 files changed

+86
-53
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

tests/Unit/Domain/Services/User/UserServiceTest.php renamed to tests/Unit/Domain/Services/User/UserServiceGetTest.php

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,64 +15,14 @@
1515

1616
use PayloadInterop\DomainStatus;
1717
use Phalcon\Api\Domain\Components\Container;
18-
use Phalcon\Api\Domain\Components\DataSource\TransportRepository;
19-
use Phalcon\Api\Domain\Components\Env\EnvManager;
2018
use Phalcon\Api\Domain\Services\User\UserGetService;
2119
use Phalcon\Api\Tests\AbstractUnitTestCase;
2220
use Phalcon\Api\Tests\Fixtures\Domain\Migrations\UsersMigration;
2321
use PHPUnit\Framework\Attributes\BackupGlobals;
2422

2523
#[BackupGlobals(true)]
26-
final class UserServiceTest extends AbstractUnitTestCase
24+
final class UserServiceGetTest extends AbstractUnitTestCase
2725
{
28-
public function testDispatch(): void
29-
{
30-
/** @var EnvManager $env */
31-
$env = $this->container->getShared(Container::ENV);
32-
/** @var TransportRepository $transport */
33-
$transport = $this->container->get(Container::REPOSITORY_TRANSPORT);
34-
35-
$migration = new UsersMigration($this->getConnection());
36-
$dbUser = $this->getNewUser($migration);
37-
$userId = $dbUser['usr_id'];
38-
$token = $this->getUserToken($dbUser);
39-
40-
$time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
41-
$_SERVER = [
42-
'REQUEST_METHOD' => 'GET',
43-
'REQUEST_TIME_FLOAT' => $time,
44-
'HTTP_AUTHORIZATION' => 'Bearer ' . $token,
45-
'REQUEST_URI' => '/user',
46-
];
47-
48-
$_GET = [
49-
'userId' => $userId,
50-
];
51-
52-
ob_start();
53-
require_once $env->appPath('public/index.php');
54-
$response = ob_get_clean();
55-
56-
$contents = json_decode($response, true);
57-
58-
restore_error_handler();
59-
60-
$this->assertArrayHasKey('data', $contents);
61-
$this->assertArrayHasKey('errors', $contents);
62-
63-
$data = $contents['data'];
64-
$errors = $contents['errors'];
65-
66-
$expected = [];
67-
$actual = $errors;
68-
$this->assertSame($expected, $actual);
69-
70-
$user = $transport->newUser($dbUser);
71-
$expected = $user->toArray();
72-
$actual = $data;
73-
$this->assertSame($expected, $actual);
74-
}
75-
7626
public function testServiceEmptyUserId(): void
7727
{
7828
/** @var UserGetService $service */
@@ -103,7 +53,7 @@ public function testServiceWithUserId(): void
10353

10454
$payload = $service->__invoke(
10555
[
106-
'userId' => $userId,
56+
'id' => $userId,
10757
]
10858
);
10959

@@ -194,7 +144,7 @@ public function testServiceWrongUserId(): void
194144

195145
$payload = $service->__invoke(
196146
[
197-
'userId' => 999999,
147+
'id' => 999999,
198148
]
199149
);
200150

0 commit comments

Comments
 (0)