You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
232 lines
6.7 KiB
232 lines
6.7 KiB
<?php
|
|
|
|
namespace Tests\Feature\Api;
|
|
|
|
use fullMstr;
|
|
use Tests\BrowserKitTest as TestCase;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
|
|
class ManageMasterTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/** @test */
|
|
public function user_can_see_master_list_in_master_index_page()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create();
|
|
|
|
$this->getJson(route('api.masters.index'), [
|
|
'Authorization' => 'Bearer '.$user->api_token
|
|
]);
|
|
|
|
$this->seeJson(['name' => $singleMstr->name]);
|
|
}
|
|
|
|
/** @test */
|
|
public function user_can_create_a_master()
|
|
{
|
|
$user = $this->createUser();
|
|
|
|
$this->postJson(route('api.masters.store'), [
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
], [
|
|
'Authorization' => 'Bearer '.$user->api_token
|
|
]);
|
|
|
|
$this->seeInDatabase('masters', [
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
]);
|
|
|
|
$this->seeStatusCode(201);
|
|
$this->seeJson([
|
|
'message' => __('master.created'),
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
]);
|
|
}
|
|
|
|
private function getCreateFields(array $overrides = [])
|
|
{
|
|
return array_merge([
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
], $overrides);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_name_is_required()
|
|
{
|
|
$user = $this->createUser();
|
|
|
|
// name empty
|
|
$requestBody = $this->getCreateFields(['name' => '']);
|
|
$this->postJson(
|
|
route('api.masters.store'),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['name']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_name_is_not_more_than_60_characters()
|
|
{
|
|
$user = $this->createUser();
|
|
|
|
// name 70 characters
|
|
$requestBody = $this->getCreateFields(['name' => str_repeat('Test Title', 7)]);
|
|
$this->postJson(
|
|
route('api.masters.store'),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['name']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_description_is_not_more_than_255_characters()
|
|
{
|
|
$user = $this->createUser();
|
|
|
|
// description 256 characters
|
|
$requestBody = $this->getCreateFields(['description' => str_repeat('Long description', 16)]);
|
|
$this->postJson(
|
|
route('api.masters.store'),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['description']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function user_can_get_a_master_detail()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create(['name' => 'Testing 123']);
|
|
|
|
$this->getJson(route('api.masters.show', $singleMstr), [
|
|
'Authorization' => 'Bearer '.$user->api_token
|
|
]);
|
|
|
|
$this->seeJson(['name' => 'Testing 123']);
|
|
}
|
|
|
|
/** @test */
|
|
public function user_can_update_a_master()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create(['name' => 'Testing 123']);
|
|
|
|
$this->patchJson(route('api.masters.update', $singleMstr), [
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
], [
|
|
'Authorization' => 'Bearer '.$user->api_token
|
|
]);
|
|
|
|
$this->seeInDatabase('masters', [
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
]);
|
|
|
|
$this->seeStatusCode(200);
|
|
$this->seeJson([
|
|
'message' => __('master.updated'),
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
]);
|
|
}
|
|
|
|
private function getEditFields(array $overrides = [])
|
|
{
|
|
return array_merge([
|
|
'name' => 'Master 1 name',
|
|
'description' => 'Master 1 description',
|
|
], $overrides);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_name_update_is_required()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create();
|
|
|
|
// name empty
|
|
$requestBody = $this->getEditFields(['name' => '']);
|
|
$this->patchJson(
|
|
route('api.masters.update', $singleMstr),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['name']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_name_update_is_not_more_than_60_characters()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create();
|
|
|
|
// name 70 characters
|
|
$requestBody = $this->getEditFields(['name' => str_repeat('Test Title', 7)]);
|
|
$this->patchJson(
|
|
route('api.masters.update', $singleMstr),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['name']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function validate_master_description_update_is_not_more_than_255_characters()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create(['name' => 'Testing 123']);
|
|
|
|
// description 256 characters
|
|
$requestBody = $this->getEditFields(['description' => str_repeat('Long description', 16)]);
|
|
$this->patchJson(
|
|
route('api.masters.update', $singleMstr),
|
|
$requestBody,
|
|
['Authorization' => 'Bearer '.$user->api_token]
|
|
);
|
|
|
|
$this->seeStatusCode(422);
|
|
$this->seeJsonStructure(['errors' => ['description']]);
|
|
}
|
|
|
|
/** @test */
|
|
public function user_can_delete_a_master()
|
|
{
|
|
$user = $this->createUser();
|
|
$singleMstr = Master::factory()->create();
|
|
|
|
$this->deleteJson(route('api.masters.destroy', $singleMstr), [
|
|
'master_id' => $singleMstr->id,
|
|
], [
|
|
'Authorization' => 'Bearer '.$user->api_token
|
|
]);
|
|
|
|
$this->dontSeeInDatabase('masters', [
|
|
'id' => $singleMstr->id,
|
|
]);
|
|
|
|
$this->seeStatusCode(200);
|
|
$this->seeJson([
|
|
'message' => __('master.deleted'),
|
|
]);
|
|
}
|
|
}
|