小编edo*_*ian的帖子

使用PHPUnit模拟对象期望部分数组

在PHPUnit mock with()子句中测试多个数组键的最佳方法是什么?

例如,要测试方法是否调用第二个参数是包含'foo'键的数组:

$this->stubDispatcher->expects($this->once())
        ->method('send')
        ->with('className', $this->arrayHasKey('foo'));
Run Code Online (Sandbox Code Playgroud)

我想要做的是像$this->arrayHasKey('foo', 'bar')实际上没有匹配数组的确切内容.

php phpunit

9
推荐指数
2
解决办法
4832
查看次数

PHPUnit - 如何模拟PDO预处理语句

我正在尝试使用PHPUnit对mapper类进行单元测试.我可以轻松地模拟将在mapper类中注入的PDO实例,但我无法弄清楚如何模拟PreparedStatement类,因为它是由PDO类生成的.

在我的情况下,我已经扩展了PDO类,所以我有这个:

public function __construct($dsn, $user, $pass, $driverOptions)
{

    //...

    parent::__construct($dsn, $user, $pass, $driverOptions);
    $this->setAttribute(PDO::ATTR_STATEMENT_CLASS,
        array('Core_Db_Driver_PDOStatement', array($this)));
}
Run Code Online (Sandbox Code Playgroud)

关键是Core_Db_Driver_PDOStatement没有注入PDO类的构造函数中,它是静态实例化的.即使我这样做:

public function __construct($dsn, $user, $pass, $driverOptions, $stmtClass = 'Core_Db_Driver_PDOStatement')
{

    //...

    parent::__construct($dsn, $user, $pass, $driverOptions);
    $this->setAttribute(PDO::ATTR_STATEMENT_CLASS,
        array($stmtClass, array($this)));
}
Run Code Online (Sandbox Code Playgroud)

...它仍然是一个静态的实例,因为我无法传递我自己的预处理语句类的模拟实例.

任何的想法 ?

编辑:解决方案,改编自anwser:

/**
 * @codeCoverageIgnore
 */
private function getDbStub($result)
{
    $STMTstub = $this->getMock('PDOStatement');
    $STMTstub->expects($this->any())
            ->method('fetchAll')
            ->will($this->returnValue($result));


    $PDOstub = $this->getMock('mockPDO');
    $PDOstub->expects($this->any())
            ->method('prepare')
            ->will($this->returnValue($STMTstub));

    return $PDOstub;
}

public function testGetFooById()
{
    $arrResult = array( ... );
    $PDOstub = $this->getDbStub($arrResult);
}
Run Code Online (Sandbox Code Playgroud)

php phpunit pdo mocking prepared-statement

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

phpunit 3.7:@assert注释发生了什么?

我有phpunit 3.7

官方声明中提到了一些新的注解(和重新引入一个老之前已停用的),但它没有提及拆除@assert.在3.7changelog中.,@assert不在页面上找到

当我使用代码片段在类上运行我的代码时

<?php
class MyMathClass
{
   /**
    * Add two given values together and return sum
    * @assert (1,2) == 3
    */
   public function addValues($a,$b)
   {
       return $a+$b;
   }
}
Run Code Online (Sandbox Code Playgroud)

输出是

PHPUnit 3.7.1 by Sebastian Bergmann.



Time: 1 second, Memory: 4.25Mb

No tests executed!
Run Code Online (Sandbox Code Playgroud)

用php 3.6.2

 phpunit MyMathClass.php
PHPUnit 3.6.12 by …
Run Code Online (Sandbox Code Playgroud)

php phpunit

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

PHPUnit代码覆盖率和例外

我怀疑PHPUnit显示单行测试没有涵盖1行代码,因为抛出的异常(但我抓到了)

我有单元测试应该涵盖那一行

/**
 * @expectedException Doctrine\ORM\NoResultException
 */
public function testCannotLoginInvalidUser() {

  $user = User::login($this->em, 'nonExistant', 'password');
  $this->assertNull($user);

}
Run Code Online (Sandbox Code Playgroud)

为什么我的代码覆盖率仍然反映不包括在内?

我做了一个测试...添加echo b4返回null ...我发现那行真的没有被覆盖......

try {
  $user = $query->getSingleResult();
} catch (Exception $e) {
  echo 'caught exception';  <-- this does not get executed. 
  return null;
}
Run Code Online (Sandbox Code Playgroud)

一旦抛出异常,PHPUnit是否会跳过所有执行?

更新:我觉得我使用@expectedException错了......

php phpunit exception

7
推荐指数
1
解决办法
1598
查看次数

Eclipse PDT和自定义PHPDoc注释

有没有办法为Eclipse PDT添加自定义phpdoc注释?例如,我想@depends在自动完成列表中看到(对于PHPUnit)注释,但现在我只能看到标准注释(例如@deprecated).

提前致谢.

php eclipse phpunit eclipse-pdt phpdoc

7
推荐指数
1
解决办法
3135
查看次数

