diff --git a/README.md b/README.md index 6026fe66..eecc8db5 100644 --- a/README.md +++ b/README.md @@ -26,13 +26,14 @@ I implemented this API in [this project](https://github.com/maurobonfietti/rest- ## :gear: QUICK INSTALL: -### Pre Requisite: +### Requirements: - Git. - Composer. -- PHP 7.3 / 7.4. +- PHP 7.3+. - MySQL/MariaDB. - Redis (Optional). +- or Docker. ### With Composer: diff --git a/README_SPANISH.md b/README_SPANISH.md index 2561c70a..3decab73 100644 --- a/README_SPANISH.md +++ b/README_SPANISH.md @@ -26,13 +26,14 @@ Implementé esta API en [este proyecto](https://github.com/maurobonfietti/rest-a ## :gear: INSTALACIÓN RÁPIDA: -### Pre Requisitos: +### Requerimientos: - Git. - Composer. -- PHP 7.3 / 7.4. +- PHP 7.3+. - MySQL/MariaDB. - Redis (Opcional). +- O simplemente Docker. ### Usando Composer: diff --git a/src/App/Routes.php b/src/App/Routes.php index 18fea08e..0ebf2b32 100644 --- a/src/App/Routes.php +++ b/src/App/Routes.php @@ -14,28 +14,28 @@ $app->group('/api/v1', function () use ($app): void { $app->group('/tasks', function () use ($app): void { $app->get('', Task\GetAll::class); - $app->get('/[{id}]', Task\GetOne::class); - $app->get('/search/[{query}]', Task\Search::class); $app->post('', Task\Create::class); - $app->put('/[{id}]', Task\Update::class); - $app->delete('/[{id}]', Task\Delete::class); + $app->get('/search/[{query}]', Task\Search::class); + $app->get('/{id}', Task\GetOne::class); + $app->put('/{id}', Task\Update::class); + $app->delete('/{id}', Task\Delete::class); })->add(new Auth()); $app->group('/users', function () use ($app): void { $app->get('', User\GetAll::class)->add(new Auth()); - $app->get('/[{id}]', User\GetOne::class)->add(new Auth()); - $app->get('/search/[{query}]', User\Search::class)->add(new Auth()); $app->post('', User\Create::class); - $app->put('/[{id}]', User\Update::class)->add(new Auth()); - $app->delete('/[{id}]', User\Delete::class)->add(new Auth()); + $app->get('/search/[{query}]', User\Search::class)->add(new Auth()); + $app->get('/{id}', User\GetOne::class)->add(new Auth()); + $app->put('/{id}', User\Update::class)->add(new Auth()); + $app->delete('/{id}', User\Delete::class)->add(new Auth()); }); $app->group('/notes', function () use ($app): void { $app->get('', Note\GetAll::class); - $app->get('/[{id}]', Note\GetOne::class); - $app->get('/search/[{query}]', Note\Search::class); $app->post('', Note\Create::class); - $app->put('/[{id}]', Note\Update::class); - $app->delete('/[{id}]', Note\Delete::class); + $app->get('/search/[{query}]', Note\Search::class); + $app->get('/{id}', Note\GetOne::class); + $app->put('/{id}', Note\Update::class); + $app->delete('/{id}', Note\Delete::class); }); }); diff --git a/src/Controller/BaseController.php b/src/Controller/BaseController.php index 53f06be4..e101adb8 100644 --- a/src/Controller/BaseController.php +++ b/src/Controller/BaseController.php @@ -4,12 +4,18 @@ namespace App\Controller; +use Slim\Container; use Slim\Http\Response; abstract class BaseController { protected $container; + public function __construct(Container $container) + { + $this->container = $container; + } + protected function jsonResponse( Response $response, string $status, diff --git a/src/Controller/DefaultController.php b/src/Controller/DefaultController.php index 1f7ab661..ca0e6411 100644 --- a/src/Controller/DefaultController.php +++ b/src/Controller/DefaultController.php @@ -4,18 +4,12 @@ namespace App\Controller; -use Slim\Container; use Slim\Http\Request; use Slim\Http\Response; final class DefaultController extends BaseController { - public const API_VERSION = '1.7.0'; - - public function __construct(Container $container) - { - $this->container = $container; - } + public const API_VERSION = '1.8.0'; public function getHelp(Request $request, Response $response): Response { diff --git a/src/Controller/Note/Base.php b/src/Controller/Note/Base.php index 2c84c63f..eff95601 100644 --- a/src/Controller/Note/Base.php +++ b/src/Controller/Note/Base.php @@ -6,15 +6,9 @@ use App\Controller\BaseController; use App\Service\Note\NoteService; -use Slim\Container; abstract class Base extends BaseController { - public function __construct(Container $container) - { - $this->container = $container; - } - protected function getNoteService(): NoteService { return $this->container->get('note_service'); diff --git a/src/Controller/Task/Base.php b/src/Controller/Task/Base.php index 1f69fd46..6beb9c16 100644 --- a/src/Controller/Task/Base.php +++ b/src/Controller/Task/Base.php @@ -6,15 +6,9 @@ use App\Controller\BaseController; use App\Service\Task\TaskService; -use Slim\Container; abstract class Base extends BaseController { - public function __construct(Container $container) - { - $this->container = $container; - } - protected function getTaskService(): TaskService { return $this->container->get('task_service'); diff --git a/src/Controller/User/Base.php b/src/Controller/User/Base.php index 3d18b206..2033f14b 100644 --- a/src/Controller/User/Base.php +++ b/src/Controller/User/Base.php @@ -7,15 +7,9 @@ use App\Controller\BaseController; use App\Exception\User; use App\Service\User\UserService; -use Slim\Container; abstract class Base extends BaseController { - public function __construct(Container $container) - { - $this->container = $container; - } - protected function getUserService(): UserService { return $this->container->get('user_service'); diff --git a/src/Service/Note/Base.php b/src/Service/Note/Base.php index 9384a52d..8bb870dd 100644 --- a/src/Service/Note/Base.php +++ b/src/Service/Note/Base.php @@ -28,7 +28,7 @@ public function __construct( protected static function validateNoteName(string $name): string { - if (! v::length(2, 50)->validate($name)) { + if (! v::length(1, 50)->validate($name)) { throw new Note('The name of the note is invalid.', 400); } diff --git a/src/Service/Task/Base.php b/src/Service/Task/Base.php index b5f75ced..dd4ab509 100644 --- a/src/Service/Task/Base.php +++ b/src/Service/Task/Base.php @@ -33,7 +33,7 @@ protected function getTaskRepository(): TaskRepository protected static function validateTaskName(string $name): string { - if (! v::length(2, 100)->validate($name)) { + if (! v::length(1, 100)->validate($name)) { throw new Task('Invalid name.', 400); } diff --git a/src/Service/User/Base.php b/src/Service/User/Base.php index 493f91a3..704af896 100644 --- a/src/Service/User/Base.php +++ b/src/Service/User/Base.php @@ -28,7 +28,7 @@ public function __construct( protected static function validateUserName(string $name): string { - if (! v::alnum()->length(2, 100)->validate($name)) { + if (! v::alnum()->length(1, 100)->validate($name)) { throw new User('Invalid name.', 400); } diff --git a/tests/integration/BaseTestCase.php b/tests/integration/BaseTestCase.php index dabb7a09..da7d5f84 100644 --- a/tests/integration/BaseTestCase.php +++ b/tests/integration/BaseTestCase.php @@ -14,8 +14,12 @@ class BaseTestCase extends \PHPUnit\Framework\TestCase { public static $jwt = ''; - public function runApp(string $requestMethod, string $requestUri, array $requestData = null): ResponseInterface - { + public function runApp( + string $requestMethod, + string $requestUri, + array $requestData = null + ): ResponseInterface { + $environment = Environment::mock( [ 'REQUEST_METHOD' => $requestMethod, diff --git a/tests/integration/DefaultTest.php b/tests/integration/DefaultTest.php index 5e95ed82..18d6b97f 100644 --- a/tests/integration/DefaultTest.php +++ b/tests/integration/DefaultTest.php @@ -13,16 +13,18 @@ public function testApiHelp(): void { $response = $this->runApp('GET', '/'); + $result = (string) $response->getBody(); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('application/json', $response->getHeaderLine('Content-Type')); - $this->assertStringContainsString('status', (string) $response->getBody()); - $this->assertStringContainsString('success', (string) $response->getBody()); - $this->assertStringContainsString('version', (string) $response->getBody()); - $this->assertStringContainsString('time', (string) $response->getBody()); - $this->assertStringContainsString('endpoints', (string) $response->getBody()); - $this->assertStringContainsString('help', (string) $response->getBody()); - $this->assertStringNotContainsString('error', (string) $response->getBody()); - $this->assertStringNotContainsString('Failed', (string) $response->getBody()); + $this->assertStringContainsString('status', $result); + $this->assertStringContainsString('success', $result); + $this->assertStringContainsString('version', $result); + $this->assertStringContainsString('time', $result); + $this->assertStringContainsString('endpoints', $result); + $this->assertStringContainsString('help', $result); + $this->assertStringNotContainsString('error', $result); + $this->assertStringNotContainsString('Failed', $result); } /** @@ -32,17 +34,19 @@ public function testStatus(): void { $response = $this->runApp('GET', '/status'); + $result = (string) $response->getBody(); + $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals('application/json', $response->getHeaderLine('Content-Type')); - $this->assertStringContainsString('status', (string) $response->getBody()); - $this->assertStringContainsString('success', (string) $response->getBody()); - $this->assertStringContainsString('message', (string) $response->getBody()); - $this->assertStringContainsString('stats', (string) $response->getBody()); - $this->assertStringContainsString('MySQL', (string) $response->getBody()); - $this->assertStringContainsString('Redis', (string) $response->getBody()); - $this->assertStringContainsString('version', (string) $response->getBody()); - $this->assertStringContainsString('time', (string) $response->getBody()); - $this->assertStringNotContainsString('error', (string) $response->getBody()); - $this->assertStringNotContainsString('Failed', (string) $response->getBody()); + $this->assertStringContainsString('status', $result); + $this->assertStringContainsString('success', $result); + $this->assertStringContainsString('message', $result); + $this->assertStringContainsString('stats', $result); + $this->assertStringContainsString('MySQL', $result); + $this->assertStringContainsString('Redis', $result); + $this->assertStringContainsString('version', $result); + $this->assertStringContainsString('time', $result); + $this->assertStringNotContainsString('error', $result); + $this->assertStringNotContainsString('Failed', $result); } } diff --git a/tests/integration/NoteTest.php b/tests/integration/NoteTest.php index 2d115529..8b37578c 100644 --- a/tests/integration/NoteTest.php +++ b/tests/integration/NoteTest.php @@ -164,7 +164,7 @@ public function testCreateNoteWithInvalidName(): void { $response = $this->runApp( 'POST', '/api/v1/notes', - ['name' => 'z'] + ['name' => ''] ); $result = (string) $response->getBody(); diff --git a/tests/integration/TaskTest.php b/tests/integration/TaskTest.php index ca61863b..868089c7 100644 --- a/tests/integration/TaskTest.php +++ b/tests/integration/TaskTest.php @@ -197,7 +197,7 @@ public function testCreateTaskWithOutTaskName(): void public function testCreateTaskWithInvalidTaskName(): void { $response = $this->runApp( - 'POST', '/api/v1/tasks', ['name' => 'z', 'status' => 1] + 'POST', '/api/v1/tasks', ['name' => '', 'status' => 1] ); $result = (string) $response->getBody(); diff --git a/tests/integration/UserTest.php b/tests/integration/UserTest.php index 904a0c9f..15335f39 100644 --- a/tests/integration/UserTest.php +++ b/tests/integration/UserTest.php @@ -293,7 +293,7 @@ public function testUpdateUserWithInvalidData(): void { $response = $this->runApp( 'PUT', '/api/v1/users/' . self::$id, - ['name' => 'z', 'email' => 'email-incorrecto...'] + ['name' => '', 'email' => 'email-incorrecto...'] ); $result = (string) $response->getBody(); diff --git a/tests/unit/Service/UserServiceTest.php b/tests/unit/Service/UserServiceTest.php index 695d7707..95fe0624 100644 --- a/tests/unit/Service/UserServiceTest.php +++ b/tests/unit/Service/UserServiceTest.php @@ -8,9 +8,6 @@ class UserServiceTest extends BaseTestCase { - /** - * @var int - */ private static $id; private function getDatabase(): \PDO