lumen 에서 phpunit
실행
test 는 <proj>/vendor/bin/phpunit.bat 를 실행하면 된다.phpunit 에서 server 주소나 port 를 변경
- Laravel\Lumen\Testing\TestCase
vscode 에서 phpunit 실행
vscode 에서 phpunit 실행을 위해서는 extension 을 설치하고, workspace setting 을 수정해주면 된다.debugger
vscode 에서 debugger 를 켠채로 phpunit (extension)을 실행하면 debugging 이 가능하다Auth
lumen 에서는 아래 파일에 자신의 model 을 넣어줘야 한다.- .\database\factories\ModelFactory.php
아래코드에서 $faker 를 사용해서 넣을 수 있는 값들을 넣으면 된다. Generator source 를 보면 가능한 값들이 무엇인지 알 수 있다.
$factory->define(App\Member::class, function (Faker\Generator $faker) {
return [
'mb_id' => $faker->email,
'mb_name' => $faker->name,
// Any other Fields in your Comments Model
];
});
DBUnit
- GitHub - sebastianbergmann/dbunit: DbUnit port for PHP/PHPUnit.
- DBUnit example
- php - PHPUnit and DBUnit - getting started - Stack Overflow
- PHPUnit Manual – Chapter 8. Database Testing
- PHPUnit Manual – Chapter 8. Database Testing> database.understanding-datasets-and-datatables
- dataset 만들기 : Matthew Turland - Database Testing with PHPUnit and MySQL
- composer require --dev phpunit/dbunit
메모리에 SQLite 으로 임시 DB 를 만들어서 DB 관련 test 하기
- http://blog.mauriziobonani.com/laravel-sql-memory-database-for-unit-tests/
- Speeding up PHPUnit tests 15 times
sqlite 을 사용하기
extension 활성화
php.ini 에서 pdo 관련 dll(또는 so) 를 enable 해야 한다.phpunit.xml
phpunit.xml 에서 DB_CONNECTION 을 변경해주면 된다. 당연히 database.php 의 connections 에 있는 값을 넣어줘야 한다.<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
...>
...
<php>
<env name="APP_ENV" value="testing"/>
...
<env name="DB_CONNECTION" value="sqlite" />
</php>
</phpunit>
MySQL data --> Sqlite
Testing
phpunit examples
주의할점은 parent::setUp() 을 꼭 써주자.
class ItemRepayCalendarControllerTest extends TestCase
{
protected $user;
public function setUp()
{
parent::setUp();
$this->user = factory('App\G5Member')->create();
}
/**
* A basic functional test example.
*b/api/v1/add-p2p-corp?name=헤라펀딩
* @return void
*/
public function testAddP2pCorp()
{
// /b/api/v1/calendar/add-p2p-corp?name='.urlencode('헤라펀딩')
$this->actingAs($this->user)
->post('/b/api/v1/calendar/add-p2p-corp', ['name'=>'헤라펀딩'])
->seeJson([
"status" => "success",
]);
}
dbunit example
class RepayPartlyManagerTest extends TestCase
{
use TestCaseTrait;
/**
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
*/
public function getConnection()
{
$id = 'tail';
$pw = 'tail';
$pdo = new PDO('mysql:...', $id, $pw);
$database = 'tail';
return $this->createDefaultDBConnection($pdo, 'tail');
}
/**
* @return PHPUnit_Extensions_Database_DataSet_IDataSet
*/
public function getDataSet()
{
return $this->createMySQLXMLDataSet(__DIR__.'/_files/dataset.xml');
}
public function testGetListOfUsersRepaymentDistribution(){
$conn = $this->getConnection();
$datas = $this->getDataSet();
...
}
}
mockup
testItemMoreInterest() 에서는 테스트하려는 method 안의 특정 method 에 대한 mockup 을 만드는 법을 확인할 수 있다.
<?
class RepayManagerTest extends TestCase
{
...
public function testGetListOfUsersRepaymentDistribution(){
$stubMgrData = $this->createMock(RepayManagerData::class);
// Configure the stub.
$stubMgrData->method('getUsersInvestedAmount')
->willReturn([(object)['rest_amount' => 1000, 'invest_amount_all' => 10000
, 'return_amount_all' => 10000,
'member_idx' => 10000,
'invest_idx' => 10000,
'dstMemGuid' => 'test-guid-1', 'title' => 'test-title'],]);
$stubMgrData->method('getAdminDeposit')
->willReturn(15000);
$stubMgrData->method('insertIntoPartlyDB');
...
}
public function testItemMoreInterest()
{
// mock inside function
$mockedObject = $this->getMockBuilder(ItemMoreInterest::class)
->setConstructorArgs(['first_arg'])
->setMethods(['innerCalledFunc']) // innerCalledFunc 는 override 가 가능해야 한다.
->getMock();
// https://phpunit.de/manual/6.5/en/test-doubles.html#test-doubles.mock-objects.tables.matchers
$mockedObject->expects($this->once())
->method("innerCalledFunc")
->with(
// parameter
// https://phpunit.readthedocs.io/en/9.5/assertions.html?highlight=identicalTo#appendixes-assertions-assertthat-tables-constraints
$this->anything()
)
->willReturn('test-tid');
$result = $mockedObject->sendMoreInterestBody(
1,
'test@test.com',
86,
'테스트product',
1000
);
$this->assertNull($result);
}
}
댓글 없음:
댓글 쓰기