Skip to content

Commit

Permalink
correcting unit tests after adding pagination feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Vahram1995 committed Sep 12, 2024
1 parent 303196d commit f443c4a
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions tests/unit/PostServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

use Quantum\Libraries\Database\PaginatorInterface;
use Quantum\Libraries\Storage\UploadedFile;
use Quantum\Libraries\Storage\FileSystem;
use Quantum\Factory\ServiceFactory;
use Shared\Services\AuthService;
use Shared\Services\PostService;
use PHPUnit\Framework\TestCase;
use Quantum\Http\Request;
use Quantum\Di\Di;
use Quantum\App;

Expand Down Expand Up @@ -62,6 +64,11 @@ class PostServiceTest extends TestCase
private $fs;
private $user;

/**
* @var Request
*/
private $request;

public function setUp(): void
{
App::loadCoreFunctions(dirname(__DIR__, 2) . DS . 'vendor' . DS . 'quantum' . DS . 'framework' . DS . 'src' . DS . 'Helpers');
Expand All @@ -70,6 +77,8 @@ public function setUp(): void

Di::loadDefinitions();

$this->request = new Request();

$this->fs = Di::get(FileSystem::class);

$this->authService = ServiceFactory::get(AuthService::class, ['shared' . DS . 'store', 'users']);
Expand All @@ -78,6 +87,8 @@ 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);
}
Expand All @@ -94,13 +105,14 @@ public function testGetPosts()
{
$this->assertIsObject($this->postService);

$posts = $this->postService->getPosts();
$posts = $this->postService->getPosts($this->request);

$this->assertIsArray($posts);
$this->assertInstanceOf(PaginatorInterface::class, $posts);
$this->assertIsArray($posts->data);

$this->assertCount(4, $posts);
$this->assertCount(4, $posts->data);

$post = $posts[0];
$post = $posts->data[0];

$this->assertIsArray($post);

Expand All @@ -111,7 +123,9 @@ public function testGetPosts()

public function testGetSinglePost()
{
$uuid = $this->postService->getPosts()[0]['id'];
$posts = $this->postService->getPosts($this->request);

$uuid = $posts->data[0]['id'];

$post = $this->postService->getPost($uuid);

Expand Down Expand Up @@ -157,7 +171,9 @@ public function testUpdatePost()
{
$date = date('Y-m-d H:i:s');

$uuid = $this->postService->getPosts()[0]['id'];
$posts = $this->postService->getPosts($this->request);

$uuid = $posts->data[0]['id'];

$this->postService->updatePost($uuid, [
'title' => 'Walt Disney Jr.',
Expand All @@ -183,7 +199,7 @@ public function testUpdatePost()

public function testDeletePost()
{
$this->assertCount(4, $this->postService->getPosts());
$this->assertCount(4, $this->postService->getPosts($this->request)->data);

$post = $this->postService->addPost([
'user_id' => 1,
Expand All @@ -193,11 +209,11 @@ public function testDeletePost()
'updated_at' => date('Y-m-d H:i:s')
]);

$this->assertCount(5, $this->postService->getPosts());
$this->assertCount(5, $this->postService->getPosts($this->request)->data);

$this->postService->deletePost($post['uuid']);

$this->assertCount(4, $this->postService->getPosts());
$this->assertCount(4, $this->postService->getPosts($this->request)->data);
}

public function testSaveDeleteImage()
Expand Down

0 comments on commit f443c4a

Please sign in to comment.