Skip to content

Commit

Permalink
test: Auth routes and UserFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
andeen171 committed Sep 28, 2024
1 parent 32b8b59 commit 9b8637d
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
53 changes: 53 additions & 0 deletions test/Cases/Auth/SignInTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Cases\Auth;


use App\Model\User;
use Faker;
use Hyperf\Testing\TestCase;

class SignInTest extends TestCase
{
protected const ROUTE = '/auth/sign-in';

protected Faker\Generator $faker;
protected User $user;
protected string $password;

public function __construct($name = null)
{
parent::__construct($name);
$this->faker = Faker\Factory::create();
}

public function setUp(): void
{
$this->password = $this->faker->password();

$this->user = factory(User::class)->create([
'password' => password_hash($this->password, PASSWORD_DEFAULT),
]);
}

public function testSignIn()
{
$data = [
'email' => $this->user->email,
'password' => $this->password,
];

$response = $this->post(self::ROUTE, $data);
$response->assertOk()->assertJsonStructure(['token']);
}
}
56 changes: 56 additions & 0 deletions test/Cases/Auth/SignUpTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/

namespace HyperfTest\Cases\Auth;


use App\Enum\UserTypeEnum;
use Faker;
use Hyperf\Testing\TestCase;

class SignUpTest extends TestCase
{
protected const ROUTE = '/auth/sign-up';

protected Faker\Generator $faker;

public function __construct($name = null)
{
parent::__construct($name);
$this->faker = Faker\Factory::create();
}

public function testSignUp()
{
$data = [
'type' => UserTypeEnum::COMMON->value,
'firstName' => $this->faker->firstName,
'lastName' => $this->faker->lastName,
'document' => $this->faker->numerify('###########'),
'email' => $this->faker->email,
'password' => $password = $this->faker->password,
'passwordConfirmation' => $password,
];

$response = $this->post(self::ROUTE, $data);
$response->assertCreated()->assertJsonStructure([
'data' => [
'id',
'type',
'firstName',
'lastName',
'document',
'email',
]
]);
}
}
20 changes: 20 additions & 0 deletions test/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

use App\Enum\UserTypeEnum;
use App\Model\User;
use Faker\Generator as Faker;
use Hyperf\Database\Model\Factory;

/* @var Factory $factory */
$factory->define(User::class, function (Faker $faker) {
return [
'type' => $faker->randomElement(UserTypeEnum::values()),
'first_name' => $faker->firstName,
'last_name' => $faker->lastName,
'email' => $faker->unique()->safeEmail,
'password' => password_hash('password', PASSWORD_DEFAULT),
'document' => $faker->numerify('###########'),
];
});

0 comments on commit 9b8637d

Please sign in to comment.