小编yvo*_*yer的帖子

PHPunit:如何模拟具有参数和返回值的方法

使用PHPUnit,我想知道我们是否可以模拟一个对象来测试是否使用期望参数返回值调用方法?

doc中,有一些传递参数或返回值的示例,但不是两者都有 ...

我试过用这个:

// My object to test
$hoard = new Hoard();
// Mock objects used as parameters
$item = $this->getMock('Item');
$user = $this->getMock('User', array('removeItem'));
...
$user->expects($this->once())
     ->method('removeItem')
     ->with($this->equalTo($item));
$this->assertTrue($hoard->removeItemFromUser($item, $user));

我的断言失败,因为Hoard :: removeItemFromUser()应该返回User :: removeItem()的返回值,这是真的.

$user->expects($this->once())
     ->method('removeItem')
     ->with($this->equalTo($item), $this->returnValue(true));
$this->assertTrue($hoard->removeItemFromUser($item, $user));

也失败并显示以下消息:" 调用User :: removeItem(Mock_Item_767aa2db Object(...))的参数计数太低. "

$user->expects($this->once())
     ->method('removeItem')
     ->with($this->equalTo($item)) …

php phpunit unit-testing

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

Symfony2:如何测试配置节点的值和索引

在Symfony2中,使用此类设置,如何测试每个节点是否在Configuration类中定义,以及它们的值是否已正确配置.

要测试的课程

# My\Bundle\DependencyInjection\Configuration.php

class Configuration implements ConfigurationInterface
{
    /**
     * {@inheritDoc}
     */
    public function getConfigTreeBuilder()
    {
        $treeBuilder = new TreeBuilder();
        $treeBuilder->root('my_bundle')
            ->children()
                ->scalarNode("scalar")->defaultValue("defaultValue")->end()
                ->arrayNode("arrayNode")
                    ->children()
                        ->scalarNode("val1")->defaultValue("defaultValue1")->end()
                        ->scalarNode("val2")->defaultValue("defaultValue2")->end()
                    ->end()
                ->end()
            ->end()
        ;

        return $treeBuilder;
    }
}

以下是我想在单元测试中做的断言:

我试图以节点的形式访问节点,但它似乎不起作用.此外,TreeBuilder似乎没有给我们将配置作为数组获取的可能性,除非它们由bundle扩展加载.

测试

# My\Bundle\Tests\DependencyInjection\ConfigurationTest.php

$configuration = $this->getConfiguration();
$treeBuilder   = $configuration->getConfigTreeBuilder();

$this->assertInstanceOf("Symfony\Component\Config\Definition\Builder\TreeBuilder", $treeBuilder);

// How to access the treebuilder's nodes ? …

phpunit symfony symfony-2.1

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

Mysql:如何获取点后小数位数超过一定数量的每一行

我有一个包含浮点值的表。

+   id   |  value  |
+--------|---------|
+   1    | 19.22   |
+   2    | 32.333  |
+   3    | 1.2332  |
+   4    | 0.22334 |
+   5    | 4.55    |
Run Code Online (Sandbox Code Playgroud)

我想提取点后包含多于3个小数的每一行。

我期望的结果是:

+   id   |  value  |
+--------|---------|
+   2    | 32.333  |
+   3    | 1.2332  |
+   4    | 0.22334 |
Run Code Online (Sandbox Code Playgroud)

mysql sql

4
推荐指数
2
解决办法
5341
查看次数

标签 统计

phpunit ×2

mysql ×1

php ×1

sql ×1

symfony ×1

symfony-2.1 ×1

unit-testing ×1