helsingborg-stad / acfservice
Simplifies ACF integration by providing a centralized AcfService that exposes global ACF functions in a streamlined manner. Simplify your development workflow and enhance ACF integration with ease.
Installs: 6 091
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
Requires (Dev)
- helsingborg-stad/phpcs: ^0.3.5
- phpunit/phpunit: ^9.6
README
AcfService
Simplifies ACF integration by providing a centralized AcfService that exposes global ACF functions in a streamlined manner. Simplify your development workflow and enhance ACF integration with ease.
About AcfService
Enable the use of global ACF functions in plugins and themes where applying Interface Segregation. Different flavors of the ACF Service can be utilized by applying available decorators.
Getting Started
Installation
- Install the package via composer:
composer require helsingborg-stad/acfservice
- Use the AcfService in your plugin or theme:
use AcfService\Implementations\NativeAcfService; $acfService = new NativeAcfService(); $fields = $acfService->getFields(123);
Built With
- PHP
Tests
Run tests
Run composer test
in the terminal.
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Using AcfService in tests
To make it easier when testing code that depends on the AcfService or parts of it, a FakeAcfService implementation is available. This implementation is useful when you want to test your code without having to rely on the ACF functions.
Example
Consider that you have the following class that utilizes a part of the AcfService:
use AcfService\Contracts\GetFields; class MyService { public function __construct(private GetFields $acfService) { } public function getFields(): int { return $this->acfService->getFields(); } }
You can then use FakeAcfService in your tests to fake the results of the AcfService as well as verifying the calls made to the AcfService functions:
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); } }
Passing return values to the FakeAcfService
The FakeAcfService constructor accepts an array of key-value pairs where the key is the name of the method and the value is the return value of the method.
# 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']
License
Distributed under the MIT License.