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://www.codercto.com/soft/d/19271.html

30天自制操作系统

30天自制操作系统

[日] 川合秀实 / 周自恒、李黎明、曾祥江、张文旭 / 人民邮电出版社 / 2012-8 / 99.00元

自己编写一个操作系统,是许多程序员的梦想。也许有人曾经挑战过,但因为太难而放弃了。其实你错了,你的失败并不是因为编写操作系统太难,而是因为没有人告诉你那其实是一件很简单的事。那么,你想不想再挑战一次呢? 这是一本兼具趣味性、实用性与学习性的书籍。作者从计算机的构造、汇编语言、C语言开始解说,让你在实践中掌握算法。在这本书的指导下,从零编写所有代码,30天后就可以制作出一个具有窗口系统的32位......一起来看看 《30天自制操作系统》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

SHA 加密
SHA 加密

SHA 加密工具