helsingborg-stad / acfservice
通过提供集中的AcfService,以简化的方式公开全局ACF函数,简化了ACF集成。简化您的开发工作流程,轻松增强ACF集成。
0.8.1
2024-08-20 06:00 UTC
Requires
- php: ^8.1
Requires (Dev)
- helsingborg-stad/phpcs: ^0.3.5
- phpunit/phpunit: ^9.6
README
AcfService
通过提供集中的AcfService,以简化的方式公开全局ACF函数,简化了ACF集成。简化您的开发工作流程,轻松增强ACF集成。
关于AcfService
在应用接口隔离的插件和主题中启用全局ACF函数的使用。可以通过应用可用的装饰器利用ACF服务的不同版本。
入门
安装
- 通过Composer安装包
composer require helsingborg-stad/acfservice
- 在您的插件或主题中使用AcfService
use AcfService\Implementations\NativeAcfService; $acfService = new NativeAcfService(); $fields = $acfService->getFields(123);
构建工具
- PHP
测试
运行测试
在终端中运行 composer test
。
贡献
贡献使得开源社区成为一个如此美妙的学习、灵感和创造的地方。您所提供的任何贡献都备受赞赏。
- 分支项目
- 创建您的功能分支 (
git checkout -b feature/AmazingFeature
) - 提交您的更改 (
git commit -m 'Add some AmazingFeature'
) - 推送至分支 (
git push origin feature/AmazingFeature
) - 打开拉取请求
在测试中使用AcfService
为了使测试依赖于AcfService或其部分的代码更加容易,提供了一个FakeAcfService实现。这个实现在你想要测试代码而不依赖于ACF函数时非常有用。
示例
假设您有以下一个类,它使用AcfService的一部分
use AcfService\Contracts\GetFields; class MyService { public function __construct(private GetFields $acfService) { } public function getFields(): int { return $this->acfService->getFields(); } }
您可以在测试中使用FakeAcfService来模拟AcfService的结果以及验证对AcfService函数的调用
use AcfService\Implementations\FakeAcfService; use PHPUnit\Framework\TestCase; class MyServiceTest extends TestCase { public function testGetFields() { // Given $fakeAcfService = new FakeAcfService(['getFields' => ['fieldName' => 'fieldValue']]); $myService = new MyService($fakeAcfService); // When $fields = $myService->getFields(); // Then $this->assertEquals([], $acfService->methodCalls['getFields'][0]); $this->assertEquals(['fieldName' => 'fieldValue'], $fields); } }
传递返回值到FakeAcfService
FakeAcfService构造函数接受一个键值对数组,键是方法的名称,值是该方法的返回值。
# Using a generic return value for all calls to the method. $fakeAcfService = new FakeAcfService(['getFields' => ['field' => 'value']]); $fakeAcfService->getFields(); // Returns ['field' => 'value'] $fakeAcfService->getFields(321); // Returns ['field' => 'value'] $fakeAcfService->getFields(123); // Returns ['field' => 'value'] # Using a callback to determine the return value based on the arguments passed to the method. $return = fn($postId) => $postId === 123 ? ['field' => 'value'] : []; $fakeAcfService = new FakeAcfService(['getFields' => $return]); $fakeAcfService->getFields(); // Returns false $fakeAcfService->getFields(321); // Returns false $fakeAcfService->getFields(123); // Returns ['field' => 'value']
许可证
在MIT许可证下分发。