API project base on YII2 basic
Run the below command under the project to get the unittest feedback.
./phpunit
Run the below command under the prooject to test the ChannelTest
./phpunit tests/Unit/ModelsTest/ChannelTest.php
./phpunit tests/Unit/ModelsTest/ChannelTest.php --no-coverage
- Mock redis
use \PHPUnit\Framework\TestCase;
use \Mockery;
use app\components\redis\HQRedis;
$HQRedisMock = Mockery::mock('alias:' . HQRedis::class);- Mock DB
use \Mockery;
use yii\db\ActiveRecord;
$activeRecordMock = Mockery::mock('alias:' . ActiveRecord::class);- Don't forget to close the mock
public function tearDown(): void
{
Mockery::close();
parent::tearDown();
}- Install xdebug
- Uncomment phpunit.xml attribute log with coverage-html to output unittest html format.
<log type="coverage-html" target="./tests/_report/coverage/" lowUpperBound="35" highLowerBound="70"/> - Run command
./phpunit - Open the file in
tests/_report/coverage/index.htmlwith your browser. - Without coverage, run command
./phpunit --no-coverage