Skip to content

Commit

Permalink
remove name 'require' validation and update/improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammad-Alavi committed Oct 1, 2021
1 parent 73b175f commit f8147dc
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function rules(): array
return [
'email' => 'required|email|max:40|unique:users,email',
'password' => 'required|min:6|max:30',
'name' => 'required|min:2|max:50',
'name' => 'min:2|max:50',
'gender' => 'in:male,female,unspecified',
'birth' => 'date',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Containers\AppSection\User\UI\API\Tests\Functional;

use App\Containers\AppSection\User\UI\API\Tests\ApiTestCase;
use Illuminate\Testing\Fluent\AssertableJson;

/**
* Class RegisterUserTest.
Expand All @@ -25,120 +26,94 @@ public function testRegisterNewUserWithCredentials(): void
{
$data = [
'email' => '[email protected]',
'name' => 'Apiato',
'password' => 'secretpass',
];

$response = $this->makeCall($data);

$response->assertStatus(200);
$this->assertResponseContainKeyValue([
'email' => $data['email'],
'name' => $data['name'],
]);
$responseContent = $this->getResponseContentObject();
$this->assertNotEmpty($responseContent->data);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('data')
->where('data.email', $data['email'])
->etc()
);
}

public function testRegisterNewUserUsingGetVerb(): void
{
$data = [
'email' => '[email protected]',
'name' => 'Apiato',
'password' => 'secret',
];

$response = $this->endpoint('get@v1/register')->makeCall($data);

$response->assertStatus(405);
$this->assertResponseContainKeyValue([
'message' => 'The GET method is not supported for this route. Supported methods: POST.',
]);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('message')
->where('message', 'The GET method is not supported for this route. Supported methods: POST.')
->etc()
);
}

public function testRegisterExistingUser(): void
{
$userDetails = [
'email' => '[email protected]',
'name' => 'Apiato',
'password' => 'secret',
];

$this->getTestingUser($userDetails);

$data = [
'email' => $userDetails['email'],
'name' => $userDetails['name'],
'password' => $userDetails['password'],
];

$response = $this->makeCall($data);

$response->assertStatus(422);
$this->assertValidationErrorContain([
'email' => 'The email has already been taken.',
]);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('errors')
->where('errors.email.0', 'The email has already been taken.')
->etc()
);
}

public function testRegisterNewUserWithoutEmail(): void
public function testRegisterNewUserWithoutData(): void
{
$data = [
'name' => 'Apiato',
'password' => 'secret',
];

$response = $this->makeCall($data);

$response->assertStatus(422);
// assert response contain the correct message
$this->assertValidationErrorContain([
'email' => 'The email field is required.',
]);
}

public function testRegisterNewUserWithoutName(): void
{
$data = [
'email' => '[email protected]',
'password' => 'secret',
];

$response = $this->makeCall($data);

$response->assertStatus(422);
$this->assertValidationErrorContain([
'name' => 'The name field is required.',
]);
}

public function testRegisterNewUserWithoutPassword(): void
{
$data = [
'email' => '[email protected]',
'name' => 'Apiato',
];
$data = [];

$response = $this->makeCall($data);

$response->assertStatus(422);
$this->assertValidationErrorContain([
'password' => 'The password field is required.',
]);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('errors')
->where('errors.email.0', 'The email field is required.')
->where('errors.password.0', 'The password field is required.')
->etc()
);
}

public function testRegisterNewUserWithInvalidEmail(): void
{
$data = [
'email' => 'missing-at.test',
'name' => 'Apiato',
'password' => 'secret',
];

$response = $this->makeCall($data);

$response->assertStatus(422);
$this->assertValidationErrorContain([
'email' => 'The email must be a valid email address.',
]);
$response->assertJson(
fn (AssertableJson $json) =>
$json->has('errors')
->where('errors.email.0', 'The email must be a valid email address.')
->etc()
);
}
}

0 comments on commit f8147dc

Please sign in to comment.