Swoft单元测试基本坑

前言:

基于项目中使用swoft写Api接口服务,要求使用phpunit单元测试,趟过两个大坑。

1. 报各种类的找不到,原因是swoft框架测试基础类SwoftTest\Testing\TestApplication 无法自动加载。

解决办法:需要手动加载,更改文件test/bootstrap.php

use Composer\Autoload\ClassLoader;
use SwoftTest\Testing\TestApplication;


/** @var ClassLoader $loader */
$loader = require dirname(__DIR__) . '/vendor/autoload.php';

$componentDir  = dirname($loader->findFile("Swoft"), 2);
$componentJson = $componentDir . '/composer.json';
$composerData  = json_decode(file_get_contents($componentJson), true);
foreach ($composerData['autoload-dev']['psr-4'] as $prefix => $dir) {
    $loader->addPsr4($prefix, $componentDir . '/' . $dir);
}
//$FormPage= new \App\Testing\Entity\FormPage();
//var_dump($loader);die;

$application = new TestApplication();
$application->setBeanFile(__DIR__ . '/testing/bean.php');
$application->run();

2. 报关于协程的错误

PHP Fatal error:  Uncaught Swoole\Error: API must be called in the coroutine in /webroot/vendor/swoft/connection-pool/src/AbstractPool.php:296
解决办法:

到swoft兄弟项目Hyperf测试组件,把co-phpunit复制到你想放到的位置,我自己放到vender/bin/目录下,执行测试时用vendor/bin/co-phpunit就可以了。

[root@you webroot]# vendor/bin/co-phpunit --configuration phpunit.xml --coverage-text --colors=never
PHPUnit 7.5.18 by Sebastian Bergmann and contributors.

.....                                                               5 / 5 (100%)

Time: 4.77 seconds, Memory: 38.00 MB

OK (5 tests, 5 assertions)

Generating code coverage report in HTML format ... done


Code Coverage Report:      
  2019-12-18 19:47:35      
                           
 Summary:                  
  Classes:  2.33% (2/86)   
  Methods:  3.67% (19/518) 
  Lines:    4.55% (61/1340)

后续再有什么坑,之后再写!!!!!!

你可能感兴趣的:(phpunit,swoft,PHP)