chaplean / unit-bundle
包含用于功能测试和单元测试的实用工具
v8.0.0
2019-04-09 08:58 UTC
Requires
- php: >=7.1
- doctrine/doctrine-bundle: ^1.6
- doctrine/doctrine-fixtures-bundle: ^3.0
- doctrine/orm: ^2.5
- liip/functional-test-bundle: ^1.9
- mockery/mockery: ^1.0
- nelmio/alice: ^2.0
- phpunit/phpunit: ^7.0
- symfony/browser-kit: ^3.0 || ^4.0
- symfony/config: ^3.0 || ^4.0
- symfony/dependency-injection: ^3.0|^4.0
- symfony/dom-crawler: ^3.0 || ^4.0
- symfony/yaml: ^3.0 || ^4.0
Requires (Dev)
- chaplean/coding-standard: ^1.1
- friendsofsymfony/rest-bundle: ^2.0
- sensio/distribution-bundle: ^4.0 || ^5.0
- sensio/framework-extra-bundle: ^4.0 || ^5.0
- symfony/console: ^3.0 || ^4.0
- symfony/form: ^3.0 || ^4.0
- symfony/http-kernel: ^3.0 || ^4.0
- symfony/monolog-bundle: ^3.0 || ^4.0
- symfony/phpunit-bridge: ^4.0
- symfony/security-bundle: ^3.0 || ^4.0
- symfony/serializer: ^3.0 || ^4.0
- symfony/translation: ^3.0 || ^4.0
- symfony/var-dumper: ^3.0 || ^4.0
- dev-master
- v8.0.0
- 7.3.0
- 7.2.2
- v7.2.1
- v7.2.0
- v7.1.0
- v7.0.1
- v7.0.0
- v6.2.2
- v6.2.1
- v6.2.0
- v6.1.0
- v6.0.0
- v5.2.7
- v5.2.6
- v5.2.5
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.1.1
- v5.1.0
- v5.0.0
- v4.2.3
- v4.2.2
- v4.2.1
- v4.2.0
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.0
- v3.0.7
- v3.0.6
- v3.0.5
- v3.0.4
- v3.0.3
- v3.0.2
- v3.0.1
- v3.0.0
- v2.2.1
- v2.2.0
- v2.1.7
- v2.1.6
- v2.1.5
- v2.1.4
- v2.1.3
- v2.1.2
- v2.1.1
- v2.1.0
- v2.0.0
- v1.7.1
- v1.7.0
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.1
- v1.2.0
- v1.1.8
- v1.1.7
- v1.1.6
- v1.1.5
- v1.1.4
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.0
- dev-feature/v9
- dev-feature/printer
This package is auto-updated.
Last update: 2024-09-10 06:14:17 UTC
README
先决条件
本版本的包需要Symfony 2.8+。
安装
1. Composer
composer require chaplean/unit-bundle
2. AppKernel.php
添加
$bundles[] = new Chaplean\Bundle\UnitBundle\ChapleanUnitBundle();
$bundles[] = new Liip\FunctionalTestBundle\LiipFunctionalTestBundle();
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
3. 导入配置
3.1. 在 config_test.yml
中导入单元测试文件配置
imports: - { resource: '@ChapleanUnitBundle/Resources/config/config.yml' }
3.2. 配置模拟(可选)
在 config_test.yml
中
chaplean_unit: mocked_service: <YourClassImplementingMockedServiceOnSetUpInterface>
示例类
class MockService implements MockedServiceOnSetUpInterface { /** * @return void */ public static function getMockedServices() { $knpPdf = \Mockery::mock('Knp\Bundle\SnappyBundle\Snappy\LoggableGenerator'); $knpPdf->shouldReceive('getOutputFromHtml')->andReturn('example'); $knpPdf->shouldReceive('getOutput')->andReturn('example'); $mocks['knp_snappy.pdf'] = $knpPdf; $client = \Mockery::mock(Client::class); $client->shouldReceive('request')->andReturn(new Response()); $mocks['guzzle.client.sor_api'] = $client; return $mocks; } }
3.3. 添加参数(可选)
打开 app/config/parameters*
文件
添加和更改默认值。 false
值将禁用数据固定加载。
parameters: ... data_fixtures_namespace: App\Bundle\RestBundle\
角色提供者
您可以使用phpunit的@dataProvider
自动运行一个测试,该测试具有不同值的列表。我们可以使用此功能使用单个单元测试测试路由对不同角色的响应。为了实现这一点,我们需要
- 列出角色以及如何登录为该角色的用户
- 创建一个dataProvider,为每个角色提供我们想要的预期结果(通常是http代码)
- 使用@dataProvider编写测试
1. 列出角色
在您的 parameters_test.yml
中添加一个test_roles
字典,如下所示
parameters: # Dictionnary where the key is the name of the role (displayed when a # failure happens), and the value is the reference to an entity used # to do the login (the entity is given to LogicalTestCase::authenticate()). test_roles: NotLogged: '' User: 'user-1' Admin: 'user-2'
2. 创建一个dataProvider
在您的测试类中添加一个提供者
class ExampleTest extends FunctionalTestCase { /** * @return array */ public function rolesMustBeLoggedProvider() { return $this->rolesProvider( // rolesProvider is an utility to map your expectations with the // configured roles. It takes an array with the roles as keys and // your expectations as values. array( 'NotLogged' => Response::HTTP_FORBIDDEN, 'User' => Response::HTTP_OK, 'Admin' => Response::HTTP_OK, ) ); } /** * @return array */ public function rolesWithDifferentExpectations() { return $this->rolesProvider( // You can also give different expectations, see 3. Create a unittest // testWithDifferentExpectations to see how it translates in the test // function signature. array( 'NotLogged' => Response::HTTP_FORBIDDEN, 'User' => array(Response::HTTP_OK), 'Admin' => array(Response::HTTP_OK, 'other expectation), ) ); } /** * @return array */ public function rolesWithExtraRoles() { return $this->rolesProvider( array( 'NotLogged' => Response::HTTP_FORBIDDEN, 'User' => Response::HTTP_OK, 'Admin' => Response::HTTP_OK, ), // You can also provide extra roles, thoses are added to the list // of default roles. Like with regular roles you provide the role // name as key and then the expectations as value, but the first // expectation must be the user to use to log in as. array( 'SpecialCase' => array('user-3', Response::HTTP_OK) ) ); } }
3. 创建一个unittest
使用先前创建的dataProvider编写unittest
class ExampleTest extends FunctionalTestCase { // Data provider ommited, see previous section /** * @dataProvider rolesMustBeLoggedProvider * * @param string $user * @param integer $expectedCode * * @return void */ public function testRouteMustBeLogged($user, $expectedCode) { $client = $this->createClientWith($user); $client->request('/protected/url'); $response = $client->getResponse(); $this->assertEquals($expectedCode, $response->getStatusCode()); } /** * @dataProvider rolesWithDifferentExpectations * * @param string $client * @param integer $expectedCode * @param string $otherExpectation * * @return void */ public function testWithDifferentExpectations($client, $expectedCode, $otherExpectation = null) { // $otherExpectation is not defined for every value in the provider so we must default to null } }
自定义打印机
如果您想使用自定义打印机,请将printerClass
属性添加到phpunit.xml
中,并使用值为Chaplean\Bundle\UnitBundle\TextUI\ResultPrinter
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <!-- ... --> printerClass="Chaplean\Bundle\UnitBundle\TextUI\ResultPrinter" >