apostle/phpunit-validation

在PHPUnit中使用Symfony Validator组件

dev-master / 1.0.x-dev 2014-08-06 12:32 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:47:26 UTC


README

PHPUnit Validation包允许你在PHPUnit测试中使用Symfony Validator验证规则。

一些现有的PHPUnit断言已经被Symfony验证规则如assertTrueassertFalse覆盖。

安装

安装和更新你的composer.json一样简单。

{
    "require": {
        "apostle/phpunit-validation": "1.0.*@dev"
    }
}

使用

使用方法与安装一样简单。你只需使用这个库提供的基类测试用例,而不是默认的基类测试用例。

<?php

use Apostle\PHPUnit\TestCase;

class FooTest extends TestCase
{
    public function testBar()
    {
        $this->assertEmail('foo@example.com');
    }
}

当然,这并不算特别有趣。最好的改进之处在于你可以如何验证集合。你可以在数组内使用断言,而不仅仅是可以精确匹配数组或不匹配数组。

<?php

use Apostle\PHPUnit\TestCase;

class FooTest extends TestCase
{
    public function testBar()
    {
        $data = array(
            'roles' => array('ROLE_USER', 'ROLE_ADMIN', 'ROLE_SUPER_ADMIN')
        );

        $this->assertCollection(array(
            'roles' => $this->all(array($this->matchesRegex('/ROLE_(.+)/')))
        ), $data);
    }

    public function testBaz()
    {
        $data = array('this', 'that', 'thus');

        $this->assertAll(array(
            $this->matchesRegex('/th(is|at|us)/')
        ), $data);
    }
}

所有当前存在的断言示例可以在示例文件中找到。

额外

你也可以使用这个库提供的断言子集,甚至只是其中一个断言,这要归功于特性。

<?php

use Apostle\PHPUnit\Assert\Basic;

class FooTest extends \PHPUnit_Framework_TestCase
{
    use Basic;

    public function barTest()
    {
        $this->assertType('integer', 1);
    }
}

许可证

此库在BSD 2条款许可证下授权。

Copyright (c) 2014, Apostle <info@apostle.nl>
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.