标签: phpunit

TDD:如何测试搜索?

我的网站将有一个高级搜索.人们可以去那里寻找一个权利(例如汽车).我已经创建了一些测试,根据搜索参数检查结果的数量.我想我应该写什么测试,然后我写它,然后我将数据添加到测试数据库.但问题来了.当我向数据库插入新值时,我的旧测试会中断.那是因为我正在检查记录的数量......

<?php defined('SYSPATH') or die('No direct access allowed!');

class Search_Test extends PHPUnit_Extensions_Database_TestCase
{
    /**
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
     */
    public function getConnection()
    {
        $pdo = new PDO('mysql:dbname=db_test;host=127.0.0.1', 'root', null);
        return $this->createDefaultDBConnection($pdo, 'db_test');
    }

    /**
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
     */
    public function getDataSet()
    {
        $fixture = realpath(dirname(__FILE__).'/../data/fixture.xml');
        return $this->createXMLDataSet($fixture);
    }

    public function numberOfResultsDataProvider()
    {
        return array(
            array(1, null, null, 1),
            array(2, null, null, 3),
            array(3, null, null, 0),
            array('abc', null, null, 5),
            array(null, 1996, 2003, 3),
            array(null, 1996, 1999, 2),
            array(null, 2002, 2003, …
Run Code Online (Sandbox Code Playgroud)

php testing tdd phpunit redgreen

8
推荐指数
1
解决办法
357
查看次数

为什么使用PHPUnit比创建自己的测试脚本更好?

浏览文档,我看到PHPUnit只提供以下功能:

http://www.phpunit.de/manual/current/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.assertions

所有这些都可以非常容易地在自定义测试脚本中实现,在不到1K的行中...

PHPUnit有2 MB的文件(大约200个),其中包含大量的类.此外,PHPUnit只从命令行运行:(

不会创建自己的脚本是一个更好的主意吗?

php phpunit unit-testing

8
推荐指数
3
解决办法
676
查看次数

PHPUnit存根方法返回NULL?

我正在尝试使用PHPUnit的作者描述的这个方法来模拟一个单例,并且使用它的一个方法:

public function setUp() {
    $this->_foo = $this->getMockBuilder('Foo')
        ->disableOriginalConstructor()
        ->getMock();

    $this->_foo->expects($this->any())
        ->method('bar')
        ->will($this->returnValue('bar'));

    var_dump($this->_foo->bar());
}
Run Code Online (Sandbox Code Playgroud)

问题是NULL每次都会转储.据我了解,当你模拟一个对象时,所有的方法都会被返回的存根替换,NULL除非像我正在做的那样明确地存根.所以,既然我已经删除了该bar()方法,为什么它不会转储预期的'bar'字符串?我做错了什么?

php phpunit unit-testing

8
推荐指数
1
解决办法
2137
查看次数

在Symfony2中创建虚假用户以进行测试

我对使用PHP进行测试有些新意.我使用Cucumber,RSpec,Capybara和Factory Girl对Rails进行了相当多的测试,但我几乎没有用PHP进行任何测试.

我会用尽可能最普遍的方式问一个关于我当前挑战的问题,因为我已经走了几条特定的道路而且只是沮丧地遇到了.

我想编写一个用于签署用户的功能测试.创建测试用户对象的好方法是什么,以便尝试登录?

更一般地说,在Symfony2中创建测试对象的事实标准是什么?赛程?某种工厂?

在Ruby中,我会使用Factory Girl,因为它允许您以干净,干燥的方式处理任何对象的依赖项.在Phactory中似乎有一个与Apple相当的工厂女孩​​,但不幸的是,该工具似乎没有得到广泛使用而且不再维护.

php testing phpunit symfony

8
推荐指数
1
解决办法
1485
查看次数

PHPUnit:仅针对一个测试套件进行过滤

在PHPUnit中,可以在不同的测试套件中组织测试:

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2sandbox">
            <directory>./AlbumTest</directory>
        </testsuite>
    </testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

此外,您可以定义过滤器

<filter>
    <whitelist>
        <directory suffix=".php">/var/www/sandbox/zf2sandbox/module/Album/src/Album/</directory>
    </whitelist>
</filter>
Run Code Online (Sandbox Code Playgroud)

现在我想结合这两个胎儿.不允许将filter标记放入a testsuite(过滤器只是被忽略).

<phpunit bootstrap="Bootstrap.php">
    <testsuites>
        <testsuite name="zf2sandbox">
            <directory>./AlbumTest</directory>
            <filter>
                <whitelist>
                    <directory suffix=".php">/var/www/sandbox/zf2sandbox/module/Album/src/Album/</directory>
                </whitelist>
            </filter>
        </testsuite>
    </testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以为每个过滤器定义过滤器(whilists,blacklists等)testsuite

configuration phpunit filter test-suite

8
推荐指数
1
解决办法
1563
查看次数

解释PHP单元输出

我的PHP单元在控制台上输出.究竟是什么63/129 ( 48%)和一般意味着什么?它是否运行所有测试?

PHPUnit 3.7.22 by Sebastian Bergmann.

Configuration read from phpunit.xml

...............................................................  63 / 129 ( 48%)
............................................................... 126 / 129 ( 97%)
...

Time: 0 seconds, Memory: 6.75Mb

OK (129 tests, 245 assertions)
Run Code Online (Sandbox Code Playgroud)

phpunit.xml看起来像:

<?xml version="1.0" encoding="utf-8" ?>
<phpunit bootstrap="vendor/autoload.php">

    <testsuites>
        <testsuite name="SDK Testsuite">
            <directory suffix="Test.php">src/MyNamespace/Test/Unit</directory>
        </testsuite>
    </testsuites>

</phpunit>
Run Code Online (Sandbox Code Playgroud)

phpunit

8
推荐指数
2
解决办法
1596
查看次数

Laravel 4 phpunit test:测试标记为"risky",因为它没有关闭自己的输出缓冲区

我在尝试在phpunit测试中测试一个简单的Illuminate\Http\Response对象时遇到了问题.有问题的错误是:

PHPUnit 4.3-dev by Sebastian Bergmann.

Configuration read from /var/MyApp/phpunit.xml

....................R.................

Time: 2.71 seconds, Memory: 76.75Mb

OK, but incomplete, skipped, or risky tests!
Tests: 38, Assertions: 55, Risky: 1.     
Run Code Online (Sandbox Code Playgroud)

其中运行时,将R被示出phpunit --tap

# RISKY Test code or tested code did not (only) close its own output buffers
Run Code Online (Sandbox Code Playgroud)

我要测试的课程是:

class PrettyError {

    /**
     * Renders a pretty 500 error page view
     *
     * @return string with error view
     */
    public function render()
    {
             return Response::make(View::make('viewName')->with('param','value')->render(), 500, array());
    }
}
Run Code Online (Sandbox Code Playgroud)

而测试是 …

phpunit laravel laravel-4

8
推荐指数
3
解决办法
3928
查看次数

PHPUnit:尝试@cover或@use不是现有的方法

我正在学习如何使用PHPUnit 4.3.5/PHP 5.5.14进行单元测试.一切顺利,直到我试图获得我的代码覆盖率.我收到这个错误:"尝试@cover或@use不存在的方法"MyClass :: __构造"当试图获得代码覆盖时.我尝试了其他SO答案但无法解决它.任何想法我做错了什么? 谢谢!

/**
 * Test constructor.
 * @covers MyClass::__construct
 * @group MyClassTest
 */
public function test_Can_Create_MyClass_Instance() {
    $this->assertInstanceOf('MyClass', $this->_myClass);
}
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing

8
推荐指数
2
解决办法
4395
查看次数

PHPUnit测试file_get_contents

我有一个类,它有一个利用PHP的全局file_get_contents函数的方法.我需要在类上测试方法,而不是实际调用全局函数.

我知道我可以使用命名空间来覆盖返回的内容file_get_contents,但是我的测试已经在一个单独的命名空间中,所以我不能简单地将命名空间与类匹配.

这是一些代码:

班级

<?php
namespace MyVendor\MyProject;

class MyClass
{

private $someProperty;

public function __construct($override = '')
{
    $this->someProperty = $override;
}

public function myMethod()
{
    $request = 'http://example.com';
    $response = $this->someMethodUsingGlobals($request);
    // Do something with the response..
}
public function someMethodUsingGlobals($url)
{
    return json_decode(file_get_contents($url),true)['results'][0];
}

}
Run Code Online (Sandbox Code Playgroud)

考试

<?php
namespace MyProjectTests;

public function test_it_does_something_with_the_response()
{
    $sut = new MyClass();

    $response = $sut->myMethod();

    $this->assertEquals('Some expectation', $response);
}
Run Code Online (Sandbox Code Playgroud)

我需要someMethodUsingGlobals()在类上模拟该方法,但不完全确定如何去做.

testing phpunit unit-testing mocking

8
推荐指数
2
解决办法
5924
查看次数

使用reCaptcha测试表单提交的安全和标准方法是什么?

我有很多不同形式的观点,有些形式有reCaptcha保护,有些则没有.具有reCaptcha保护的表单是关键的(注册,登录,还原等),这就是为什么它们应该被测试.

我不想做的事:

  1. 添加任何debug,test这把验证码断标志(不安全?).
  2. 在每个单元测试和部署阶段评论或取消注释reCaptcha HTML-block.

使用reCaptcha测试表单的标准方法是什么?

phpunit unit-testing recaptcha symfony

8
推荐指数
1
解决办法
527
查看次数