Skip to content

Instantly share code, notes, and snippets.

@jaonoctus
Last active March 28, 2019 00:55
Show Gist options
  • Save jaonoctus/a4770900c9fda743f8e45a267ec57409 to your computer and use it in GitHub Desktop.
Save jaonoctus/a4770900c9fda743f8e45a267ec57409 to your computer and use it in GitHub Desktop.
<?php
namespace Tests\Feature;
use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\Response;
class UserUpdateTest extends TestCase
{
use RefreshDatabase, WithFaker;
/** @test */
public function o_usuario_precisa_estar_autenticado()
{
$attributes = $this->validAttributes();
$response = $this->makeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNAUTHORIZED);
}
/** @test */
public function o_usuario_pode_atualizar_informacoes_do_proprio_perfil()
{
$attributes = $this->validAttributes();
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_NO_CONTENT);
$userAfterUpdate = auth()->user();
// verifica que os dados foram atualizados.
$this->assertEquals($userAfterUpdate->name, $attributes['name']);
$this->assertEquals($userAfterUpdate->email, $attributes['email']);
$this->assertEquals($userAfterUpdate->profile_picture, $attributes['profile_picture']);
$this->assertEquals($userAfterUpdate->phone, $attributes['phone']);
// verifica que a senha foi atualizada.
$isPasswordUpdated = auth()->once(['email' => $attributes['email'], 'password' => $attributes['new_password']]);
$this->assertTrue($isPasswordUpdated, 'A senha não foi atualizada.');
}
/** @test */
public function o_nome_precisa_ser_informado()
{
$attributes = $this->invalidAttributes();
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonValidationErrors(['name']);
}
/** @test */
public function o_email_precisa_ser_informado()
{
$attributes = $this->invalidAttributes();
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonValidationErrors(['email']);
}
/** @test */
public function a_foto_de_perfil_precisa_ser_um_url()
{
$attributes = $this->invalidAttributes();
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonValidationErrors(['profile_picture']);
}
/** @test */
public function a_senha_atual_precisa_estar_correta()
{
$attributes = $this->invalidAttributes();
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonValidationErrors(['current_password']);
}
/** @test */
public function a_nova_senha_precisa_ser_confirmada()
{
$attributes = [
'current_password' => $this->validAttributes()['current_password'],
'new_password' => $this->invalidAttributes()['new_password'],
'new_password_confirmation' => $this->invalidAttributes()['new_password_confirmation'],
];
$response = $this->authThenMakeUpdateRequest($attributes);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonValidationErrors(['new_password']);
}
/** @test */
public function nao_pode_usar_um_email_repetido()
{
$firstUser = factory(User::class)->create();
$secondUser = factory(User::class)->create();
$attributes = $this->validAttributes();
$attributes['email'] = $secondUser->email;
$response = $this->authThenMakeUpdateRequest($attributes, $firstUser);
$response->assertJsonValidationErrors(['email']);
}
private function makeUpdateRequest($attributes = [])
{
return $this->put(route('user.update'), $attributes);
}
private function authThenMakeUpdateRequest($attributes, $user = null)
{
$this->signIn($user);
return $this->makeUpdateRequest($attributes);
}
private function validAttributes()
{
return [
'name' => $this->faker->name,
'email' => $this->faker->unique()->safeEmail,
'profile_picture' => null,
'phone' => $this->faker->unique()->phoneNumber,
'current_password' => 'password',
'new_password' => 'new_password',
'new_password_confirmation' => 'new_password'
];
}
private function invalidAttributes()
{
return [
'name' => '',
'email' => '',
'profile_picture' => 'not a url',
'phone' => 'not a number',
'current_password' => 'not_the_user_password',
'new_password' => 'the new password',
'new_password_confirmation' => 'does not match'
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment