-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8a356b9
commit c522047
Showing
4 changed files
with
94 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Rules; | ||
|
||
use App\Models\Question; | ||
use Closure; | ||
use Illuminate\Contracts\Validation\ValidationRule; | ||
|
||
class SameQuestionRule implements ValidationRule | ||
{ | ||
/** | ||
* Run the validation rule. | ||
* | ||
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail | ||
*/ | ||
public function validate(string $attribute, mixed $value, Closure $fail): void | ||
{ | ||
if ($this->validationRule($value)) { | ||
$fail('Pergunta já existe!'); | ||
} | ||
} | ||
|
||
private function validationRule(string $value): bool | ||
{ | ||
return Question::whereQuestion($value)->exists(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,87 @@ | ||
<?php | ||
|
||
use App\Models\User; | ||
use App\Models\{Question, User}; | ||
|
||
use function Pest\Laravel\{actingAs, assertDatabaseCount, assertDatabaseHas, post}; | ||
use function Pest\Laravel\{actingAs, assertDatabaseCount, assertDatabaseHas, post, postJson}; | ||
|
||
it('should be able to create a new question bigger than 255 characters', function () { | ||
// Arrange | ||
// Arrange :: preparar | ||
$user = User::factory()->create(); | ||
actingAs($user); | ||
|
||
//Act | ||
// Act :: agir | ||
$request = post(route('question.store'), [ | ||
'question' => str_repeat('a', 260) . '?', | ||
'question' => str_repeat('*', 260) . '?', | ||
]); | ||
|
||
// Assert | ||
// Assert :: verificar | ||
$request->assertRedirect(); | ||
assertDatabaseCount('questions', 1); | ||
assertDatabaseHas('questions', ['question' => str_repeat('*', 260) . '?']); | ||
}); | ||
|
||
it('should create as a draft all the time', function () { | ||
// Arrange :: preparar | ||
$user = User::factory()->create(); | ||
actingAs($user); | ||
|
||
// Act :: agir | ||
post(route('question.store'), [ | ||
'question' => str_repeat('*', 260) . '?', | ||
]); | ||
|
||
// Assert :: verificar | ||
assertDatabaseHas('questions', [ | ||
'question' => str_repeat('a', 260) . '?', | ||
'question' => str_repeat('*', 260) . '?', | ||
'draft' => true, | ||
]); | ||
}); | ||
|
||
it('should check if ends with a question mark ?', function () { | ||
// Arrange | ||
it('should check if ends with question mark ?', function () { | ||
// Arrange :: preparar | ||
$user = User::factory()->create(); | ||
actingAs($user); | ||
|
||
//Act | ||
// Act :: agir | ||
$request = post(route('question.store'), [ | ||
'question' => str_repeat('?', 9) . ".", | ||
'question' => str_repeat('*', 10), | ||
]); | ||
|
||
// Assert | ||
// Assert :: verificar | ||
$request->assertSessionHasErrors([ | ||
'question' => 'The question must end with a question mark?.', | ||
'question' => 'Are you sure that is a question? It is missing the question mark in the end.', | ||
]); | ||
assertDatabaseCount('questions', 0); | ||
}); | ||
|
||
it('should have at least 10 characters', function () { | ||
// Arrange | ||
// Arrange :: preparar | ||
$user = User::factory()->create(); | ||
actingAs($user); | ||
|
||
//Act | ||
// Act :: agir | ||
$request = post(route('question.store'), [ | ||
'question' => 'a', | ||
'question' => str_repeat('*', 8) . '?', | ||
]); | ||
|
||
// Assert | ||
$request->assertSessionHasErrors(['question' => __('validation.min.string', ['attribute' => 'question', 'min' => 10])]); | ||
// Assert :: verificar | ||
$request->assertSessionHasErrors(['question' => __('validation.min.string', ['min' => 10, 'attribute' => 'question'])]); | ||
assertDatabaseCount('questions', 0); | ||
}); | ||
|
||
test('only authenticated user can create a new question', function () { | ||
test('only authenticated users can create a new question', function () { | ||
post(route('question.store'), [ | ||
'question' => str_repeat('a', 260) . '?', | ||
'question' => str_repeat('*', 8) . '?', | ||
])->assertRedirect(route('login')); | ||
}); | ||
|
||
test('question should be unique', function () { | ||
$user = User::factory()->create(); | ||
actingAs($user); | ||
|
||
Question::factory()->create(['question' => 'Alguma Pergunta?']); | ||
|
||
post(route('question.store'), [ | ||
'question' => 'Alguma Pergunta?', | ||
])->assertSessionHasErrors(['question' => 'Pergunta já existe!']); | ||
}); |