๊ฐ๋ ์๊ฐ
Laravel Testing์์ Mock ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ โoverload:โ์ โalias:โ๋ ์๋ก ๋ค๋ฅธ ๋ชฉ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ค์ํ ํจํด์ด๋ค.
Mock ํ์ ๋ณ ํน์ง
overload: ํจํด
์๋ก์ด ํด๋์ค ์ธ์คํด์ค ์์ฑ์ ๊ฐ๋ก์ฑ์ Mock ๊ฐ์ฒด๋ก ๋์ฒดํ๋ ๋ฐฉ์์ด๋ค.
์ฃผ์ ํน์ง
- new ํค์๋๋ก ์์ฑ๋๋ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ก์ฑ
- ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ํ ์คํธํ ๋ ์ฌ์ฉ
- ์์กด์ฑ ์ฃผ์ ์ด ์ด๋ ค์ด ์ํฉ์์ ์ ์ฉ
class OrderProcessor {
public function process()
{
$paymentGateway = new PaymentGateway();
return $paymentGateway->charge();
}
}
class OrderProcessorTest extends TestCase
{
public function testProcessOrder()
{
// PaymentGateway ์ธ์คํด์ค ์์ฑ์ ๊ฐ๋ก์ฑ
$mock = Mockery::mock('overload:PaymentGateway');
$mock->shouldReceive('charge')
->once()
->andReturn(true);
$processor = new OrderProcessor();
$result = $processor->process();
$this->assertTrue($result);
}
}alias: ํจํด
์ ์ (static) ๋ฉ์๋ ํธ์ถ์ Mock ๊ฐ์ฒด๋ก ๋์ฒดํ๋ ๋ฐฉ์์ด๋ค.
์ฃผ์ ํน์ง
- ํด๋์ค์ static ๋ฉ์๋๋ฅผ ๋์ฒด
- ํ์ฌ๋(Facade) ํจํด์ด๋ ์ ์ ๋ฉ์๋๋ฅผ ํ ์คํธํ ๋ ์ฌ์ฉ
- ์ ์ญ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ ํธ๋ฆฌํฐ ๋ฉ์๋ ํ ์คํธ์ ์ ์ฉ
class NotificationService {
public function send()
{
return Logger::log('Notification sent');
}
}
class NotificationServiceTest extends TestCase
{
public function testSendNotification()
{
// Logger์ ์ ์ ๋ฉ์๋๋ฅผ Mock์ผ๋ก ๋์ฒด
$mock = Mockery::mock('alias:Logger');
$mock->shouldReceive('log')
->with('Notification sent')
->once()
->andReturn(true);
$service = new NotificationService();
$result = $service->send();
$this->assertTrue($result);
}
}์ค์ ํ์ฉ ์ฌ๋ก
overload: ํ์ฉ ์์
class UserRepository {
public function create($data)
{
$validator = new DataValidator();
if ($validator->validate($data)) {
return User::create($data);
}
return false;
}
}
class UserRepositoryTest extends TestCase
{
public function testCreateUser()
{
// DataValidator ์ธ์คํด์ค ์์ฑ์ ๊ฐ๋ก์ฑ
$validatorMock = Mockery::mock('overload:DataValidator');
$validatorMock->shouldReceive('validate')
->once()
->andReturn(true);
$repo = new UserRepository();
$result = $repo->create(['name' => 'Test User']);
$this->assertInstanceOf(User::class, $result);
}
}alias: ํ์ฉ ์์
class EmailService {
public function sendWelcomeEmail($user)
{
if (Config::get('mail.enabled')) {
return Mailer::send('welcome', ['user' => $user]);
}
return false;
}
}
class EmailServiceTest extends TestCase
{
public function testSendWelcomeEmail()
{
// Config์ Mailer์ ์ ์ ๋ฉ์๋๋ฅผ Mock์ผ๋ก ๋์ฒด
$configMock = Mockery::mock('alias:Config');
$configMock->shouldReceive('get')
->with('mail.enabled')
->andReturn(true);
$mailerMock = Mockery::mock('alias:Mailer');
$mailerMock->shouldReceive('send')
->once()
->andReturn(true);
$service = new EmailService();
$result = $service->sendWelcomeEmail(['name' => 'Test User']);
$this->assertTrue($result);
}
}์ฌ์ฉ ์ ์ฃผ์์ฌํญ
overload: ์ฃผ์์ฌํญ
- ํ ์คํธ ์คํ ์ค ํด๋น ํด๋์ค์ ๋ชจ๋ ์ธ์คํด์ค ์์ฑ์ ์ํฅ
- ํ ๋ฒ overload๋ ํด๋์ค๋ ํด๋น ํ ์คํธ ์คํ ๋์ ๊ณ์ ์ ์ฉ
- ๋ค๋ฅธ ํ ์คํธ์ ์ํฅ์ ์ค ์ ์์ผ๋ฏ๋ก tearDown์์ ์ ๋ฆฌ ํ์
alias: ์ฃผ์์ฌํญ
- ์ ์ ๋ฉ์๋๋ง mock ๊ฐ๋ฅ
- ์ผ๋ฐ ์ธ์คํด์ค ๋ฉ์๋๋ ์ํฅ๋ฐ์ง ์์
- ์ ์ญ์ ์ผ๋ก ์ ์ฉ๋๋ฏ๋ก ๋ค๋ฅธ ํ ์คํธ์์ ๊ฒฉ๋ฆฌ ์ฃผ์
์ ํ ๊ฐ์ด๋
overload: ์ฌ์ฉ ์๊ธฐ
- new ํค์๋๋ก ์์ฑ๋๋ ๊ฐ์ฒด๋ฅผ ๋์ฒดํด์ผ ํ ๋
- ์์กด์ฑ ์ฃผ์ ์ด ์ด๋ ค์ด ๋ ๊ฑฐ์ ์ฝ๋ ํ ์คํธ
- ์ธ์คํด์ค ์์ฑ ๊ณผ์ ์ ์ ์ดํด์ผ ํ ๋
alias: ์ฌ์ฉ ์๊ธฐ
- ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋ ํ ์คํธ
- Laravel Facade ํจํด ํ ์คํธ
- ์ ์ญ์ ์ผ๋ก ์ฌ์ฉ๋๋ ์ ํธ๋ฆฌํฐ ํจ์ ํ ์คํธ
์ฑ๋ฅ ๊ณ ๋ ค์ฌํญ
overload:
- ํด๋์ค ๋ก๋ ์์ค์์ ๋์ํ๋ฏ๋ก ์ฝ๊ฐ์ ์ฑ๋ฅ ์ํฅ
- ๋ง์ ์์ overload๋ฅผ ์ฌ์ฉํ ๊ฒฝ์ฐ ํ ์คํธ ์คํ ์๋ ์ ํ ๊ฐ๋ฅ
alias:
- ์ ์ ํธ์ถ๋ง ์ํฅ๋ฐ์ผ๋ฏ๋ก ์๋์ ์ผ๋ก ๊ฐ๋ฒผ์
- ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ด ์ ์
๊ฒฐ๋ก
Laravel ํ ์คํธ์์ โoverload:โ์ โalias:โ๋ ๊ฐ๊ฐ ์ธ์คํด์ค ์์ฑ๊ณผ ์ ์ ๋ฉ์๋ ํธ์ถ์ Mockํ๋ ๋ฐ ํนํ๋ ํจํด์ด๋ค. ํ ์คํธ ๋์ ์ฝ๋์ ํน์ฑ๊ณผ ์์กด์ฑ ํจํด์ ๋ฐ๋ผ ์ ์ ํ ๋ฐฉ์์ ์ ํํด์ผ ํ๋ค.