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.

0.8.1 2024-08-20 06:00 UTC

This package is auto-updated.

Last update: 2024-10-20 06:23:39 UTC


README

Contributors Forks Stargazers Issues License

Unit Tests PHP Versions

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.

Report Bug ยท Request Feature

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

  1. Install the package via composer:
composer require helsingborg-stad/acfservice
  1. 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.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. 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.