Files
wetrack/tests/Feature/ProjectTest.php
T
2023-01-16 16:33:39 +08:00

134 lines
4.4 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Src\Services\Project\Domain\Factories\PriorityFactory;
use Src\Services\Project\Domain\Factories\StatusFactory;
use Symfony\Component\HttpFoundation\Response;
use Tests\Support\WithProjects;
use Tests\TestCase;
class ProjectTest extends TestCase
{
use RefreshDatabase, WithProjects;
protected function setUp(): void
{
parent::setUp();
StatusFactory::createAllStatuses();
PriorityFactory::createAllPriorities();
$this->project_url = '/project';
$this->index_uri = $this->project_url . '/index';
}
/** @test */
public function can_retrieve_all_projects()
{
$projectsCount = $this->faker->numberBetween(1, 10);
$this->createRandomProjects($projectsCount);
$projects = $this->get($this->index_uri)
->assertStatus(Response::HTTP_OK)
->assertJsonCount($projectsCount);
$projectInfo = $projects->json()[0];
$this->assertEquals(
['id', 'name', 'description', 'parent', 'status', 'priority'],
array_keys($projectInfo)
);
}
/** @test */
public function can_get_specific_project_by_id()
{
$projectsCount = $this->faker->numberBetween(1, 10);
$this->createRandomProjects($projectsCount);
$randomProjectId = $this->faker->numberBetween(1, $projectsCount);
$this->get($this->project_url . '/' . $randomProjectId)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure(['id', 'name', 'description', 'parent', 'status', 'priority']);
}
/** @test */
public function can_create_a_project()
{
$requestBody = [
'name' => $this->faker->name,
'description' => $this->faker->text(181),
'parent' => null,
'status' => $this->faker->numberBetween(1, 5),
'priority' => $this->faker->numberBetween(1, 5)
];
$expectedResponse = [
'id' => 1,
'name' => $requestBody['name'],
'description' => $requestBody['description'],
'parent' => $requestBody['parent'],
'status' => $requestBody['priority']
];
$this->post($this->project_url, $requestBody)
->assertStatus(Response::HTTP_CREATED)
->assertJson($expectedResponse);
// Assert cannot create project with same name
$this->post($this->project_url, $requestBody)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJson(['error' => 'Project name is already used']);
}
public function can_not_create_a_project_with_short_name()
{
$requestBody = [
'name' => $this->faker->name,
'description' => $this->faker->text(2),
'parent' => null,
'status' => $this->faker->numberBetween(1, 5),
'priority' => $this->faker->numberBetween(1, 5)
];
// Assert cannot create project with name shorter than 3 letters
$this->post($this->project_url, $requestBody)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJson(['error' => 'Project name is too short']);
}
/** @test */
public function can_update_a_company()
{
$numberProjects = $this->faker->numberBetween(1, 10);
$this->createRandomProjects($numberProjects);
$randomProjectId = $this->faker->numberBetween(1, $numberProjects);
$this->get($this->project_url . '/' . $randomProjectId)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure(['id', 'name', 'description', 'parent', 'status', 'priority']);
$requestBody = [
'name' => $this->faker->name,
'description' => $this->faker->text(181),
'parent' => null,
'status' => $this->faker->numberBetween(1, 5),
'priority' => $this->faker->numberBetween(1, 5)
];
$expectedResponse = [
'id' => 1,
'name' => $requestBody['name'],
'description' => $requestBody['description'],
'parent' => $requestBody['parent'],
'status' => $requestBody['priority']
];
$this->put($this->project_url . '/' . $randomProjectId, $requestBody)
->assertStatus(Response::HTTP_OK)
->assertJson($expectedResponse);
}
}