idrinth / phpunit-test-generator
基于现有的composer.json文件生成与PHPunit一起使用的测试
dev-master
2024-07-21 17:10 UTC
Requires
- php: ^5.3.3|^7.0.0
- ext-json: *
- composer/semver: ^1.4.2
- nikic/php-parser: ^1.4.1
- psr/container: ^1.0.0
- symfony/finder: ^2.6.13
- twig/twig: ^1.35.0
Requires (Dev)
- mikey179/vfsstream: ^1.6.5
- phpunit/phpunit: ^4.8.36|^6.5.7|^7.0.2
Suggests
- squizlabs/php_codesniffer: For formatting according to PSR2
This package is auto-updated.
Last update: 2024-09-21 17:29:35 UTC
README
命令与选项
php bin/generate-tests --dir=/path/to/alternative/dir --mode=replace
dir
dir是可选的,默认为当前工作目录。选择composer.json所在的目录。
replace(已弃用,请参阅mode)
如果设置,旧测试文件将被覆盖,如果没有设置,则将重命名。通常你不需要这个。
mode
replace、skip或move之一。定义如何处理文件冲突,默认为移动旧文件。
output
如果设置,将添加到dev-autoload路径的相对路径之前。
它是如何工作的?
工作目录(或提供的dir)中的composer.json将解析命名空间(psr 0和4),搜索由非dev命名空间针对的文件夹,并在适当的开发自动加载文件夹中生成测试类。为了确定要使用的测试类,将解析phpunit开发依赖项,因此你需要提供约束以使生成工作。
生成的测试 - 示例
这是为这个库的实际控制器生成的测试文件。目的是生成PSR2兼容的代码,显然这可能在所有地方都还未实现。如果你发现其中存在失败的地方,请随时提交错误报告。
<?php namespace De\Idrinth\TestGenerator\Test; use PHPUnit\Framework\TestCase as TestCaseImplementation; use De\Idrinth\TestGenerator\Controller; /** * this is an automatically generated skeleton for testing Controller * @todo actually test **/ class ControllerTest extends TestCaseImplementation { /** * @return Controller * @todo make sure the construction works as expected **/ protected function getInstance() { return new Controller( $this->getMockBuilder('Symfony\Component\Finder\Finder')->getMock(), $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\ClassReader')->getMock(), $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\ClassWriter')->getMock(), $this->getMockBuilder('De\Idrinth\TestGenerator\Interfaces\Composer')->getMock(), null ); } /** * From Controller * @test * @todo replace with actual tests **/ public function testInit () { $instance = $this->getInstance(); $return = $instance->init(); $this->assertInternalType( 'object', $return, 'Return didn\'t match expected type object' ); $this->assertInstanceOf( 'De\Idrinth\TestGenerator\Controller', $return, 'Return didn\'t match expected instance De\Idrinth\TestGenerator\Controller' ); $this->markTestIncomplete( 'This test has not been implemented yet.' ); } /** * From Controller * @test * @todo replace with actual tests **/ public function testRun () { $instance = $this->getInstance(); $return = $instance->run(); $this->assertInternalType( 'null', $return, 'Return didn\'t match expected type null' ); $this->markTestIncomplete( 'This test has not been implemented yet.' ); } }