This commit is contained in:
yansongda 2019-09-09 14:57:22 +08:00
parent 162d7cab6f
commit 615ab0b847
9 changed files with 180 additions and 38 deletions

13
.github/workflows/Linter.yml vendored Normal file
View File

@ -0,0 +1,13 @@
name: Linter
on: [push, pull_request]
jobs:
php_cs_fixer:
name: PHP-CS-Fxier
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Install Dependencies
run: composer install --no-progress
- name: Run PHP-CS-Fxier
run: ./vendor/bin/php-cs-fixer fix --dry-run --diff 1>&2

21
.github/workflows/Tester.yml vendored Normal file
View File

@ -0,0 +1,21 @@
name: Tester
on: [push, pull_request]
jobs:
PHPUnit:
name: php-${{ matrix.php }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php:
- 7.1
- 7.2
container:
image: yansongda/php-fpm:${{ matrix.php }}
steps:
- uses: actions/checkout@master
- name: Install Dependencies
run: composer update --prefer-dist --no-interaction --no-suggest
- name: Run PHPUnit
run: ./vendor/bin/phpunit

View File

@ -1,6 +1,7 @@
<?php <?php
$finder = PhpCsFixer\Finder::create() $finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('tests') ->exclude('tests')
->exclude('vendor'); ->exclude('vendor');

View File

@ -0,0 +1,10 @@
<h1 align="center">Pay</h1>
<p align="center">
[![Linter Status](https://github.com/yansongda/pay/workflows/Linter/badge.svg)](https://github.com/yansongda/pay/actions)
[![Tester Status](https://github.com/yansongda/pay/workflows/Tester/badge.svg)](https://github.com/yansongda/pay/actions)
[![Latest Stable Version](https://poser.pugx.org/yansongda/pay/v/stable)](https://packagist.org/packages/yansongda/pay)
[![Total Downloads](https://poser.pugx.org/yansongda/pay/downloads)](https://packagist.org/packages/yansongda/pay)
[![Latest Unstable Version](https://poser.pugx.org/yansongda/pay/v/unstable)](https://packagist.org/packages/yansongda/pay)
[![License](https://poser.pugx.org/yansongda/pay/license)](https://packagist.org/packages/yansongda/pay)
</p>

View File

@ -5,12 +5,15 @@ namespace Yansongda\Pay;
use Pimple\Container; use Pimple\Container;
use Pimple\Exception\FrozenServiceException; use Pimple\Exception\FrozenServiceException;
use Pimple\Exception\UnknownIdentifierException; use Pimple\Exception\UnknownIdentifierException;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Yansongda\Pay\Contract\ServiceInterface; use Yansongda\Pay\Contract\ServiceInterface;
use Yansongda\Pay\Exception\GatewayServiceException; use Yansongda\Pay\Exception\GatewayServiceException;
use Yansongda\Pay\Exception\UnknownServiceException; use Yansongda\Pay\Exception\UnknownServiceException;
use Yansongda\Pay\Service\ConfigService; use Yansongda\Pay\Service\ConfigService;
use Yansongda\Pay\Service\EventService; use Yansongda\Pay\Service\EventService;
use Yansongda\Pay\Service\LoggerService; use Yansongda\Pay\Service\LoggerService;
use Yansongda\Supports\Config;
use Yansongda\Supports\Logger;
use Yansongda\Supports\Str; use Yansongda\Supports\Str;
/** /**
@ -20,6 +23,10 @@ use Yansongda\Supports\Str;
* @property \Yansongda\Supports\Logger log * @property \Yansongda\Supports\Logger log
* @property \Yansongda\Supports\Config config * @property \Yansongda\Supports\Config config
* @property \Symfony\Component\EventDispatcher\EventDispatcher event * @property \Symfony\Component\EventDispatcher\EventDispatcher event
* @method static Config config($config)
* @method static Logger logger($config)
* @method static Logger log($config)
* @method static EventDispatcher event($config)
*/ */
class Pay extends Container class Pay extends Container
{ {
@ -28,26 +35,32 @@ class Pay extends Container
* *
* @var array * @var array
*/ */
protected $config; protected $config = [];
/** /**
* service. * service.
* *
* @var array * @var string[]
*/ */
protected $service; protected $service = [];
/** /**
* baseConfig. * baseConfig.
* *
* @var array * @var array
*/ */
private $baseConfig; private $baseConfig = [
'http' => [
'timeout' => 5.0,
'connect_timeout' => 3.0,
],
'mode' => 'dev',
];
/** /**
* baseService. * baseService.
* *
* @var array * @var string[]
*/ */
private $baseService = [ private $baseService = [
ConfigService::class, ConfigService::class,
@ -111,9 +124,9 @@ class Pay extends Container
* @param $method * @param $method
* @param $params * @param $params
* *
* @return \Yansongda\Pay\Contract\ServiceInterface * @return mixed
*/ */
public static function __callStatic($method, $params): ServiceInterface public static function __callStatic($method, $params)
{ {
$app = new static(...$params); $app = new static(...$params);
@ -182,41 +195,21 @@ class Pay extends Container
* *
* @throws \Yansongda\Pay\Exception\GatewayServiceException * @throws \Yansongda\Pay\Exception\GatewayServiceException
* *
* @return ServiceInterface * @return mixed
*/ */
protected function create(string $method): ServiceInterface protected function create(string $method)
{ {
if (!isset($this[$method])) { if (isset($this[$method])) {
$service = __NAMESPACE__.'\\Service\\Gateway\\'.Str::studly($method).'Service'; return $this[$method];
if (class_exists($service)) {
self::make($service);
}
throw new GatewayServiceException("Gateway [{$method}] Not Exists");
} }
return $this[$method]; $service = __NAMESPACE__.'\\Service\\Gateway\\'.Str::studly($method).'Service';
}
/** if (class_exists($service)) {
* make. self::make($service);
*
* @author yansongda <me@yansongda.cn>
*
* @param string $service
*
* @throws \Yansongda\Pay\Exception\GatewayServiceException
*/
private function make(string $service): void
{
$gatewayService = new $service($this);
if ($gatewayService instanceof ServiceInterface) {
$this->registerService($gatewayService);
} }
throw new GatewayServiceException("Gateway [{$service}] Must Be An Instance Of ServiceInterface"); throw new GatewayServiceException("Gateway [{$method}] Not Exists");
} }
/** /**
@ -238,4 +231,24 @@ class Pay extends Container
parent::register(new $service()); parent::register(new $service());
} }
} }
/**
* make.
*
* @author yansongda <me@yansongda.cn>
*
* @param string $service
*
* @throws \Yansongda\Pay\Exception\GatewayServiceException
*/
private function make(string $service): void
{
$gatewayService = new $service($this);
if ($gatewayService instanceof ServiceInterface) {
$this->registerService($gatewayService);
}
throw new GatewayServiceException("Gateway [{$service}] Must Be An Instance Of ServiceInterface");
}
} }

View File

@ -5,6 +5,7 @@ namespace Yansongda\Pay\Service;
use Pimple\Container; use Pimple\Container;
use Yansongda\Pay\Contract\ServiceInterface; use Yansongda\Pay\Contract\ServiceInterface;
use Yansongda\Supports\Collection; use Yansongda\Supports\Collection;
use Yansongda\Supports\Config;
class ConfigService implements ServiceInterface class ConfigService implements ServiceInterface
{ {
@ -20,7 +21,7 @@ class ConfigService implements ServiceInterface
{ {
$pimple['config'] = function ($container) { $pimple['config'] = function ($container) {
/* @var \Yansongda\Pay\Pay $container */ /* @var \Yansongda\Pay\Pay $container */
return new Collection($container->getConfig()); return new Config($container->getConfig());
}; };
} }
} }

View File

@ -33,7 +33,7 @@ class LoggerService implements ServiceInterface
$logger->setConfig($config); $logger->setConfig($config);
return $logger->getLogger(); return $logger;
}; };
} }
} }

77
tests/PayTest.php Normal file
View File

@ -0,0 +1,77 @@
<?php
namespace Yansongda\Pay\Tests;
use Pimple\Container;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Yansongda\Pay\Exception\UnknownServiceException;
use Yansongda\Pay\Pay;
use Yansongda\Supports\Config;
use Yansongda\Supports\Logger;
class PayTest extends TestCase
{
public function testBootstrap()
{
$pay = new Pay([]);
$this->assertInstanceOf(Pay::class, $pay);
$this->assertInstanceOf(Container::class, $pay);
}
public function testMagicGetAndGet()
{
$config = [];
$pay = new Pay($config);
$this->assertInstanceOf(Config::class, $pay->config);
$this->assertEquals(new Config($config), $pay->config);
$this->assertEquals(new Config($config), $pay->get('config'));
$this->assertInstanceOf(Logger::class, $pay->logger);
$this->assertInstanceOf(Logger::class, $pay->log);
$this->assertInstanceOf(EventDispatcher::class, $pay->event);
$this->expectException(UnknownServiceException::class);
$pay->get('foo');
}
public function testMagicSetAndSet()
{
$pay = new Pay([]);
$pay->name = 'yansongda';
$pay->set('age', '26');
$this->assertEquals('yansongda', $pay->name);
$this->assertEquals('yansongda', $pay->get('name'));
$this->assertEquals('26', $pay->age);
$this->assertEquals('26', $pay->get('age'));
}
public function testStaticCall()
{
$config = [];
$this->assertInstanceOf(Config::class, Pay::config($config));
$this->assertInstanceOf(Logger::class, Pay::logger($config));
$this->assertInstanceOf(Logger::class, Pay::log($config));
$this->assertInstanceOf(EventDispatcher::class, Pay::event($config));
}
public function testGetConfig()
{
$config = ['name' => 'yansongda'];
$pay = new Pay($config);
$this->assertArrayHasKey('name', $pay->getConfig());
$this->assertEquals(array_merge([
'http' => [
'timeout' => 5.0,
'connect_timeout' => 3.0,
],
'mode' => 'dev'
], $config), $pay->getConfig());
}
}

View File

@ -4,5 +4,11 @@ namespace Yansongda\Pay\Tests;
class TestCase extends \PHPUnit\Framework\TestCase class TestCase extends \PHPUnit\Framework\TestCase
{ {
public function setUp()
{
}
public function tearDown()
{
}
} }