<?php
/**
 *
 * @author：llbjj
 * @DateTime：2022/5/9 12:25
 * @Description：
 *
 */

namespace Application\Command;

use Application\Service\Extension\Helper\ArrayHelper;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use function Co\run;

class UnitTestCommand extends BasicCommand
{
    /**
     * 脚本描述.
     * @var string
     */
    protected static $defaultDescription = '运行单元测试实例。';

    protected function configure()
    {
        $this
            ->addArgument(
                'files',
                InputArgument::IS_ARRAY
            )
        ;
    }

    public function execute(InputInterface $input, OutputInterface $output)
    {
        $updateFiles = $input->getArguments();

        run(function() use ($updateFiles) {
//            $ret = \Swoole\Coroutine\System::exec("/usr/bin/php vendor/bin/phpunit module/Application/test/model --coverage-html report");
            $unitTestFiles = [
                'phpunit.xml.dist',
                'module/Application/test/model/BaseTestUnit.php',
                'module/Application/test/model/DictionaryFormTest.php',
                'module/Application/test/model/HelperTest.php',
                'module/Application/test/model/PatientFormTest.php',
                'module/Application/test/model/PatientModelTest.php',
            ];

            $file = implode(' ', ArrayHelper::merge($unitTestFiles, $updateFiles['files']));
            $ret = \Swoole\Coroutine\System::exec("git fetch && git checkout origin/develop {$file} && /usr/bin/php vendor/bin/phpunit");
            echo $ret['output'];
        });

        return 0;
    }
}

