From 984a7bda776a75a50d183d6b010ea8eb8f437e97 Mon Sep 17 00:00:00 2001 From: Arman <407448+armanist@users.noreply.github.com> Date: Tue, 29 Oct 2024 16:05:37 +0400 Subject: [PATCH] Updating PostService and unit tests --- composer.json | 5 +++ shared/Services/PostService.php | 3 -- tests/unit/AuthServiceTest.php | 9 ++-- tests/unit/PostServiceTest.php | 73 +++++++++++++-------------------- 4 files changed, 40 insertions(+), 50 deletions(-) diff --git a/composer.json b/composer.json index 2f0ffbc..f54fb3a 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,11 @@ "Shared\\": "shared" } }, + "autoload-dev": { + "psr-4": { + "Quantum\\Tests\\": "tests/unit/" + } + }, "scripts": { "post-create-project-cmd": [ "php qt core:env", diff --git a/shared/Services/PostService.php b/shared/Services/PostService.php index 09b42d1..90ef9ce 100644 --- a/shared/Services/PostService.php +++ b/shared/Services/PostService.php @@ -14,15 +14,12 @@ namespace Shared\Services; -use Quantum\Http\Request; use Quantum\Libraries\Database\PaginatorInterface; -use Quantum\Libraries\Transformer\TransformerInterface; use Quantum\Exceptions\FileSystemException; use Quantum\Exceptions\FileUploadException; use Quantum\Libraries\Storage\UploadedFile; use Quantum\Exceptions\DatabaseException; use Quantum\Libraries\Storage\FileSystem; -use Shared\Transformers\PostTransformer; use Quantum\Exceptions\ConfigException; use Quantum\Exceptions\ModelException; use Quantum\Exceptions\LangException; diff --git a/tests/unit/AuthServiceTest.php b/tests/unit/AuthServiceTest.php index ad6e1cb..176d94a 100644 --- a/tests/unit/AuthServiceTest.php +++ b/tests/unit/AuthServiceTest.php @@ -1,7 +1,10 @@ authService->get('email', 'anonymous@qt.com'); - $this->assertInstanceOf(\Quantum\Libraries\Auth\User::class, $user); + $this->assertInstanceOf(User::class, $user); $this->assertArrayHasKey('password', $user->getData()); $this->assertArrayHasKey('firstname', $user->getData()); $this->assertArrayHasKey('lastname', $user->getData()); @@ -65,7 +68,7 @@ public function testUserGetById() { $user = $this->authService->get('id', 1); - $this->assertInstanceOf(\Quantum\Libraries\Auth\User::class, $user); + $this->assertInstanceOf(User::class, $user); $userData = $user->getData(); @@ -81,7 +84,7 @@ public function testUserAdd() 'lastname' => 'User', ]); - $this->assertInstanceOf(\Quantum\Libraries\Auth\User::class, $user); + $this->assertInstanceOf(User::class, $user); $this->assertArrayHasKey('email', $user->getData()); $this->assertEquals('guest@qt.com', $user->getFieldValue('email')); } diff --git a/tests/unit/PostServiceTest.php b/tests/unit/PostServiceTest.php index 1596466..311f360 100644 --- a/tests/unit/PostServiceTest.php +++ b/tests/unit/PostServiceTest.php @@ -1,5 +1,7 @@ request = new Request(); - $this->fs = Di::get(FileSystem::class); $this->authService = ServiceFactory::get(AuthService::class, ['shared' . DS . 'store', 'users']); @@ -87,8 +82,6 @@ public function setUp(): void $this->postService = ServiceFactory::get(PostService::class, ['shared' . DS . 'store', 'posts']); - $this->request->set('per_page', 5); - foreach ($this->initialPosts as $post) { $this->postService->addPost($post); } @@ -105,41 +98,37 @@ public function testGetPosts() { $this->assertIsObject($this->postService); - $posts = $this->postService->getPosts($this->request); + $posts = $this->postService->getPosts(10, 1); - $this->assertInstanceOf(PaginatorInterface::class, $posts); - $this->assertIsArray($posts->data); + $this->assertInstanceOf(PaginatorInterface::class, $posts); - $this->assertCount(4, $posts->data); + $this->assertIsArray($posts->data()); - $post = $posts->data[0]; + $this->assertCount(4, $posts->data()); - $this->assertIsArray($post); + $post = $posts->data()[0]; - $this->assertArrayHasKey('author', $post); - - $this->assertEquals('Tom Hunter', $post['author']); + $this->assertIsObject($post); } public function testGetSinglePost() { - $posts = $this->postService->getPosts($this->request); + $posts = $this->postService->getPosts(10, 1); - $uuid = $posts->data[0]['id']; + $uuid = $posts->data()[0]->uuid; $post = $this->postService->getPost($uuid); - $this->assertIsArray($post); + $this->assertInstanceOf(Post::class, $post); - $this->assertArrayHasKey('id', $post); + $postData = $post->asArray(); - $this->assertArrayHasKey('title', $post); + $this->assertArrayHasKey('title', $postData); - $this->assertArrayHasKey('content', $post); + $this->assertArrayHasKey('content', $postData); - $this->assertArrayHasKey('date', $post); + $this->assertArrayHasKey('updated_at', $postData); - $this->assertArrayHasKey('author', $post); } public function testAddNewPost() @@ -158,22 +147,20 @@ public function testAddNewPost() $post = $this->postService->getPost($uuid); - $this->assertEquals('Just another post', $post['title']); - - $this->assertEquals('Content of just another post', $post['content']); + $this->assertEquals('Just another post', $post->title); - $this->assertEquals(date('Y/m/d H:i', strtotime($date)), $post['date']); + $this->assertEquals('Content of just another post', $post->content); - $this->assertEquals('Tom Hunter', $post['author']); + $this->assertEquals($date, $post->updated_at); } public function testUpdatePost() { $date = date('Y-m-d H:i:s'); - $posts = $this->postService->getPosts($this->request); + $posts = $this->postService->getPosts(10, 1); - $uuid = $posts->data[0]['id']; + $uuid = $posts->data()[0]->uuid; $this->postService->updatePost($uuid, [ 'title' => 'Walt Disney Jr.', @@ -184,22 +171,20 @@ public function testUpdatePost() $post = $this->postService->getPost($uuid); - $this->assertNotEquals('Lorem ipsum dolor sit amet', $post['title']); - - $this->assertEquals('Walt Disney Jr.', $post['title']); + $this->assertNotEquals('Lorem ipsum dolor sit amet', $post->title); - $this->assertEquals('The best way to get started is to quit talking and begin doing.', $post['content']); + $this->assertEquals('Walt Disney Jr.', $post->title); - $this->assertEquals($this->user->uuid . '/image.jpg', $post['image']); + $this->assertEquals('The best way to get started is to quit talking and begin doing.', $post->content); - $this->assertEquals(date('Y/m/d H:i', strtotime($date)), $post['date']); + $this->assertEquals('image.jpg', $post->image); - $this->assertEquals('Tom Hunter', $post['author']); + $this->assertEquals($date, $post->updated_at); } public function testDeletePost() { - $this->assertCount(4, $this->postService->getPosts($this->request)->data); + $this->assertCount(4, $this->postService->getPosts(10, 1)->data()); $post = $this->postService->addPost([ 'user_id' => 1, @@ -209,11 +194,11 @@ public function testDeletePost() 'updated_at' => date('Y-m-d H:i:s') ]); - $this->assertCount(5, $this->postService->getPosts($this->request)->data); + $this->assertCount(5, $this->postService->getPosts(10, 1)->data()); $this->postService->deletePost($post['uuid']); - $this->assertCount(4, $this->postService->getPosts($this->request)->data); + $this->assertCount(4, $this->postService->getPosts(10, 1)->data()); } public function testSaveDeleteImage()