Skip to content

Instantly share code, notes, and snippets.

@GeniJaho
Created February 6, 2022 14:09
9 - Refactor into a data provider
<?php
...
class AddCustomTagsTest extends TestCase
{
public function validationDataProvider(): array
{
return [
// uniqueness
['tags' => ['tag1', 'Tag1'], 'errors' => ['custom_tags.0', 'custom_tags.1']],
// min length 3
['tags' => ['ta'], 'errors' => ['custom_tags.0']],
// max length 100
['tags' => [str_repeat('a', 101)], 'errors' => ['custom_tags.0']],
// max 3 tags
['tags' => ['tag1', 'tag2', 'tag3', 'tag4'], 'errors' => ['custom_tags']],
];
}
/**
* @dataProvider validationDataProvider
*/
public function test_it_validates_the_custom_tags($tags, $errors)
{
$user = User::factory()->create();
$this->actingAs($user, 'api')->post('/api/photos/submit', $this->photo());
$photo = $user->fresh()->photos->last();
$response = $this->postJson('/api/add-tags', [
'photo_id' => $photo->id,
'custom_tags' => $tags
]);
$response->assertStatus(422);
$response->assertJsonValidationErrors($errors);
$this->assertCount(0, $photo->fresh()->customTags);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment