enzyme/parrot 问题修复 & 功能扩展

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

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

enzyme/parrot

Composer 安装命令:

composer require enzyme/parrot

包简介

Mockable native static functions for PHP.

README 文档

README

Build Status

Mockable native static functions for PHP.

What is it?

Do you have a class method that calls a PHP native/built-in function like file_get_contents, which you then test by having to create a dummy file for it to ingest? Well, say hello to Parrot. Now you can inject the built-in File functions, along with many others as a dependency which you can mock out during tests.

Installation

composer require enzyme/parrot

Example

Let's take a look at what used to happen:

Using the built in method.

class Foo
{
    public function openConfig($file)
    {
        $contents = file_get_contents($file);
        return $contents;
    }
}

Testing...

public function FooTest
{
    $file = __DIR__ . '/actual_file.txt';
    $expected = 'Contents of actual_file.txt';

    $foo = new Foo;
    $actual = $foo->openConfig($file);

    $this->assertEquals($actual, $expected);
}

The problem with the above is, actual_file.txt needs to reside in your test folder, get shipped with your tests, have the correct permission etc... it's a hassle.

Now using Parrot.

use Enzyme\Parrot\File;

class Foo
{
    protected $fileDispatch;

    public function __construct(File $fileDispatch)
    {
        $this->fileDispatch = $fileDispatch;
    }

    public function openConfig($file)
    {
        $contents = $this->fileDispatch->getContents($file);
        return $contents;
    }
}

Testing...

public function FooTest
{
    $file = __DIR__ . '/fake_file.txt';
    $expected = 'Contents of fake_file.txt';

    $fileDispatch = m::mock('Enzyme\Parrot\File[getContents]', function ($mock) use ($expected, $file) {
        $mock->shouldReceive('getContents')->withArgs([$file, []])->times(1)->andReturn($expected);
    });


    $foo = new Foo($fileDispatch);
    $actual = $foo->openConfig($file);

    $this->assertEquals($actual, $expected);
}

Now we just fake the file and it's contents, sweet!!!

Sugar

You may have noticed in the above example that the Parrot version of file_get_contents was simply getContents().

With all the Parrot'd wrappers around PHP's functions, we leave off the prefix. Who wants to repeat themselves?

The function name rules.

  1. If the original function name starts with a prefix like file_, f, curl_, imap_, mysql_ etc, you simply leave them off when using the equivalent class wrapper.

  2. If the original name has an underscore in it, replace it with camelCase. So for example file_get_contents becomes getContents and mysql_affected_rows becomes affectedRows.

  3. If the original name has a string of words, they are converted to camelCase too. For example imap_createmailbox becomes createMailbox.

  4. If the original function, following the rules above now starts with a number, replace then number with it's spelt equivalent. For example imap_8bit becomes eightBit.

Wrappers

The follow wrappers are currently available:

Name Prefixes removed
Curl curl_
File file, file_, f
Ftp ftp_
Gz gz
Image image
Imap imap_
MySql mysql_
PostgreSql pg_
Sqlite sqlite_
Glob glob (Simply becomes execute)

Missing something?

If there's a particular wrapper you're looking for and it isn't listed above, open a new issue, or simply extend Parrot following the rules in the existing wrappers and send a PR. Please check out CONTRIBUTING.md as well.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-12-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固