Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions shared/Services/PostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ public function getMyPosts(string $userUuid): ?ModelCollection
/**
* Add post
* @param array $data
* @return array
* @return Post
* @throws ModelException
*/
public function addPost(array $data): array
public function addPost(array $data): Post
{
$data['uuid'] = $data['uuid'] ?? uuid_ordered();
$data['created_at'] = date('Y-m-d H:i:s');
Expand All @@ -164,22 +164,25 @@ public function addPost(array $data): array
$post->fillObjectProps($data);
$post->save();

return $data;
return $this->getPost($post->uuid);
}

/**
* Update post
* @param string $uuid
* @param array $data
* @return Post
* @throws ModelException
*/
public function updatePost(string $uuid, array $data)
public function updatePost(string $uuid, array $data): Post
{
$data['updated_at'] = date('Y-m-d H:i:s');

$post = $this->model->findOneBy('uuid', $uuid);
$post->fillObjectProps($data);
$post->save();

return $this->getPost($post->uuid);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/AppTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function request(

protected function signInAndGetTokens(): array
{
$response = $this->request('post', '/api/en/signin', [
$response = $this->request('post', '/api/signin', [
'email' => $this->defaultEmail,
'password' => $this->defaultPassword
]);
Expand Down
81 changes: 81 additions & 0 deletions tests/Feature/modules/Api/CommentControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Quantum\Tests\Feature\modules\Api;


use Quantum\Model\Factories\ModelFactory;
use Quantum\Tests\Feature\AppTestCase;
use Shared\Models\Comment;
use Quantum\Http\Response;
use Quantum\Http\Request;

class CommentControllerTest extends AppTestCase
{

private $tokens = [];

private $post = null;

public function setUp(): void
{
parent::setUp();

$this->tokens = $this->signInAndGetTokens();

$response = $this->request('get', '/api/posts');

$postData = $response->get('data');

$this->post = $postData[0];

Request::flush();
Response::flush();
}

public function tearDown(): void
{
parent::tearDown();
}

public function testModuleApiCommentCreateEndpoint()
{
$method = 'POST';
$endpoint = '/api/comments/create/';
$body = ['content' => 'My first comment'];
$headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']];

$response = $this->request($method, $endpoint . $this->post['uuid'], $body, $headers);

$this->assertEquals('success', $response->get('status'));

$this->assertEquals('Created successfully', $response->get('message'));

$comment = $response->get('data');

$this->assertIsArray($comment);

$this->assertArrayHasKey('uuid', $comment);
$this->assertArrayHasKey('user_uuid', $comment);
$this->assertArrayHasKey('post_uuid', $comment);
$this->assertArrayHasKey('content', $comment);
$this->assertArrayHasKey('created_at', $comment);
}

public function testModuleApiCommentDeleteEndpoint()
{
$method = 'DELETE';
$endpoint = '/api/comments/delete/';
$body = [];
$headers = ['Authorization' => 'Bearer ' . $this->tokens['access_token']];

$comment = ModelFactory::get(Comment::class)->first();

$response = $this->request($method, $endpoint . $comment->uuid, $body, $headers);

$this->assertIsObject($response);

$this->assertEquals('success', $response->get('status'));

$this->assertEquals('Deleted successfully', $response->get('message'));
}
}
152 changes: 40 additions & 112 deletions tests/Feature/modules/Api/PostControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

class PostControllerTest extends AppTestCase
{
/**
* @var string
*/
protected $email = 'test@test.test';

/**
* @var string
*/
protected $password = 'password';

public function setUp(): void
{
Expand All @@ -33,28 +24,40 @@ public function tearDown(): void

public function testModuleApiPostsEndpoint()
{
$response = $this->request('get', '/api/en/posts');
$method = 'GET';
$endpoint = '/api/posts';

$response = $this->request($method, $endpoint);

$this->assertIsObject($response);

$this->assertEquals('success', $response->get('status'));

$this->assertArrayHasKey('data', $response->all());

$postData = $response->get('data');

$this->assertIsArray($postData);

$this->assertCount(8, $postData);

$this->assertArrayHasKey('uuid', $postData[0]);
$this->assertArrayHasKey('title', $postData[0]);
$this->assertArrayHasKey('content', $postData[0]);
$this->assertArrayHasKey('image', $postData[0]);
$this->assertArrayHasKey('date', $postData[0]);
$this->assertArrayHasKey('author', $postData[0]);
$firstPost = $postData[0];

$this->assertIsArray($firstPost);

$this->assertArrayHasKey('uuid', $firstPost);
$this->assertArrayHasKey('title', $firstPost);
$this->assertArrayHasKey('content', $firstPost);
$this->assertArrayHasKey('image', $firstPost);
$this->assertArrayHasKey('date', $firstPost);
$this->assertArrayHasKey('author', $firstPost);

$this->assertArrayHasKey('pagination', $response->all());

$pagination = $response->get('pagination');

$this->assertIsArray($pagination);

$this->assertArrayHasKey('total_records', $pagination);
$this->assertArrayHasKey('current_page', $pagination);
$this->assertArrayHasKey('next_page', $pagination);
Expand All @@ -68,114 +71,39 @@ public function testModuleApiPostsEndpoint()

public function testModuleApiSinglePostEndpoint()
{
$post = ModelFactory::get(Post::class)->first();

$response = $this->request('get', '/api/en/post/' . $post->uuid);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertArrayHasKey('data', $response->all());

$rawData = $response->get('data');

$this->assertArrayHasKey('uuid', $rawData);
$this->assertArrayHasKey('title', $rawData);
$this->assertArrayHasKey('content', $rawData);
$this->assertArrayHasKey('image', $rawData);
$this->assertArrayHasKey('date', $rawData);
$this->assertArrayHasKey('author', $rawData);
}

public function testModuleApiMyPostsEndpoint()
{
$tokens = $this->signInAndGetTokens();

$response = $this->request('get', '/api/en/my-posts', [], [
'Authorization' => 'Bearer ' . $tokens['access_token'],
'refresh_token' => $tokens['refresh_token']
]);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertArrayHasKey('data', $response->all());

$postData = $response->get('data');

$this->assertCount(10, $postData);
$method = 'GET';
$endpoint = '/api/post/';

$this->assertArrayHasKey('uuid', $postData[0]);
$this->assertArrayHasKey('title', $postData[0]);
$this->assertArrayHasKey('content', $postData[0]);
$this->assertArrayHasKey('image', $postData[0]);
$this->assertArrayHasKey('date', $postData[0]);
$this->assertArrayHasKey('author', $postData[0]);
}

public function testModuleApiPostCreateEndpoint()
{
$tokens = $this->signInAndGetTokens();

$response = $this->request('post', '/api/en/my-posts/create',
[
'title' => 'test title',
'content' => 'test content',
'image' => '',
'updated_at' => date('Y-m-d H:i:s'),
],
['Authorization' => 'Bearer ' . $tokens['access_token']]
);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Created successfully', $response->get('message'));
}

public function testModuleApiAmendPostEndpoint()
{
$post = ModelFactory::get(Post::class)->first();

$tokens = $this->signInAndGetTokens();

$response = $this->request('put', '/api/en/my-posts/amend/' . $post->uuid,
[
'title' => 'test title123',
'content' => 'test content123',
],
['Authorization' => 'Bearer ' . $tokens['access_token']]
);
$response = $this->request($method, $endpoint . $post->uuid);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Updated successfully', $response->get('message'));
}

public function testDeletePostApi()
{
$post = ModelFactory::get(Post::class)->first();
$this->assertEquals('success', $response->get('status'));

$tokens = $this->signInAndGetTokens();
$this->assertArrayHasKey('data', $response->all());

$response = $this->request('delete', '/api/en/my-posts/delete/' . $post->uuid, [], [
'Authorization' => 'Bearer ' . $tokens['access_token']
]);
$post = $response->get('data');

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Deleted successfully', $response->get('message'));
}
$this->assertIsArray($post);

public function testDeleteImagePostApi()
{
$post = ModelFactory::get(Post::class)->first();
$this->assertArrayHasKey('uuid', $post);
$this->assertArrayHasKey('title', $post);
$this->assertArrayHasKey('content', $post);
$this->assertArrayHasKey('image', $post);
$this->assertArrayHasKey('date', $post);
$this->assertArrayHasKey('author', $post);
$this->assertArrayHasKey('comments', $post);

$tokens = $this->signInAndGetTokens();
$firstComment = $post['comments'][0];

$response = $this->request('delete', '/api/en/my-posts/delete-image/' . $post->uuid, [], [
'Authorization' => 'Bearer ' . $tokens['access_token']
]);
$this->assertIsArray($firstComment);

$this->assertIsObject($response);
$this->assertEquals('success', $response->get('status'));
$this->assertEquals('Deleted successfully', $response->get('message'));
$this->assertArrayHasKey('uuid', $firstComment);
$this->assertArrayHasKey('author', $firstComment);
$this->assertArrayHasKey('author', $firstComment);
$this->assertArrayHasKey('content', $firstComment);
$this->assertArrayHasKey('date', $firstComment);
}
}
Loading