PHP 测试框架 Shmock

码农软件 · 软件分类 · 单元测试工具 · 2019-11-18 16:43:48

软件介绍

Shmock(SHorthand for MOCKing)是 PHPUnit 创建 mocks 的平稳替代,使用 EasyMock 的 mock/replay 概念,但是又使用 mocking 定义的闭包范围。

示例代码:

<?php
    namespace Foo;

    /**     * Here's a class we're trying to test yay.     */
    class Foo
    {
        private $foo = 0;
        private $incrementing_service = null;

        public function __construct(Incrementing_Service $incrementing_service)
        {
            $this->incrementing_service = $incrementing_service;
        }

        public function next_foo()
        {
            $this->foo = $this->incrementing_service->increment($this->foo);
            return $this->foo;
        }
    }

    /**     * Our test case runs the same test case twice - once with the original PHPUnit mocking     * syntax and a second time with Shmock syntax.     */
    class Foo_Test extends PHPUnit_Framework_TestCase
    {
                use \Shmock\Shmockers; // This enables the use of the Shmock helper methods (replicated below)

                public function test_phpunit_original_mocking_syntax()
        {
            // this is the original PHPUnit mock syntax

            $incrementing_service_mock = $this->getMock('\Foo\Incrementing_Service', array('increment'));
            $incrementing_service_mock->expects($this->once())
                ->method('increment')
                ->with($this->equalTo(0))
                ->will($this->returnValue(1));

            $foo = new Foo($incrementing_service_mock);
            $this->assertEquals(1, $foo->next_foo(0));
        }

        /**         * Create a shmock representation for $class_name and configure expected         * mock interaction with $conf_closure         * @return Shmock A fully configured mock object         * @note You do not need this protected method if you use the Shmockers trait, shown above         */
        protected function shmock($class_name, $conf_closure)
        {
            return \Shmock\Shmock::create_class($this, $class_name, $conf_closure);
        }

        public function test_shmock_syntax()
        {
            // here's shmock. Neat huh?
            $incrementing_service_mock = $this->shmock('\Foo\Incrementing_Service', function($shmock)
            {
                $shmock->increment(0)->return_value(1);
            });

            $foo = new Foo($incrementing_service_mock);
            $this->assertEquals(1, $foo->next_foo(0));
        }
    }


本文地址:https://codercto.com/soft/d/19271.html

计算机组成与设计硬件/软件接口

计算机组成与设计硬件/软件接口

[美] David A.Patterson、John L.Hennessy / 郑纬民 / 机械工业出版社 / 2007-4 / 75.00元

《计算机组成与设计硬件:软件接口》(原书第3版)是计算机组成的经典教材。全书着眼于当前计算机设计中最基本的概念,展示了软硬件间的关系,并全面介绍当代计算机系统发展的主流技术和最新成就。同以往版本一样,《计算机组成与设计硬件:软件接口》(原书第3版)采用MIPS处理器作为展示计算机硬件技术基本功能的核心。书中逐条指令地列举了完整的MIPS指令集,并介绍了网络和多处理器结构的基本内容。将CPU性能和程......一起来看看 《计算机组成与设计硬件/软件接口》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具