2019-09-09 14:57:22 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Yansongda\Pay\Tests;
|
|
|
|
|
|
2021-06-06 21:39:26 +08:00
|
|
|
|
use DI\Container;
|
2022-03-05 21:59:11 +08:00
|
|
|
|
use DI\ContainerBuilder;
|
2021-06-06 21:39:26 +08:00
|
|
|
|
use GuzzleHttp\Client;
|
2023-06-10 16:18:47 +08:00
|
|
|
|
use Monolog\Logger;
|
2021-06-06 21:39:26 +08:00
|
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
|
|
|
|
use Yansongda\Pay\Contract\ConfigInterface;
|
|
|
|
|
use Yansongda\Pay\Contract\EventDispatcherInterface;
|
|
|
|
|
use Yansongda\Pay\Contract\HttpClientInterface;
|
|
|
|
|
use Yansongda\Pay\Contract\LoggerInterface;
|
|
|
|
|
use Yansongda\Pay\Exception\ContainerException;
|
2022-03-05 21:59:11 +08:00
|
|
|
|
use Yansongda\Pay\Exception\Exception;
|
2021-06-06 21:39:26 +08:00
|
|
|
|
use Yansongda\Pay\Exception\ServiceNotFoundException;
|
|
|
|
|
use Yansongda\Pay\Pay;
|
2022-03-05 21:59:11 +08:00
|
|
|
|
use Yansongda\Pay\Provider\Alipay;
|
2021-06-07 21:15:52 +08:00
|
|
|
|
use Yansongda\Pay\Tests\Stubs\FooServiceProviderStub;
|
2021-06-06 21:39:26 +08:00
|
|
|
|
use Yansongda\Supports\Config;
|
2021-06-06 22:15:10 +08:00
|
|
|
|
use Yansongda\Supports\Pipeline;
|
2021-06-02 10:25:51 +08:00
|
|
|
|
|
2019-09-09 14:57:22 +08:00
|
|
|
|
class PayTest extends TestCase
|
|
|
|
|
{
|
2021-06-29 19:20:28 +08:00
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
Pay::clear();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-29 19:07:49 +08:00
|
|
|
|
protected function tearDown(): void
|
2021-06-06 21:39:26 +08:00
|
|
|
|
{
|
|
|
|
|
Pay::clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testConfig()
|
|
|
|
|
{
|
|
|
|
|
$result = Pay::config(['name' => 'yansongda']);
|
2022-03-05 21:59:11 +08:00
|
|
|
|
self::assertTrue($result);
|
2021-06-29 19:10:44 +08:00
|
|
|
|
self::assertEquals('yansongda', Pay::get(ConfigInterface::class)->get('name'));
|
|
|
|
|
|
2021-06-29 19:19:05 +08:00
|
|
|
|
// force
|
|
|
|
|
$result1 = Pay::config(['name' => 'yansongda1', '_force' => true]);
|
2022-03-05 21:59:11 +08:00
|
|
|
|
self::assertTrue($result1);
|
2021-06-29 19:10:44 +08:00
|
|
|
|
self::assertEquals('yansongda1', Pay::get(ConfigInterface::class)->get('name'));
|
2022-03-05 21:59:11 +08:00
|
|
|
|
|
2022-03-18 19:18:27 +08:00
|
|
|
|
// 直接使用 config 去设置 container
|
2022-03-05 21:59:11 +08:00
|
|
|
|
if (class_exists(Container::class)) {
|
|
|
|
|
// container - closure
|
|
|
|
|
Pay::clear();
|
|
|
|
|
$container2 = (new ContainerBuilder())->build();
|
|
|
|
|
$result2 = Pay::config(['name' => 'yansongda2'], function () use ($container2) {
|
|
|
|
|
return $container2;
|
|
|
|
|
});
|
|
|
|
|
self::assertTrue($result2);
|
|
|
|
|
self::assertSame($container2, Pay::getContainer());
|
|
|
|
|
|
|
|
|
|
// container - object
|
|
|
|
|
Pay::clear();
|
|
|
|
|
$container3 = (new ContainerBuilder())->build();
|
|
|
|
|
$result3 = Pay::config(['name' => 'yansongda2'], $container3);
|
|
|
|
|
self::assertTrue($result3);
|
|
|
|
|
self::assertSame($container3, Pay::getContainer());
|
2022-03-18 19:18:27 +08:00
|
|
|
|
|
|
|
|
|
// container - object force
|
|
|
|
|
Pay::clear();
|
|
|
|
|
$container4 = (new ContainerBuilder())->build();
|
|
|
|
|
Pay::setContainer($container4);
|
|
|
|
|
$result4 = Pay::config(['name' => 'yansongda2', '_force' => true]);
|
|
|
|
|
self::assertTrue($result4);
|
|
|
|
|
self::assertSame($container4, Pay::getContainer());
|
2022-03-05 21:59:11 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testDirectCallStatic()
|
|
|
|
|
{
|
|
|
|
|
$pay = Pay::alipay([]);
|
|
|
|
|
self::assertInstanceOf(Alipay::class, $pay);
|
|
|
|
|
|
|
|
|
|
if (class_exists(Container::class)) {
|
|
|
|
|
Pay::clear();
|
|
|
|
|
$container3 = (new ContainerBuilder())->build();
|
|
|
|
|
$pay = Pay::alipay([], $container3);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Alipay::class, $pay);
|
|
|
|
|
}
|
2021-06-06 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSetAndGet()
|
|
|
|
|
{
|
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
|
|
|
|
|
|
|
|
|
Pay::set('age', 28);
|
|
|
|
|
|
|
|
|
|
self::assertEquals(28, Pay::get('age'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testHas()
|
|
|
|
|
{
|
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
|
|
|
|
|
|
|
|
|
Pay::set('age', 28);
|
|
|
|
|
|
|
|
|
|
self::assertFalse(Pay::has('name'));
|
|
|
|
|
self::assertTrue(Pay::has('age'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetContainerAndClear()
|
|
|
|
|
{
|
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
|
|
|
|
self::assertInstanceOf(ContainerInterface::class, Pay::getContainer());
|
|
|
|
|
|
|
|
|
|
Pay::clear();
|
|
|
|
|
|
|
|
|
|
$this->expectException(ContainerException::class);
|
2022-03-05 21:59:11 +08:00
|
|
|
|
$this->expectExceptionCode(Exception::CONTAINER_NOT_FOUND);
|
|
|
|
|
$this->expectExceptionMessage('`getContainer()` failed! Maybe you should `setContainer()` first');
|
2021-06-06 21:39:26 +08:00
|
|
|
|
|
|
|
|
|
Pay::getContainer();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 22:15:10 +08:00
|
|
|
|
public function testMakeService()
|
|
|
|
|
{
|
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
|
|
|
|
self::assertNotSame(Pay::make(Pipeline::class), Pay::make(Pipeline::class));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 21:39:26 +08:00
|
|
|
|
public function testRegisterService()
|
|
|
|
|
{
|
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
|
|
|
|
|
2021-06-07 21:15:52 +08:00
|
|
|
|
Pay::registerService(FooServiceProviderStub::class, []);
|
2021-06-06 21:39:26 +08:00
|
|
|
|
|
|
|
|
|
self::assertEquals('bar', Pay::get('foo'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testMagicCallNotFoundService()
|
|
|
|
|
{
|
|
|
|
|
$this->expectException(ServiceNotFoundException::class);
|
|
|
|
|
|
2022-03-05 21:59:11 +08:00
|
|
|
|
Pay::foo1([]);
|
2021-06-06 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-05 21:59:11 +08:00
|
|
|
|
public function testCoreServiceContainer()
|
2021-06-06 21:39:26 +08:00
|
|
|
|
{
|
2022-03-18 19:18:27 +08:00
|
|
|
|
Pay::config(['name' => 'yansongda']);
|
2022-03-05 21:59:11 +08:00
|
|
|
|
|
2022-03-18 19:18:27 +08:00
|
|
|
|
// 单在 hyperf 框架内没有 container,所以手动设置一个
|
|
|
|
|
if (class_exists(Container::class) && class_exists(ApplicationContext::class)) {
|
|
|
|
|
ApplicationContext::setContainer((new ContainerBuilder())->build());
|
2022-03-05 21:59:11 +08:00
|
|
|
|
}
|
2021-06-06 21:39:26 +08:00
|
|
|
|
|
2022-03-18 19:18:27 +08:00
|
|
|
|
self::assertInstanceOf(ContainerInterface::class, Pay::getContainer());
|
2021-06-06 21:39:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCoreServiceConfig()
|
|
|
|
|
{
|
|
|
|
|
$config = ['name' => 'yansongda'];
|
|
|
|
|
Pay::config($config);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Config::class, Pay::get(ConfigInterface::class));
|
|
|
|
|
self::assertEquals($config['name'], Pay::get(ConfigInterface::class)->get('name'));
|
|
|
|
|
|
|
|
|
|
// 修改 config 的情况
|
|
|
|
|
$config2 = [
|
|
|
|
|
'name' => 'yansongda2',
|
|
|
|
|
];
|
|
|
|
|
Pay::set(ConfigInterface::class, new Config($config2));
|
|
|
|
|
|
|
|
|
|
self::assertEquals($config2['name'], Pay::get(ConfigInterface::class)->get('name'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCoreServiceLogger()
|
|
|
|
|
{
|
|
|
|
|
$config = ['name' => 'yansongda','logger' => ['enable' => true]];
|
|
|
|
|
Pay::config($config);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Logger::class, Pay::get(LoggerInterface::class));
|
|
|
|
|
|
2023-06-10 16:18:47 +08:00
|
|
|
|
$otherLogger = new Logger('test');
|
2021-06-06 21:39:26 +08:00
|
|
|
|
Pay::set(LoggerInterface::class, $otherLogger);
|
|
|
|
|
self::assertEquals($otherLogger, Pay::get(LoggerInterface::class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCoreServiceEvent()
|
|
|
|
|
{
|
|
|
|
|
$config = ['name' => 'yansongda'];
|
|
|
|
|
Pay::config($config);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(EventDispatcher::class, Pay::get(EventDispatcherInterface::class));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testCoreServiceHttpClient()
|
|
|
|
|
{
|
|
|
|
|
$config = ['name' => 'yansongda'];
|
|
|
|
|
Pay::config($config);
|
|
|
|
|
|
|
|
|
|
self::assertInstanceOf(Client::class, Pay::get(HttpClientInterface::class));
|
|
|
|
|
|
|
|
|
|
// 使用外部 http client
|
|
|
|
|
$oldClient = Pay::get(HttpClientInterface::class);
|
|
|
|
|
|
|
|
|
|
$client = new Client(['timeout' => 3.0]);
|
|
|
|
|
Pay::set(HttpClientInterface::class, $client);
|
|
|
|
|
|
|
|
|
|
self::assertEquals($client, Pay::get(HttpClientInterface::class));
|
|
|
|
|
self::assertNotEquals($oldClient, Pay::get(HttpClientInterface::class));
|
|
|
|
|
}
|
2020-10-13 00:07:35 +08:00
|
|
|
|
}
|