Files
ddd-example/tests/Feature/CompanyTest.php
T
2022-10-04 20:58:33 +08:00

259 lines
10 KiB
PHP

<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;
use Tests\Support\WithLogin;
use Tests\Support\WithCompanies;
class CompanyTest extends TestCase
{
use RefreshDatabase, WithLogin, WithCompanies;
protected function setUp(): void
{
parent::setUp();
$this->company_uri = '/company';
$this->index_uri = $this->company_uri . '/index';
$this->adminToken = $this->newLoggedAdmin()['token'];
$this->userData = $this->newLoggedUser();
$this->userToken = $this->userData['token'];
}
/** @test */
public function admin_can_retrieve_all_companies()
{
$companiesCount = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($companiesCount);
$companies = $this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->get($this->index_uri)
->assertStatus(Response::HTTP_OK)
->assertJsonCount($companiesCount);
$companyInfo = $companies->json()[0];
$this->assertEquals(
['id', 'fiscal_name', 'social_name', 'vat', 'is_active'],
array_keys($companyInfo)
);
}
/** @test */
public function user_cannot_retrieve_all_companies()
{
$companiesCount = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($companiesCount);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
->get($this->index_uri)
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertSee(['error' => 'The user is not authorized to access this resource']);
}
/** @test */
public function admin_can_get_specific_company_by_id()
{
$companiesCount = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($companiesCount);
$randomCompanyId = $this->faker->numberBetween(1, $companiesCount);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->get($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure(['id', 'fiscal_name', 'social_name', 'vat', 'is_active']);
}
/** @test */
public function user_cannot_get_specific_company_by_id_except_if_belongs_to()
{
$companiesCount = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($companiesCount);
$randomCompanyId = $this->faker->numberBetween(2, $companiesCount + 1);
// User cannot retrieve company where it is not belonged to
$this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
->get($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertSee(['error' => 'The user is not authorized to access this resource']);
// User can retrieve company where it belongs
//todo implement user relationship to company
// $this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
// ->get($this->company_uri . '/' . $companyId)
// ->assertStatus(Response::HTTP_OK)
// ->assertJsonStructure(['id', 'fiscal_name', 'social_name', 'vat', 'is_active']);
}
/** @test */
public function admin_can_create_a_company()
{
$requestBody = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => $this->faker->bothify('?#########'),
'is_active' => $this->faker->boolean,
];
$expectedResponse = [
'id' => 1,
'fiscal_name' => $requestBody['fiscal_name'],
'social_name' => $requestBody['social_name'],
'vat' => $requestBody['vat'],
'is_active' => $requestBody['is_active']
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->post($this->company_uri, $requestBody)
->assertStatus(Response::HTTP_CREATED)
->assertJson($expectedResponse);
// Assert cannot create company with same vat
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->post($this->company_uri, $requestBody)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJson(['error' => 'Vat is already used']);
}
/** @test */
public function user_cannot_create_a_company()
{
$requestBody = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => $this->faker->bothify('?#########'),
'is_active' => $this->faker->boolean
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
->post($this->company_uri, $requestBody)
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertSee(['error' => 'The user is not authorized to access this resource']);
}
/** @test */
public function cannot_create_company_with_invalid_vat()
{
$requestBodyInvalidVat = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => 'invalidvat',
'is_active' => $this->faker->boolean,
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->post($this->company_uri, $requestBodyInvalidVat)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJson(['error' => 'Vat must be valid']);
}
/** @test */
public function admin_can_update_a_company()
{
$numberCompanies = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($numberCompanies);
$randomCompanyId = $this->faker->numberBetween(1, $numberCompanies);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->get($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure(['id', 'fiscal_name', 'social_name', 'vat', 'is_active']);
$requestBody = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => $this->faker->bothify('?#########'),
'is_active' => $this->faker->boolean,
];
$expectedResponse = [
'id' => $randomCompanyId,
'fiscal_name' => $requestBody['fiscal_name'],
'social_name' => $requestBody['social_name'],
'vat' => $requestBody['vat'],
'is_active' => $requestBody['is_active']
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->put($this->company_uri . '/' . $randomCompanyId, $requestBody)
->assertStatus(Response::HTTP_OK)
->assertJson($expectedResponse);
$requestBodyInvalidVat = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => 'invalidvat',
'is_active' => $this->faker->boolean,
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->put($this->company_uri . '/' . $randomCompanyId, $requestBodyInvalidVat)
->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY)
->assertJson(['error' => 'Vat must be valid']);
}
/** @test */
public function user_cannot_update_a_company()
{
$numberCompanies = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($numberCompanies);
$randomCompanyId = $this->faker->numberBetween(1, $numberCompanies);
$company = $this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->get($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_OK)
->assertJsonStructure(['id', 'fiscal_name', 'social_name', 'vat', 'is_active']);
$requestBody = [
'fiscal_name' => $this->faker->name,
'social_name' => $this->faker->company,
'vat' => $this->faker->bothify('?#########'),
'is_active' => $this->faker->boolean
];
$this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
->put($this->company_uri . '/' . $randomCompanyId, $requestBody)
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertSee(['error' => 'The user is not authorized to access this resource']);
}
/** @test */
public function admin_can_delete_a_company()
{
$numberCompanies = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($numberCompanies);
$randomCompanyId = $this->faker->numberBetween(1, $numberCompanies);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->delete($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_NO_CONTENT);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->get($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_NOT_FOUND);
}
/** @test */
public function user_cannot_delete_a_company()
{
$numberCompanies = $this->faker->numberBetween(1, 10);
$this->createRandomCompanies($numberCompanies);
$randomCompanyId = $this->faker->numberBetween(1, $numberCompanies);
$this->withHeaders(['Authorization' => 'Bearer ' . $this->userToken])
->delete($this->company_uri . '/' . $randomCompanyId)
->assertStatus(Response::HTTP_UNAUTHORIZED)
->assertSee(['error' => 'The user is not authorized to access this resource']);
}
/** @test */
public function cannot_delete_company_if_does_not_exists()
{
$this->withHeaders(['Authorization' => 'Bearer ' . $this->adminToken])
->delete($this->company_uri . '/' . 3)
->assertStatus(Response::HTTP_NOT_FOUND);
}
}