đã tạo được test case của createUser
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class AccountManagementTest extends DuskTestCase
|
||||
{
|
||||
/**
|
||||
* Helper: đăng ký tài khoản mới và đăng nhập.
|
||||
*/
|
||||
private function registerAndLogin(Browser $browser, string $phone): void
|
||||
{
|
||||
$browser->visit('/login')
|
||||
->type('name', 'Dusk Test User')
|
||||
->type('phone', $phone)
|
||||
->type('address', 'Cần Thơ')
|
||||
->radio('known_sis', '1')
|
||||
->type('job', 'Nhân viên văn phòng')
|
||||
->press('Gửi thông tin')
|
||||
->assertPathIs('/game');
|
||||
}
|
||||
|
||||
/**
|
||||
* Cập nhật tài khoản thành công.
|
||||
*/
|
||||
public function test_update_account_successfully(): void
|
||||
{
|
||||
$phone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($phone) {
|
||||
$this->registerAndLogin($browser, $phone);
|
||||
|
||||
$browser->visit('/login')
|
||||
->assertSee('Cập nhật thông tin cá nhân')
|
||||
->clear('name')
|
||||
->type('name', 'Tên Mới Test')
|
||||
->clear('address')
|
||||
->type('address', 'Hà Nội')
|
||||
->radio('known_sis', '0')
|
||||
->clear('job')
|
||||
->type('job', 'Giáo viên')
|
||||
->press('Cập nhật')
|
||||
->assertPathIs('/game')
|
||||
->assertSee('Cập nhật tài khoản thành công');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Cập nhật tài khoản với tên rỗng phải hiện lỗi.
|
||||
*/
|
||||
public function test_update_account_with_empty_name_shows_error(): void
|
||||
{
|
||||
$phone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($phone) {
|
||||
$this->registerAndLogin($browser, $phone);
|
||||
|
||||
$browser->visit('/login')
|
||||
->clear('name')
|
||||
->press('Cập nhật')
|
||||
->assertSee('Nhập tên hiển thị');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Cập nhật tài khoản với số điện thoại sai phải hiện lỗi.
|
||||
*/
|
||||
public function test_update_account_with_invalid_phone_shows_error(): void
|
||||
{
|
||||
$phone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($phone) {
|
||||
$this->registerAndLogin($browser, $phone);
|
||||
|
||||
$browser->visit('/login')
|
||||
->clear('phone')
|
||||
->type('phone', 'abcde')
|
||||
->press('Cập nhật')
|
||||
->assertSee('Số điện thoại không đúng định dạng');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Trang lịch sử hoạt động hiển thị khi đã đăng nhập.
|
||||
*/
|
||||
public function test_history_section_visible_when_authenticated(): void
|
||||
{
|
||||
$phone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($phone) {
|
||||
$this->registerAndLogin($browser, $phone);
|
||||
|
||||
$browser->visit('/login')
|
||||
->assertSee('LỊCH SỬ HOẠT ĐỘNG');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng xuất thành công.
|
||||
*/
|
||||
public function test_logout_works(): void
|
||||
{
|
||||
$phone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($phone) {
|
||||
$this->registerAndLogin($browser, $phone);
|
||||
|
||||
$browser->visit('/logout')
|
||||
->assertGuest();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class CreateAccountTest extends DuskTestCase
|
||||
{
|
||||
/**
|
||||
* Trang đăng ký / đăng nhập hiển thị form nhập thông tin.
|
||||
*/
|
||||
public function test_login_page_shows_form(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/login')
|
||||
->assertSee('Nhập thông tin cá nhân')
|
||||
->assertPresent('input[name="name"]')
|
||||
->assertPresent('input[name="phone"]')
|
||||
->assertPresent('input[name="address"]')
|
||||
->assertPresent('input[name="job"]');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Submit form rỗng phải hiện lỗi validation.
|
||||
*/
|
||||
public function test_empty_form_shows_validation_errors(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/login')
|
||||
->press('Gửi thông tin')
|
||||
->assertPresent('.text-danger');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Số điện thoại sai định dạng phải hiện lỗi.
|
||||
*/
|
||||
public function test_invalid_phone_shows_error(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/login')
|
||||
->type('name', 'Nguyễn Test')
|
||||
->type('phone', '12345')
|
||||
->type('address', 'Hà Nội')
|
||||
->radio('known_sis', '0')
|
||||
->type('job', 'Kỹ sư')
|
||||
->press('Gửi thông tin')
|
||||
->pause(400)
|
||||
->assertSee('Số điện thoại không đúng định dạng');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng ký thành công với số điện thoại mới và redirect về /game.
|
||||
* Lưu ý: test này dùng số điện thoại ngẫu nhiên để tránh trùng DB.
|
||||
*/
|
||||
public function test_successful_registration_redirects_to_game(): void
|
||||
{
|
||||
$uniquePhone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($uniquePhone) {
|
||||
$browser->visit('/login')
|
||||
->type('name', 'Test Dusk User')
|
||||
->type('phone', $uniquePhone)
|
||||
->type('address', 'Cần Thơ')
|
||||
->radio('known_sis', '1')
|
||||
->type('job', 'Kỹ sư')
|
||||
->press('Gửi thông tin')
|
||||
->waitForText('thành công', 10)
|
||||
->assertPathIs('/game');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Khi đã đăng nhập, GET /login hiển thị trang tài khoản (không có form đăng ký).
|
||||
*/
|
||||
public function test_authenticated_user_sees_account_page(): void
|
||||
{
|
||||
$uniquePhone = '09' . rand(10000000, 99999999);
|
||||
|
||||
$this->browse(function (Browser $browser) use ($uniquePhone) {
|
||||
// Đảm bảo guest (xóa session từ test trước)
|
||||
$browser->visit('/logout');
|
||||
|
||||
// Đăng ký và đăng nhập
|
||||
$browser->visit('/login')
|
||||
->type('name', 'Dusk Auth User')
|
||||
->type('phone', $uniquePhone)
|
||||
->type('address', 'TP.HCM')
|
||||
->radio('known_sis', '0')
|
||||
->type('job', 'Bác sĩ')
|
||||
->press('Gửi thông tin')
|
||||
->waitForText('thành công', 10)
|
||||
->assertPathIs('/game');
|
||||
|
||||
// Truy cập lại /login sau khi đã đăng nhập: phone field phải được điền sẵn (chỉ xảy ra khi đã login)
|
||||
$browser->visit('/login')
|
||||
->assertInputValue('phone', $uniquePhone);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class ExampleTest extends DuskTestCase
|
||||
{
|
||||
/**
|
||||
* A basic browser test example.
|
||||
*/
|
||||
public function test_basic_example(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/')
|
||||
->assertPathIs('/game');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
use Tests\DuskTestCase;
|
||||
|
||||
class GamePageTest extends DuskTestCase
|
||||
{
|
||||
/**
|
||||
* Trang game hiển thị đúng khi chưa đăng nhập.
|
||||
*/
|
||||
public function test_game_page_loads_when_guest(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/game')
|
||||
->assertPathIs('/game');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Trang chủ (/) redirect về game.
|
||||
*/
|
||||
public function test_root_redirects_to_game(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/')
|
||||
->assertPathIs('/game');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Trang game hiển thị vòng quay may mắn.
|
||||
*/
|
||||
public function test_game_page_shows_lucky_wheel(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/game')
|
||||
->assertPresent('#wheel, canvas, .wheel, [id*="wheel"], [class*="wheel"]');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Popup luật chơi có thể tải được.
|
||||
*/
|
||||
public function test_game_rule_endpoint_returns_content(): void
|
||||
{
|
||||
$this->browse(function (Browser $browser) {
|
||||
$browser->visit('/game/rule')
|
||||
->assertSee('luật');
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Browser;
|
||||
|
||||
class HomePage extends Page
|
||||
{
|
||||
/**
|
||||
* Get the URL for the page.
|
||||
*/
|
||||
public function url(): string
|
||||
{
|
||||
return '/';
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that the browser is on the page.
|
||||
*/
|
||||
public function assert(Browser $browser): void
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the element shortcuts for the page.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public function elements(): array
|
||||
{
|
||||
return [
|
||||
'@element' => '#selector',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Browser\Pages;
|
||||
|
||||
use Laravel\Dusk\Page as BasePage;
|
||||
|
||||
abstract class Page extends BasePage
|
||||
{
|
||||
/**
|
||||
* Get the global element shortcuts for the site.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
public static function siteElements(): array
|
||||
{
|
||||
return [
|
||||
'@element' => '#selector',
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
@@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
||||
Reference in New Issue
Block a user