Skip to content

Commit

Permalink
improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
fokosun committed Aug 23, 2023
1 parent 43ab988 commit 437fe69
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 120 deletions.
30 changes: 0 additions & 30 deletions app/Http/Controllers/CategoryController.php

This file was deleted.

67 changes: 0 additions & 67 deletions app/Http/Controllers/StatsController.php

This file was deleted.

36 changes: 13 additions & 23 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,33 +132,23 @@ public function addFeedback(Request $request)
{
/** @phpstan-ignore-next-line */
if ($user = JWTAuth::parseToken()->user()) {
$hasRespondedAlready = UserFeedback::where(['user_id' => $user->getKey(), 'type' => 'feedback']);

try {
$hasRespondedAlready = UserFeedback::where(['user_id' => $user->getKey(), 'type' => 'feedback']);

if (collect($hasRespondedAlready->pluck('response')->toArray())->isEmpty()) {
$userFeedback = new UserFeedback([
'user_id' => $user->getKey(),
'type' => 'feedback',
'response' => $request->get('choice', 'still-thinking')
]);

return response()->json(['success' => $userFeedback->save()]);
}

return response()->json([
'success' => $hasRespondedAlready->first()->update([
'response' => $request->get('choice', 'still-thinking')
])
if (collect($hasRespondedAlready->pluck('response')->toArray())->isEmpty()) {
$userFeedback = new UserFeedback([
'user_id' => $user->getKey(),
'type' => 'feedback',
'response' => $request->get('choice', 'still-thinking')
]);
} catch (ApiException $exception){
Log::debug(
'error creating user feedback',
['exception' => $exception]
);

return response()->json(['error', 'There was an error processing this request. Please try again later.'], $exception->getCode());
return response()->json(['success' => $userFeedback->save()]);
}

return response()->json([
'success' => $hasRespondedAlready->first()->update([
'response' => $request->get('choice', 'still-thinking')
])
]);
}

return $this->unauthorizedResponse();
Expand Down
3 changes: 3 additions & 0 deletions app/Models/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Definition extends Model
{
use HasFactory;

/**
* @var array<string>
*/
Expand Down
31 changes: 31 additions & 0 deletions tests/Feature/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,4 +379,35 @@ public function test_who_to_folow()
]
]);
}

/**
* @test
*/
public function it_allows_authorized_users_to_use_the_add_feedback_feature(): void
{
$choices = ['still-thinking', 'probably', 'very-likely'];
$choice = $choices[array_rand($choices)];

$user = User::factory()->make([
'email' => '[email protected]',
'password' => (new BcryptHasher)->make('pass123'),
]);
$user->save();

$bearerToken = Auth::attempt([
'email' => '[email protected]',
'password' => 'pass123'
]);

$this->json(
'POST',
'/api/v1/feedback',
[
'choice' => $choice
],
[
'Authorization' => 'Bearer ' . $bearerToken
]
)->assertStatus(200);
}
}
45 changes: 45 additions & 0 deletions tests/Unit/StaticContentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Unit;

use App\Http\Controllers\DefinitionsController;
use App\Http\Controllers\StaticContentController;
use App\Models\Definition;

class StaticContentsTest extends \TestCase
{
/**
* @test
*/
public function it_returns_definitions_data()
{
$this->seed('Database\\Seeders\\DefinitionsSeeder');
$controller = new DefinitionsController();

$definitions = $controller->index();
$this->assertNotEmpty($definitions->all());

$definitions->map(function ($defintion) {
$this->assertInstanceOf(Definition::class, $defintion);
});
}

/**
* @test
*/
public function it_returns_static_contents_data()
{
$this->seed('Database\Seeders\StaticContentsSeeder');
$controller = new StaticContentController();

$staticContents = $controller->get();
$decoded = json_decode($staticContents->getContent(), true);

$this->assertArrayHasKey('cookiePolicy', $decoded['response']);
$this->assertArrayHasKey('usagePolicy', $decoded['response']);
$this->assertArrayHasKey('dataRetentionPolicy', $decoded['response']);
$this->assertArrayHasKey('termsAndConditions', $decoded['response']);
}
}

0 comments on commit 437fe69

Please sign in to comment.