neclimdul/openapi-php-helper

Runtime helper for OpenAPI Generator PHP templates.

Maintainers

Package info

gitlab.com/neclimdul/openapi-php-helper

Issues

pkg:composer/neclimdul/openapi-php-helper

Statistics

Installs: 8 395

Dependents: 8

Suggesters: 0

Stars: 0


README

PHP OpenAPI helpers.

This project aims to consolidate shared logic for PHP OpenAPI projects. This provides a more complete solution with less shared boilerplate.

Goals

This project shares its goals with its sibling template project it is designed to support.

  • Faster development
  • Bug fixes
  • Additional spec compatibility
  • Testing

Usage

Generally this package will be used with the templates it is designed to support and not on its own. If you need to add it to your project you can do so via composer.

composer require neclimdul/openapi-php-helper

Neclimdul\OpenapiPhp\Helper\Error

This provides a Class to help in handling errors. It has a helper that will allow you to generalize logging errors from responses to a PSR logging interface.

Neclimdul\OpenapiPhp\Helper\Models

This namespace contains tools for building data objects to represent OpenAPI Models. Its generally optimized to do the heavy lifting so a large templated library's Models will be mostly declarative and documentation.

Neclimdul\OpenapiPhp\Helper\Response

This namespace contains various classes to help manage responses. The ApiResponse class is the most visible, wrapping up tasks that a developer would need to accomplish with the response from a OpenAPI request.

Neclimdul\OpenapiPhp\Helper\Serialization

This provides classes for serialization/deserialization as well as decoding and encoding. The line is a bit blurred based on legacy implementation but this should generally be transparent to a developer if your API uses JSON.

Neclimdul\OpenapiPhp\Helper\Testing

This provides tools for testing libraries built on top of this code. See below for more information on testing.

Testing

This library comes with some tools to test the libraries built on top of it using Guzzle's mocking framework.

https://docs.guzzlephp.org/en/stable/testing.html

Including the GuzzleMockingTrait in your PHPUnit test allows you to use a couple methods to build your test. For example:

<?php

declare(strict_types=1);

namespace App\Tests

use App\MarketoService;
use NecLimDul\MarketoRest\Lead\Api\LeadsApi;
use NecLimDul\MarketoRest\Lead\Model\Lead;
use NecLimDul\MarketoRest\Lead\Model\ResponseOfPushLeadToMarketo;
use Neclimdul\OpenapiPhp\Helper\Testing\GuzzleMockingTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;


#[CoversClass(MarketoService::class)]
class MarketoServiceTest extends TestCase {

  use GuzzleMockingTrait;

  public function testWebhook(): void {
    $sut = new MarketoService(LeadsApi::create($this->createMockClient()));

    $this->mockGuzzleResponse(body: new ResponseOfPushLeadToMarketo([
      'request_id' => 'foobar',
      'success' => TRUE,
      'errors' => [],
      'warnings' => [],
      'result' => [
        new Lead(['status' => 'updated']),
      ],
    ]));
    $sut->syncLead('test@example.com');
    $history = $this->assertGuzzleApiRequest('POST', '/rest/v1/leads/push.json');
    $request = $history['request'];
    $this->assertJsonStringEqualsJsonString(
      json_encode([
        'lookupField' => 'email',
        'programName' => 'My-Integration',
        'reason' => 'High priority updates',
        'input' => [
          [
            'email' => 'test@example.com',
            'myCustomField' => '1732cde5-ec82-4491-ac62-5e0f61519eea'
          ],
        ],
      ]),
      (string) $request->getBody(),
    );
  }

}

License

This library is derived from the OpenAPI and Swagger projects and is released under the Apache 2.0.