在phpunit中连接约束

我的问题是,我如何连接phpunit的clausule中的约束?在虚拟示例中:

$test->expects ($this->once())
     ->method ('increaseValue')
     ->with ($this->greaterThan (0)
     ->will ($this->returnValue (null));
Run Code Online (Sandbox Code Playgroud)

方法increaseValue的参数必须大于0,但如果我需要评估此参数必须小于10.

我如何连接$this->lessThan(10)

php phpunit

7
推荐指数
1
解决办法
1586
查看次数

PHPUnit ReflectionException方法套件不存在

我正在使用PHPUnit版本3.6.2,并且总是得到

PHP ReflectionException:第113行的/pathTo/pear/PHPUnit/Runner/BaseTestRunner.php中不存在方法套件

运行单一测试时:

phpunit path/to/my/ClassToTest.php
Run Code Online (Sandbox Code Playgroud)

使用pear安装PHPUnit,我使用的是php 5.3.6

我应该修复任何PHP配置吗?或者这只是PHPUnit应该修复的东西.

班级

<?php

class ClassToTest extends PHPUnit_Framework_TestCase{

    public function testSomething(){
        $this->assertTrue(true);
    }

}
Run Code Online (Sandbox Code Playgroud)

php testing phpunit

7
推荐指数
2
解决办法
2586
查看次数

Maven JAXB插件只执行一次执行

我试图从两个XSD模式生成源.我的JAXBmaven插件看起来像这样:

<plugin>
    <groupId>com.sun.tools.xjc.maven2</groupId>
    <artifactId>maven-jaxb-plugin</artifactId>
    <version>1.1.1</version>
    <executions>
        <execution>
            <id>GenerateKenexa</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <includeBindings>
                    <includeBinding>**/jaxb-bindings-kenexa.xml</includeBinding>
                </includeBindings>
                <includeSchemas>
                    <includeSchema>**/KenexaXMLConfiguration.xsd</includeSchema>
                </includeSchemas>
            </configuration>
        </execution>
        <execution>
            <id>GenerateTalentQ</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <includeBindings>
                    <includeBinding>**/jaxb-bindings-talentq.xml</includeBinding>
                </includeBindings>
                <includeSchemas>
                    <includeSchema>**/TalentQXMLConfiguration.xsd</includeSchema>
                </includeSchemas>
            </configuration>
        </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)

第一个生成正常.但第二个没有.我在maven输出中看到:

[INFO] --- maven-jaxb-plugin:1.1.1:generate (GenerateKenexa) @ online.tests.management ---
[INFO] Compiling file:/D:/Projects/GTIWebApplications/gti_online_tests_management/src/main/resources/xsd/KenexaXMLConfiguration.xsd
[INFO] Writing output to D:\Projects\GTIWebApplications\gti_online_tests_management\target\generated-sources\xjc
[INFO] 
[INFO] --- maven-jaxb-plugin:1.1.1:generate (GenerateTalentQ) @ online.tests.management ---
[INFO] files are up to date
Run Code Online (Sandbox Code Playgroud)

它说文件是最新的,但它们甚至都没有生成.可能有什么问题?

java jaxb maven-3 maven maven-jaxb2-plugin

7
推荐指数
2
解决办法
1万
查看次数

如何模拟对象工厂

我使用Factories(请参阅http://www.php.net/manual/en/language.oop5.patterns.php获取模式)来增加代码的可测试性.一个简单的工厂看起来像这样:

class Factory
{
    public function getInstanceFor($type)
    {
        switch ($type) {
            case 'foo':
                return new Foo();
            case 'bar':
                return new Bar();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是使用该工厂的示例类:

class Sample
{
    protected $_factory;

    public function __construct(Factory $factory)
    {
        $this->_factory = $factory;
    }

    public function doSomething()
    {
        $foo = $this->_factory->getInstanceFor('foo');
        $bar = $this->_factory->getInstanceFor('bar');
        /* more stuff done here */
        /* ... */
    }
}
Run Code Online (Sandbox Code Playgroud)

现在进行适当的单元测试,我需要模拟将返回类的存根的对象,这就是我遇到的问题.我认为有可能这样做:

class SampleTest extends PHPUnit_Framework_TestCase
{
    public function testAClassUsingObjectFactory()
    {
        $fooStub = $this->getMock('Foo');
        $barStub = $this->getMock('Bar');

        $factoryMock = …
Run Code Online (Sandbox Code Playgroud)

php phpunit factory-pattern

6
推荐指数
1
解决办法
4532
查看次数

如何在php命令行中模拟文件上传

我是PHPUnit测试框架的新手.

我们知道,move_uploaded_file()在通过http POST方法上传文件之前,PHP的功能不起作用

所以,问题是如何在PHP命令行中模拟这个

注意:使用selenium我们可以模拟webform ..但我需要另一种选择.

php phpunit

6
推荐指数
1
解决办法
7501
查看次数