Skip to content

Commit

Permalink
Updating PostService and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
armanist committed Oct 29, 2024
1 parent a7ff958 commit 984a7bd
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 50 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
"Shared\\": "shared"
}
},
"autoload-dev": {
"psr-4": {
"Quantum\\Tests\\": "tests/unit/"
}
},
"scripts": {
"post-create-project-cmd": [
"php qt core:env",
Expand Down
3 changes: 0 additions & 3 deletions shared/Services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 6 additions & 3 deletions tests/unit/AuthServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

namespace Quantum\Tests;

use Quantum\Libraries\Storage\FileSystem;
use Quantum\Factory\ServiceFactory;
use Quantum\Libraries\Auth\User;
use Shared\Services\AuthService;
use PHPUnit\Framework\TestCase;
use Quantum\Di\Di;
Expand Down Expand Up @@ -51,7 +54,7 @@ public function testUserGet()
{
$user = $this->authService->get('email', '[email protected]');

$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());
Expand All @@ -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();

Expand All @@ -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('[email protected]', $user->getFieldValue('email'));
}
Expand Down
73 changes: 29 additions & 44 deletions tests/unit/PostServiceTest.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

namespace Quantum\Tests;

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 Shared\Models\Post;
use Quantum\Di\Di;
use Quantum\App;

Expand Down Expand Up @@ -64,11 +66,6 @@ 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 @@ -77,8 +74,6 @@ 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 @@ -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);
}
Expand All @@ -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()
Expand All @@ -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.',
Expand All @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit 984a7bd

Please sign in to comment.