Skip to content

Commit 8a8e791

Browse files
committed
[#.x] - tests and more tests!
1 parent cfb2181 commit 8a8e791

12 files changed

+848
-0
lines changed

tests/Unit/AbstractUnitTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717

1818
abstract class AbstractUnitTestCase extends TestCase
1919
{
20+
/**
21+
* @param string $fileName
22+
* @param string $stream
23+
*
24+
* @return void
25+
*/
26+
public function assertFileContentsContains(string $fileName, string $stream): void
27+
{
28+
$contents = file_get_contents($fileName);
29+
$this->assertStringContainsString($stream, $contents);
30+
}
31+
2032
/**
2133
* Return a long series of strings to be used as a password
2234
*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Middleware;
15+
16+
use Phalcon\Api\Domain\Middleware\HealthMiddleware;
17+
use Phalcon\Api\Domain\Services\Container;
18+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
19+
use Phalcon\Mvc\Micro;
20+
use PHPUnit\Framework\Attributes\BackupGlobals;
21+
22+
use function ob_get_clean;
23+
use function ob_start;
24+
25+
#[BackupGlobals(true)]
26+
final class HealthMiddlewareTest extends AbstractUnitTestCase
27+
{
28+
public function testCall(): void
29+
{
30+
$container = new Container();
31+
$application = new Micro($container);
32+
$middleware = new HealthMiddleware();
33+
34+
$time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
35+
$_SERVER = [
36+
'REQUEST_METHOD' => 'GET',
37+
'REQUEST_TIME_FLOAT' => $time,
38+
'REQUEST_URI' => '/health',
39+
];
40+
41+
ob_start();
42+
$actual = $middleware->call($application);
43+
$contents = ob_get_clean();
44+
45+
$this->assertTrue($actual);
46+
47+
$contents = json_decode($contents, true);
48+
49+
$expected = [
50+
'status' => 'ok',
51+
'message' => 'service operational',
52+
];
53+
$actual = $contents['data'];
54+
$this->assertSame($expected, $actual);
55+
}
56+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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\Middleware;
15+
16+
use Phalcon\Api\Domain\Middleware\NotFoundMiddleware;
17+
use Phalcon\Api\Domain\Services\Container;
18+
use Phalcon\Api\Domain\Services\Http\HttpCodesEnum;
19+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
20+
use Phalcon\Events\Event;
21+
use Phalcon\Mvc\Micro;
22+
use PHPUnit\Framework\Attributes\BackupGlobals;
23+
24+
use function ob_get_clean;
25+
use function ob_start;
26+
27+
#[BackupGlobals(true)]
28+
final class NotFoundMiddlewareTest extends AbstractUnitTestCase
29+
{
30+
public function testBeforeNotFound(): void
31+
{
32+
$container = new Container();
33+
$application = new Micro($container);
34+
$middleware = new NotFoundMiddleware();
35+
36+
$time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
37+
$_SERVER = [
38+
'REQUEST_METHOD' => 'GET',
39+
'REQUEST_TIME_FLOAT' => $time,
40+
'REQUEST_URI' => '/unknown',
41+
];
42+
43+
ob_start();
44+
$actual = $middleware->beforeNotFound(
45+
new Event('ev1'),
46+
$application
47+
);
48+
$contents = ob_get_clean();
49+
50+
$this->assertFalse($actual);
51+
52+
$contents = json_decode($contents, true);
53+
54+
$expected = [HttpCodesEnum::AppResourceNotFound->error()];
55+
$actual = $contents['errors'];
56+
$this->assertSame($expected, $actual);
57+
}
58+
59+
public function testCall(): void
60+
{
61+
$container = new Container();
62+
$application = new Micro($container);
63+
$middleware = new NotFoundMiddleware();
64+
65+
$time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
66+
$_SERVER = [
67+
'REQUEST_METHOD' => 'GET',
68+
'REQUEST_TIME_FLOAT' => $time,
69+
'REQUEST_URI' => '/unknown',
70+
];
71+
72+
ob_start();
73+
$actual = $middleware->call($application);
74+
ob_get_clean();
75+
76+
$this->assertTrue($actual);
77+
}
78+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\Middleware;
15+
16+
use Phalcon\Api\Domain\Middleware\ResponseSenderMiddleware;
17+
use Phalcon\Api\Domain\Services\Container;
18+
use Phalcon\Api\Domain\Services\Http\Response;
19+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
20+
use Phalcon\Mvc\Micro;
21+
use PHPUnit\Framework\Attributes\BackupGlobals;
22+
23+
use function ob_get_clean;
24+
use function ob_start;
25+
use function uniqid;
26+
27+
#[BackupGlobals(true)]
28+
final class ResponseSenderMiddlewareTest extends AbstractUnitTestCase
29+
{
30+
public function testCall(): void
31+
{
32+
$container = new Container();
33+
$application = new Micro($container);
34+
$middleware = new ResponseSenderMiddleware();
35+
/** @var Response $response */
36+
$response = $container->getShared(Container::RESPONSE);
37+
38+
$content = uniqid('content-');
39+
$response->setContent($content);
40+
41+
$time = $_SERVER['REQUEST_TIME_FLOAT'] ?? time();
42+
$_SERVER = [
43+
'REQUEST_METHOD' => 'GET',
44+
'REQUEST_TIME_FLOAT' => $time,
45+
'REQUEST_URI' => '/health',
46+
];
47+
48+
ob_start();
49+
$actual = $middleware->call($application);
50+
$contents = ob_get_clean();
51+
52+
$this->assertTrue($actual);
53+
54+
$expected = $content;
55+
$actual = $contents;
56+
$this->assertEquals($expected, $actual);
57+
}
58+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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;
15+
16+
use Phalcon\Api\Domain\Services\Container;
17+
use Phalcon\Api\Tests\Unit\AbstractUnitTestCase;
18+
use Phalcon\Events\Manager as EventsManager;
19+
use Phalcon\Filter\Filter;
20+
21+
final class ContainerTest extends AbstractUnitTestCase
22+
{
23+
public function testContainerEventManager(): void
24+
{
25+
$container = new Container();
26+
27+
$actual = $container->has(Container::EVENTS_MANAGER);
28+
$this->assertTrue($actual);
29+
30+
$eventsManager = $container->getShared(Container::EVENTS_MANAGER);
31+
$this->assertInstanceOf(EventsManager::class, $eventsManager);
32+
}
33+
34+
public function testContainerFilter(): void
35+
{
36+
$container = new Container();
37+
38+
$actual = $container->has(Container::FILTER);
39+
$this->assertTrue($actual);
40+
41+
$eventsManager = $container->getShared(Container::FILTER);
42+
$this->assertInstanceOf(Filter::class, $eventsManager);
43+
}
44+
}

0 commit comments

Comments
 (0)