letsdrink/ouzo-goodies

从 Ouzo 框架中提取的实用类、测试断言和模拟框架。

1.8.0 2021-03-08 13:23 UTC

This package is auto-updated.

Last update: 2024-08-28 23:29:49 UTC


README

这是什么

Ouzo 框架 中提取的实用类、测试断言和模拟框架。我们与 PHP 7.2 及以上版本兼容。

Build Status Coverage Status Latest Stable Version Total Downloads License

如何使用

一些示例。

流畅数组:

$result = FluentArray::from($users)
             ->map(Functions::extractField('name'))
             ->filter(Functions::notEmpty())
             ->unique()
             ->toArray();

流畅迭代器:

$result = FluentIterator::fromArray([1, 2, 3])
             ->cycle()
             ->limit(10)
             ->reindex()
             ->toArray(); // [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]

流畅函数:

$product = new Product(['name' => 'super phone']);

$function = FluentFunctions::extractField('name')
      ->removePrefix('super')
      ->prepend(' extra')
      ->append('! ')
      ->surroundWith("***");

$result = Functions::call($function, $product); //=> '*** extra phone! ***'
$phones = Arrays::filter($products, FluentFunctions::extractField('type')->equals('PHONE'));

提取(从函数中):

$cities = Arrays::map($users, Functions::extract()->getAddress('home')->city);

时钟:

$string = Clock::now()
    ->plusYears(1)
    ->plusMonths(2)
    ->minusDays(3)
    ->format();

比较器:

$product1 = new Product(['name' => 'b']);
$product2 = new Product(['name' => 'c']);
$product3 = new Product(['name' => 'a']);

$result = Arrays::sort([$product1, $product2, $product3], Comparator::compareBy('name'));

数组流畅断言:

$animals = ['cat', 'dog', 'pig'];
Assert::thatArray($animals)->hasSize(3)->contains('cat');

字符串流畅断言:

Assert::thatString("Frodo")
     ->startsWith("Fro")
     ->endsWith("do")
     ->contains("rod")
     ->doesNotContain("fro")
     ->hasSize(5);

模拟:

$mock = Mock::create();
Mock::when($mock)->someMethod('arg')->thenReturn('123');

$result = $mock->someMethod('arg');

$this->assertEquals('123', $result);
Mock::verify($mock)->method('arg');

异常断言:

$foo = new Foo();

CatchException::when($foo)->method();

CatchException::assertThat()->isInstanceOf("FooException");

这只是 Ouzo 的一部分。查看文档以获取更多信息。

在哪里获取

从 github 下载或简单添加 composer 依赖项

composer require letsdrink/ouzo-goodies

packagist 上的 Ouzo Goodies.

文档

教程

实用工具

  • 数组 - 数组的辅助函数。
  • FluentArray - 以链式方式操作数组的接口。
  • 迭代器 - 迭代器的辅助函数。
  • FluentIterator- 以链式方式操作迭代器的接口。
  • 字符串 - 字符串的辅助函数。
  • 对象- 可以对任何 PHP 对象进行操作的辅助函数。
  • 函数 - 返回闭包的静态实用方法,可用于 Arrays 和 FluentArray,或其他 PHP 函数。
  • FluentFunctions - 用于函数组合的流畅实用工具。
  • 缓存 - 通用缓存。
  • 路径 - 路径操作的辅助函数。
  • 时钟 - DateTime 替代品。
  • 比较器 - 排序。

测试

查看完整文档:http://ouzo.readthedocs.org

PhpStorm 插件

有关想法、问题、讨论,请发送电子邮件至 ouzo-framework@googlegroups.com

支持 PHP 5.6、7.0 和 7.1

Ouzo 自 Ouzo 2.x 版本以来已停止支持 7.2 以下的 PHP 版本。如果您想使用 PHP 5.6、7.0 或 7.1 使用 Ouzo,请尝试 Ouzo 1.x 分支。