Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
srgoogle23 committed Dec 28, 2024
1 parent e381d14 commit 2ca8100
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions test/Cases/UserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

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

namespace HyperfTest\Cases;

use Hyperf\Testing\TestCase;

/**
* @internal
* @coversNothing
*/
class UserTest extends TestCase
{
public function testAddValidUser()
{
$user = [
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 'Password1@',
];
$response = $this->post('/users', $user);

$this->assertSame(201, $response->getStatusCode());
$responseContent = $response->toArray();
$this->assertArrayHasKey('id', $responseContent);
unset($responseContent['id']);
$this->assertSame($user, $responseContent);
}

public function testAddDuplicateUser()
{
$user = [
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 'Password1@',
];
$response = $this->post('/users', $user);

$this->assertSame(409, $response->getStatusCode());
$responseContent = $response->toArray();
$this->assertSame('User already exists.', $responseContent['message']);
}

public function testAddUserInvalidEmail()
{
$user = [
'name' => 'John Doe',
'email' => 'johnexample.com',
'password' => 'Password1@',
];
$response = $this->post('/users', $user);

$this->assertSame(400, $response->getStatusCode());
$responseContent = $response->toArray();
$this->assertSame('Invalid email address.', $responseContent['message']);
}

public function testAddUserInvalidPassword()
{
$user = [
'name' => 'John Doe',
'email' => '[email protected]',
'password' => 'password',
];
$response = $this->post('/users', $user);

$this->assertSame(400, $response->getStatusCode());
$responseContent = $response->toArray();
$this->assertSame('Password must contain at least one uppercase letter, one lowercase letter, one number and one special character.', $responseContent['message']);
}

public function testAddUserInvalidName()
{
$user = [
'name' => 'J1',
'email' => '[email protected]',
'password' => 'Password1@',
];
$response = $this->post('/users', $user);

$this->assertSame(400, $response->getStatusCode());
$responseContent = $response->toArray();
$this->assertSame('Name must have at least two characters and cannot contain numbers.', $responseContent['message']);
}
}

0 comments on commit 2ca8100

Please sign in to comment.