Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

Commit

Permalink
organizando assercoes na edicao
Browse files Browse the repository at this point in the history
  • Loading branch information
josemotta committed Aug 26, 2021
1 parent f6148ba commit 3cfff3a
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 82 deletions.
115 changes: 70 additions & 45 deletions tests/Feature/Http/Controllers/Api/CategoryControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,58 +82,83 @@ public function testStore()

public function testUpdate()
{
$category = factory(Category::class)->create([
$this->category = factory(Category::class)->create([
'is_active' => false,
'description' => 'description'
]);
$response = $this->json(
'PUT',
route('categories.update', ['category' => $category->id]),
[
'name' => 'test',
'description' => 'test',
'is_active' => true
]);

$id = $response->json('id');
$category = Category::find($id);
$data = [
'name' => 'test',
'description' => 'test',
'is_active' => true
];
$response = $this->assertUpdate($data, $data + ['deleted_at' => null]);
$response->assertJsonStructure([
'created_at', 'updated_at'
]);

$response
->assertStatus(200)
->assertJson($category->toArray())
->assertJsonFragment([
'description' => 'test',
'is_active' => true
]);

// empty description is converted to null
$response = $this->json(
'PUT',
route('categories.update', ['category' => $category->id]),
[
'name' => 'test',
'description' => ''
]);
$data = [
'name' => 'test',
'description' => '',
'is_active' => true
];
$this->assertUpdate($data, array_merge($data, ['description' => null]));

$data['description'] = 'test';
$this->assertUpdate($data, array_merge($data, ['description' => 'test']));

$data['description'] = null;
$this->assertUpdate($data, array_merge($data, ['description' => null]));

$response->assertJsonFragment([
'description' => null
]);

$category->description = 'test';
$category->save();

// also testing null
$response = $this->json(
'PUT',
route('categories.update', ['category' => $category->id]),
[
'name' => 'test',
'description' => null
]);

// $response = $this->json(
// 'PUT',
// route('categories.update', ['category' => $category->id]),
// [
// 'name' => 'test',
// 'description' => 'test',
// 'is_active' => true
// ]);

// $id = $response->json('id');
// $category = Category::find($id);

// $response
// ->assertStatus(200)
// ->assertJson($category->toArray())
// ->assertJsonFragment([
// 'description' => 'test',
// 'is_active' => true
// ]);

$response->assertJsonFragment([
'description' => null
]);
// // empty description is converted to null
// $response = $this->json(
// 'PUT',
// route('categories.update', ['category' => $category->id]),
// [
// 'name' => 'test',
// 'description' => ''
// ]);

// $response->assertJsonFragment([
// 'description' => null
// ]);

// $category->description = 'test';
// $category->save();

// // also testing null
// $response = $this->json(
// 'PUT',
// route('categories.update', ['category' => $category->id]),
// [
// 'name' => 'test',
// 'description' => null
// ]);

// $response->assertJsonFragment([
// 'description' => null
// ]);
}

public function testDestroy()
Expand Down
59 changes: 23 additions & 36 deletions tests/Feature/Http/Controllers/Api/GenreControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,50 +67,37 @@ public function testStore()
$data = [
'name' => 'test'
];
$this->assertStore($data, $data + ['is_active' => true]);

// $response = $this->json('POST', route('genres.store'), [
// 'name' => 'test'
// ]);

// $id = $response->json('id');
// $genre = Genre::find($id);
$response = $this->assertStore($data, $data + ['is_active' => true]);
$response->assertJsonStructure([
'created_at', 'updated_at'
]);

// $response
// ->assertStatus(201)
// ->assertJson($genre->toArray());

// $this->assertTrue($response->json('is_active'));

// $response = $this->json('POST', route('genres.store'), [
// 'name' => 'test',
// 'is_active' => false
// ]);
// $response
// ->assertJsonFragment([
// 'is_active' => false
// ]);
$data = [
'name' => 'test',
'is_active' => false
];
$this->assertStore($data, $data + ['is_active' => false]);
}

public function testUpdate()
{
$genre = factory(Genre::class)->create([
$this->genre = factory(Genre::class)->create([
'is_active' => false
]);
$response = $this->json(
'PUT',
route('genres.update', ['genre' => $genre->id]),
[
'name' => 'test',
'is_active' => true
]);

$id = $response->json('id');
$genre = Genre::find($id);
$data = [
'name' => 'test',
'is_active' => true
];
$response = $this->assertUpdate($data, $data + ['deleted_at' => null]);
$response->assertJsonStructure([
'created_at', 'updated_at'
]);

$response
->assertStatus(200)
->assertJson($genre->toArray());
$data = [
'name' => 'test',
'is_active' => false
];
$this->assertUpdate($data, $data);
}

public function testDestroy()
Expand Down
24 changes: 23 additions & 1 deletion tests/Traits/TestSaves.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,33 @@ protected function assertStore(array $sendData, array $testDatabase, array $test
if ($response->status() !== 201) {
throw new \Exception("Response status must be 201, given {$response->status()}:\n{$response->content()}");
}
$this->assertInDatabase($response, $testDatabase);
$this->assertJsonResponseContent($response, $testDatabase, $testJsonData);
return $response;
}

protected function assertUpdate(array $sendData, array $testDatabase, array $testJsonData = null): TestResponse
{
/** @var TestResponse $response */
$response = $this->json('PUT', $this->routeUpdate(), $sendData);
if ($response->status() !== 200) {
throw new \Exception("Response status must be 200, given {$response->status()}:\n{$response->content()}");
}
$this->assertInDatabase($response, $testDatabase);
$this->assertJsonResponseContent($response, $testDatabase, $testJsonData);
return $response;
}

protected function assertInDatabase(TestResponse $response, array $testDatabase)
{
$model = $this->model();
$table = (new $model)->getTable();
$this->assertDatabaseHas($table, $testDatabase + ['id' => $response->json('id')]);
}

protected function assertJsonResponseContent(TestResponse $response, array $testDatabase, array $testJsonData = null)
{
$testResponse = $testJsonData ?? $testDatabase;
$response->assertJsonFragment($testResponse + ['id' => $response->json('id')]);
return $response;
}
}

0 comments on commit 3cfff3a

Please sign in to comment.