kambo/classopener 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kambo/classopener

Composer 安装命令:

composer require kambo/classopener

包简介

Mock/stub final classes and methods in PHPUnit.

README 文档

README

Build Status Scrutinizer Code Quality Code Coverage Software License Wouldn't it be great if you can mock/stub final classes and methods in PHPUnit? With this package you can do exactly that. Just extend your test case from ClassOpenerTestCase and add to the test method comment following annotation@disableFinal name\of\the\final\class After this you can continue with your regular mock/stub techniques.

But be aware, there are few gotchas as this is basically a hack packed in a nice package with ribbon.

Install

Prefered way to install library is with composer:

composer require kambo/classopener

Usage

Extend your test case from ClassOpenerTestCase and add to the test method comment following annotation@disableFinal name\of\the\final\class After this you can continue with your regular mock/stub techniques.

Example:

<?php

use Kambo\Testing\ClassOpener\PHPUnit\ClassOpenerTestCase;

class ClassOpenerTestCaseTest extends ClassOpenerTestCase
{
    /**
     * Disable final and mock the class
     *
     * @disableFinal Kambo\Tests\Testing\ClassOpener\Fixtures\Foo
     */
    public function testAnnotationMocking()
    {
        // You can use your traditional mocking/stubing techniques
        $fooMock = $this->getMockBuilder(Foo::class)
                        ->disableOriginalConstructor()
                        ->getMock();

        $fooMock->method('bar')->will($this->returnValue(false));

        $this->assertFalse($fooMock->bar());
    }
}

Gotchas

The "opening" of class must be done before the creation of any class instance. This premise can be easilly broken by the previously running tests. One of the possible solutions is to isolate the tests, which are using Class open with annotation @runInSeparateProcess

Example:

<?php

use Kambo\Testing\ClassOpener\PHPUnit\ClassOpenerTestCase;

class ClassOpenerTestCaseTest extends ClassOpenerTestCase
{
    /**
     * Disable final and mock the class
     *
     * @runInSeparateProcess
     * @disableFinal Kambo\Tests\Testing\ClassOpener\Fixtures\Foo
     */
    public function testAnnotationMocking()
    {

Credits

Based on the excelent article by the Mark Baker: Extending final Classes and Methods by manipulating the AST

License

The MIT License (MIT), https://opensource.org/licenses/MIT

统计信息

  • 总下载量: 10
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固