apsconnect / connect-quickstart-template
APS Connect 快速入门模板
Requires
- php: >=5.6.0
- apsconnect/connect-sdk: >=19.0
Requires (Dev)
- mockery/mockery: ^0.9
- phpunit/phpunit: ^5.7
This package is auto-updated.
Last update: 2024-09-29 05:02:11 UTC
README
PHP Connect SDK 模板为开发者提供了一个完整的骨架,用于使用 Connect Fulfillment API 和 Connect SDK for PHP 开始他们的自动化项目。
要求
为了使用此模板,您需要一个能够运行 PHP 脚本的环境,支持任何从 PHP 5.6 开始的版本。此外,请确保 composer 功能正常。
安装
您可以通过终端中的 Composer create-project
命令下载快速入门模板
composer create-project --prefer-dist apsconnect/connect-quickstart-template project-name
一旦准备好项目骨架,您就可以在 app/ProductFulfillment.php
文件中的 processRequest()
方法开始编写自定义代码。
<?php namespace App; /** * Class ProductFulfillment * @package App */ class ProductFulfillment extends \Connect\FulfillmentAutomation { /** * Process each pending request * @param \Connect\Request $request */ public function processRequest($request) { // TODO: Implement processRequest() method. } /** * Run the Product Fulfillment Request Processor * @return bool * @throws \GuzzleHttp\Exception\GuzzleException */ public function run() { try { /** * run the application in custom context, any error * handling customization should be done here */ $this->process(); return true; } catch (\Exception $e) { $this->logger->error($e->getMessage()); if (is_callable([$this->logger, 'dump'])) { $this->logger->dump(); } } return false; } }
测试您的项目
测试 ProductFulfillment
类非常简单,您只需在默认的 tests/Feature/
目录中创建您的测试,骨架已经有一些常见的测试。
模拟 Fulfillment API 调用
要模拟 fulfillment API,并测试如请求被检索的情况,您只需在 tests/Feature/
目录中创建一个有效的 json 文件,其中包含您想要的响应,文件名为 YOUR-TEST-NAME.http.json
,其中 YOUR-TEST-NAME
是您想使用该假响应的测试文件名,例如
ProcessSkipTest.http.json
将被注入到ProcessSkipTest.php
测试中。RunAndFailTest.http.json
将被注入到RunAndFailTest.php
测试中。
模拟其他服务
骨架还提供了一个简单的模拟其他服务的方法,为此您只需在 tests/Providers
中创建一个新的服务提供者,这个新的服务提供者必须扩展 MockServiceProvider
。在文件中,您应使用静态属性 $scope
配置您的模拟服务,以检索任何辅助文件,如假 json。
<?php namespace Test\Providers; use Pimple\Container; /** * Class HttpServiceProvider * @package Test\Unit\Runtime\Providers */ class HttpServiceProvider extends MockServiceProvider { public function register(Container $container) { $content = '[]'; $fakeRequestFile = dirname(__DIR__) . '/Feature/' . self::$scope . '.json'; if (is_readable($fakeRequestFile)) { $content = trim(file_get_contents($fakeRequestFile)); } $body = \Mockery::mock('\Psr\Http\Message\StreamInterface'); $body->shouldReceive('getContents') ->andReturn($content); $response = \Mockery::mock('\Psr\Http\Message\ResponseInterface'); $response->shouldReceive('getStatusCode') ->andReturn(200); $response->shouldReceive('getBody') ->andReturn($body); $client = \Mockery::mock('GuzzleHttp\ClientInterface'); $client->shouldReceive('request') ->withAnyArgs() ->andReturn($response); return $client; } }
最后,您需要在 tests/TestCase
文件的 $providers
属性中注册您的模拟服务。
/** * List of Mocked Service Providers * @var MockServiceProvider[] */ protected $providers = [ 'logger' => '\Test\Providers\LoggerServiceProvider', 'http' => '\Test\Providers\HttpServiceProvider' ];
作用域 ID 的格式为 YOUR-TEST-NAME.serviceId.json
,其中
YOUR-TEST-NAME
是正在运行的测试的文件名。serviceId
是服务的 ID,例如logger
是记录服务 ID,而http
是 Fulfillment API 的默认 Http 服务。