mirror of
https://gitee.com/technical-laohu/mpay.git
synced 2025-11-14 06:33:44 +08:00
框架更新
This commit is contained in:
206
vendor/topthink/framework/tests/AppTest.php
vendored
Normal file
206
vendor/topthink/framework/tests/AppTest.php
vendored
Normal file
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use org\bovigo\vfs\vfsStreamDirectory;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use stdClass;
|
||||
use think\App;
|
||||
use think\Env;
|
||||
use think\Event;
|
||||
use think\event\AppInit;
|
||||
use think\exception\ClassNotFoundException;
|
||||
use think\Service;
|
||||
|
||||
class SomeService extends Service
|
||||
{
|
||||
public $bind = [
|
||||
'some' => 'class',
|
||||
];
|
||||
|
||||
public function register()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @property array initializers
|
||||
*/
|
||||
class AppTest extends TestCase
|
||||
{
|
||||
/** @var App */
|
||||
protected $app;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = new App();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
public function testService()
|
||||
{
|
||||
$service = m::mock(SomeService::class);
|
||||
|
||||
$service->shouldReceive('register')->once();
|
||||
|
||||
$this->app->register($service);
|
||||
|
||||
$this->assertEquals($service, $this->app->getService(SomeService::class));
|
||||
|
||||
$service2 = m::mock(SomeService::class);
|
||||
|
||||
$service2->shouldReceive('register')->once();
|
||||
|
||||
$this->app->register($service2);
|
||||
|
||||
$this->assertEquals($service, $this->app->getService(SomeService::class));
|
||||
|
||||
$this->app->register($service2, true);
|
||||
|
||||
$this->assertEquals($service2, $this->app->getService(SomeService::class));
|
||||
|
||||
$service->shouldReceive('boot')->once();
|
||||
$service2->shouldReceive('boot')->once();
|
||||
|
||||
$this->app->boot();
|
||||
}
|
||||
|
||||
public function testDebug()
|
||||
{
|
||||
$this->app->debug(false);
|
||||
|
||||
$this->assertFalse($this->app->isDebug());
|
||||
|
||||
$this->app->debug(true);
|
||||
|
||||
$this->assertTrue($this->app->isDebug());
|
||||
}
|
||||
|
||||
public function testNamespace()
|
||||
{
|
||||
$namespace = 'test';
|
||||
|
||||
$this->app->setNamespace($namespace);
|
||||
|
||||
$this->assertEquals($namespace, $this->app->getNamespace());
|
||||
}
|
||||
|
||||
public function testPath()
|
||||
{
|
||||
$rootPath = __DIR__ . DIRECTORY_SEPARATOR;
|
||||
|
||||
$app = new App($rootPath);
|
||||
|
||||
$this->assertEquals($rootPath, $app->getRootPath());
|
||||
|
||||
$this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath());
|
||||
|
||||
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath());
|
||||
|
||||
$appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
|
||||
$app->setAppPath($appPath);
|
||||
$this->assertEquals($appPath, $app->getAppPath());
|
||||
|
||||
$this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath());
|
||||
|
||||
$this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath());
|
||||
|
||||
$this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath());
|
||||
|
||||
$runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
|
||||
$app->setRuntimePath($runtimePath);
|
||||
$this->assertEquals($runtimePath, $app->getRuntimePath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param vfsStreamDirectory $root
|
||||
* @param bool $debug
|
||||
* @return App
|
||||
*/
|
||||
protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true)
|
||||
{
|
||||
$rootPath = $root->url() . DIRECTORY_SEPARATOR;
|
||||
|
||||
$app = new App($rootPath);
|
||||
|
||||
$initializer = m::mock();
|
||||
$initializer->shouldReceive('init')->once()->with($app);
|
||||
|
||||
$app->instance($initializer->mockery_getName(), $initializer);
|
||||
|
||||
(function () use ($initializer) {
|
||||
$this->initializers = [$initializer->mockery_getName()];
|
||||
})->call($app);
|
||||
|
||||
$env = m::mock(Env::class);
|
||||
$env->shouldReceive('load')->once()->with($rootPath . '.env');
|
||||
$env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php');
|
||||
$env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug);
|
||||
|
||||
$event = m::mock(Event::class);
|
||||
$event->shouldReceive('trigger')->once()->with(AppInit::class);
|
||||
$event->shouldReceive('bind')->once()->with([]);
|
||||
$event->shouldReceive('listenEvents')->once()->with([]);
|
||||
$event->shouldReceive('subscribe')->once()->with([]);
|
||||
|
||||
$app->instance('env', $env);
|
||||
$app->instance('event', $event);
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
public function testInitialize()
|
||||
{
|
||||
$root = vfsStream::setup('rootDir', null, [
|
||||
'.env' => '',
|
||||
'app' => [
|
||||
'common.php' => '',
|
||||
'event.php' => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
|
||||
'provider.php' => '<?php return [];',
|
||||
],
|
||||
'config' => [
|
||||
'app.php' => '<?php return [];',
|
||||
],
|
||||
]);
|
||||
|
||||
$app = $this->prepareAppForInitialize($root, true);
|
||||
|
||||
$app->debug(false);
|
||||
|
||||
$app->initialize();
|
||||
|
||||
$this->assertIsInt($app->getBeginMem());
|
||||
$this->assertIsFloat($app->getBeginTime());
|
||||
|
||||
$this->assertTrue($app->initialized());
|
||||
}
|
||||
|
||||
public function testFactory()
|
||||
{
|
||||
$this->assertInstanceOf(stdClass::class, App::factory(stdClass::class));
|
||||
|
||||
$this->expectException(ClassNotFoundException::class);
|
||||
|
||||
App::factory('SomeClass');
|
||||
}
|
||||
|
||||
public function testParseClass()
|
||||
{
|
||||
$this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
|
||||
$this->app->setNamespace('app2');
|
||||
$this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
|
||||
}
|
||||
|
||||
}
|
||||
154
vendor/topthink/framework/tests/CacheTest.php
vendored
Normal file
154
vendor/topthink/framework/tests/CacheTest.php
vendored
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\App;
|
||||
use think\Cache;
|
||||
use think\Config;
|
||||
use think\Container;
|
||||
|
||||
class CacheTest extends TestCase
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var Cache|MockInterface */
|
||||
protected $cache;
|
||||
|
||||
/** @var Config|MockInterface */
|
||||
protected $config;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
|
||||
Container::setInstance($this->app);
|
||||
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
|
||||
$this->config = m::mock(Config::class)->makePartial();
|
||||
$this->app->shouldReceive('get')->with('config')->andReturn($this->config);
|
||||
|
||||
$this->cache = new Cache($this->app);
|
||||
}
|
||||
|
||||
public function testGetConfig()
|
||||
{
|
||||
$config = [
|
||||
'default' => 'file',
|
||||
];
|
||||
|
||||
$this->config->shouldReceive('get')->with('cache')->andReturn($config);
|
||||
|
||||
$this->assertEquals($config, $this->cache->getConfig());
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->cache->getStoreConfig('foo');
|
||||
}
|
||||
|
||||
public function testCacheManagerInstances()
|
||||
{
|
||||
$this->config->shouldReceive('get')->with("cache.stores.single", null)->andReturn(['type' => 'file']);
|
||||
|
||||
$channel1 = $this->cache->store('single');
|
||||
$channel2 = $this->cache->store('single');
|
||||
|
||||
$this->assertSame($channel1, $channel2);
|
||||
}
|
||||
|
||||
public function testFileCache()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
|
||||
$this->config->shouldReceive('get')->with("cache.default", null)->andReturn('file');
|
||||
|
||||
$this->config->shouldReceive('get')->with("cache.stores.file", null)
|
||||
->andReturn(['type' => 'file', 'path' => $root->url()]);
|
||||
|
||||
$this->cache->set('foo', 5);
|
||||
$this->cache->inc('foo');
|
||||
$this->assertEquals(6, $this->cache->get('foo'));
|
||||
$this->cache->dec('foo', 2);
|
||||
$this->assertEquals(4, $this->cache->get('foo'));
|
||||
|
||||
$this->cache->set('bar', true);
|
||||
$this->assertTrue($this->cache->get('bar'));
|
||||
|
||||
$this->cache->set('baz', null);
|
||||
$this->assertNull($this->cache->get('baz'));
|
||||
|
||||
$this->assertTrue($this->cache->has('baz'));
|
||||
$this->cache->delete('baz');
|
||||
$this->assertFalse($this->cache->has('baz'));
|
||||
$this->assertNull($this->cache->get('baz'));
|
||||
$this->assertFalse($this->cache->get('baz', false));
|
||||
|
||||
$this->assertTrue($root->hasChildren());
|
||||
$this->cache->clear();
|
||||
$this->assertFalse($root->hasChildren());
|
||||
|
||||
//tags
|
||||
$this->cache->tag('foo')->set('bar', 'foobar');
|
||||
$this->assertEquals('foobar', $this->cache->get('bar'));
|
||||
$this->cache->tag('foo')->remember('baz', 'foobar');
|
||||
$this->assertEquals('foobar', $this->cache->get('baz'));
|
||||
$this->cache->tag('foo')->clear();
|
||||
$this->assertFalse($this->cache->has('bar'));
|
||||
|
||||
//multiple
|
||||
$this->cache->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
|
||||
$this->cache->tag('foo')->setMultiple(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']]);
|
||||
$this->assertEquals(['foo' => ['foobar', 'bar'], 'foobar' => ['foo', 'bar']], $this->cache->getMultiple(['foo', 'foobar']));
|
||||
$this->assertIsInt($this->cache->getWriteTimes());
|
||||
$this->assertTrue($this->cache->deleteMultiple(['foo', 'foobar']));
|
||||
}
|
||||
|
||||
public function testRedisCache()
|
||||
{
|
||||
if (extension_loaded('redis')) {
|
||||
return;
|
||||
}
|
||||
$this->config->shouldReceive('get')->with("cache.default", null)->andReturn('redis');
|
||||
$this->config->shouldReceive('get')->with("cache.stores.redis", null)->andReturn(['type' => 'redis']);
|
||||
|
||||
$redis = m::mock('overload:\Predis\Client');
|
||||
|
||||
$redis->shouldReceive("set")->once()->with('foo', 5)->andReturnTrue();
|
||||
$redis->shouldReceive("incrby")->once()->with('foo', 1)->andReturnTrue();
|
||||
$redis->shouldReceive("decrby")->once()->with('foo', 2)->andReturnTrue();
|
||||
$redis->shouldReceive("get")->once()->with('foo')->andReturn('6');
|
||||
$redis->shouldReceive("get")->once()->with('foo')->andReturn('4');
|
||||
$redis->shouldReceive("set")->once()->with('bar', serialize(true))->andReturnTrue();
|
||||
$redis->shouldReceive("set")->once()->with('baz', serialize(null))->andReturnTrue();
|
||||
$redis->shouldReceive("del")->once()->with('baz')->andReturnTrue();
|
||||
$redis->shouldReceive("flushDB")->once()->andReturnTrue();
|
||||
$redis->shouldReceive("set")->once()->with('bar', serialize('foobar'))->andReturnTrue();
|
||||
$redis->shouldReceive("sAdd")->once()->with('tag:' . md5('foo'), 'bar')->andReturnTrue();
|
||||
$redis->shouldReceive("sMembers")->once()->with('tag:' . md5('foo'))->andReturn(['bar']);
|
||||
$redis->shouldReceive("del")->once()->with(['bar'])->andReturnTrue();
|
||||
$redis->shouldReceive("del")->once()->with('tag:' . md5('foo'))->andReturnTrue();
|
||||
|
||||
$this->cache->set('foo', 5);
|
||||
$this->cache->inc('foo');
|
||||
$this->assertEquals(6, $this->cache->get('foo'));
|
||||
$this->cache->dec('foo', 2);
|
||||
$this->assertEquals(4, $this->cache->get('foo'));
|
||||
|
||||
$this->cache->set('bar', true);
|
||||
$this->cache->set('baz', null);
|
||||
$this->cache->delete('baz');
|
||||
$this->cache->clear();
|
||||
|
||||
//tags
|
||||
$this->cache->tag('foo')->set('bar', 'foobar');
|
||||
$this->cache->tag('foo')->clear();
|
||||
}
|
||||
}
|
||||
46
vendor/topthink/framework/tests/ConfigTest.php
vendored
Normal file
46
vendor/topthink/framework/tests/ConfigTest.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Config;
|
||||
|
||||
class ConfigTest extends TestCase
|
||||
{
|
||||
public function testLoad()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
$file = vfsStream::newFile('test.php')->setContent("<?php return ['key1'=> 'value1','key2'=>'value2'];");
|
||||
$root->addChild($file);
|
||||
|
||||
$config = new Config();
|
||||
|
||||
$config->load($file->url(), 'test');
|
||||
|
||||
$this->assertEquals('value1', $config->get('test.key1'));
|
||||
$this->assertEquals('value2', $config->get('test.key2'));
|
||||
|
||||
$this->assertSame(['key1' => 'value1', 'key2' => 'value2'], $config->get('test'));
|
||||
}
|
||||
|
||||
public function testSetAndGet()
|
||||
{
|
||||
$config = new Config();
|
||||
|
||||
$config->set([
|
||||
'key1' => 'value1',
|
||||
'key2' => [
|
||||
'key3' => 'value3',
|
||||
],
|
||||
], 'test');
|
||||
|
||||
$this->assertTrue($config->has('test.key1'));
|
||||
$this->assertEquals('value1', $config->get('test.key1'));
|
||||
$this->assertEquals('value3', $config->get('test.key2.key3'));
|
||||
|
||||
$this->assertEquals(['key3' => 'value3'], $config->get('test.key2'));
|
||||
$this->assertFalse($config->has('test.key3'));
|
||||
$this->assertEquals('none', $config->get('test.key3', 'none'));
|
||||
}
|
||||
}
|
||||
314
vendor/topthink/framework/tests/ContainerTest.php
vendored
Normal file
314
vendor/topthink/framework/tests/ContainerTest.php
vendored
Normal file
@@ -0,0 +1,314 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionMethod;
|
||||
use stdClass;
|
||||
use think\Container;
|
||||
use think\exception\ClassNotFoundException;
|
||||
use think\exception\FuncNotFoundException;
|
||||
|
||||
class Taylor
|
||||
{
|
||||
public $name;
|
||||
|
||||
public function __construct($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function some(Container $container)
|
||||
{
|
||||
}
|
||||
|
||||
protected function protectionFun()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public static function test(Container $container)
|
||||
{
|
||||
return $container;
|
||||
}
|
||||
|
||||
public static function __make()
|
||||
{
|
||||
return new self('Taylor');
|
||||
}
|
||||
}
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public $container;
|
||||
|
||||
public $count = 0;
|
||||
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
}
|
||||
|
||||
class ContainerTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Container::setInstance(null);
|
||||
}
|
||||
|
||||
public function testClosureResolution()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
Container::setInstance($container);
|
||||
|
||||
$container->bind('name', function () {
|
||||
return 'Taylor';
|
||||
});
|
||||
|
||||
$this->assertEquals('Taylor', $container->make('name'));
|
||||
|
||||
$this->assertEquals('Taylor', Container::pull('name'));
|
||||
}
|
||||
|
||||
public function testGet()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
$this->expectException(ClassNotFoundException::class);
|
||||
$this->expectExceptionMessage('class not exists: name');
|
||||
$container->get('name');
|
||||
|
||||
$container->bind('name', function () {
|
||||
return 'Taylor';
|
||||
});
|
||||
|
||||
$this->assertSame('Taylor', $container->get('name'));
|
||||
}
|
||||
|
||||
public function testExist()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
$container->bind('name', function () {
|
||||
return 'Taylor';
|
||||
});
|
||||
|
||||
$this->assertFalse($container->exists("name"));
|
||||
|
||||
$container->make('name');
|
||||
|
||||
$this->assertTrue($container->exists('name'));
|
||||
}
|
||||
|
||||
public function testInstance()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
$container->bind('name', function () {
|
||||
return 'Taylor';
|
||||
});
|
||||
|
||||
$this->assertEquals('Taylor', $container->get('name'));
|
||||
|
||||
$container->bind('name2', Taylor::class);
|
||||
|
||||
$object = new stdClass();
|
||||
|
||||
$this->assertFalse($container->exists('name2'));
|
||||
|
||||
$container->instance('name2', $object);
|
||||
|
||||
$this->assertTrue($container->exists('name2'));
|
||||
|
||||
$this->assertTrue($container->exists(Taylor::class));
|
||||
|
||||
$this->assertEquals($object, $container->make(Taylor::class));
|
||||
|
||||
unset($container->name1);
|
||||
|
||||
$this->assertFalse($container->exists('name1'));
|
||||
|
||||
$container->delete('name2');
|
||||
|
||||
$this->assertFalse($container->exists('name2'));
|
||||
|
||||
foreach ($container as $class => $instance) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function testBind()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
$object = new stdClass();
|
||||
|
||||
$container->bind(['name' => Taylor::class]);
|
||||
|
||||
$container->bind('name2', $object);
|
||||
|
||||
$container->bind('name3', Taylor::class);
|
||||
$container->bind('name3', Taylor::class);
|
||||
|
||||
$container->name4 = $object;
|
||||
|
||||
$container['name5'] = $object;
|
||||
|
||||
$this->assertTrue(isset($container->name4));
|
||||
|
||||
$this->assertTrue(isset($container['name5']));
|
||||
|
||||
$this->assertInstanceOf(Taylor::class, $container->get('name'));
|
||||
|
||||
$this->assertSame($object, $container->get('name2'));
|
||||
|
||||
$this->assertSame($object, $container->name4);
|
||||
|
||||
$this->assertSame($object, $container['name5']);
|
||||
|
||||
$this->assertInstanceOf(Taylor::class, $container->get('name3'));
|
||||
|
||||
unset($container['name']);
|
||||
|
||||
$this->assertFalse(isset($container['name']));
|
||||
|
||||
unset($container->name3);
|
||||
|
||||
$this->assertFalse(isset($container->name3));
|
||||
}
|
||||
|
||||
public function testAutoConcreteResolution()
|
||||
{
|
||||
$container = new Container;
|
||||
|
||||
$taylor = $container->make(Taylor::class);
|
||||
|
||||
$this->assertInstanceOf(Taylor::class, $taylor);
|
||||
$this->assertSame('Taylor', $taylor->name);
|
||||
}
|
||||
|
||||
public function testGetAndSetInstance()
|
||||
{
|
||||
$this->assertInstanceOf(Container::class, Container::getInstance());
|
||||
|
||||
$object = new stdClass();
|
||||
|
||||
Container::setInstance($object);
|
||||
|
||||
$this->assertSame($object, Container::getInstance());
|
||||
|
||||
Container::setInstance(function () {
|
||||
return $this;
|
||||
});
|
||||
|
||||
$this->assertSame($this, Container::getInstance());
|
||||
}
|
||||
|
||||
public function testResolving()
|
||||
{
|
||||
$container = new Container();
|
||||
$container->bind(Container::class, $container);
|
||||
|
||||
$container->resolving(function (SomeClass $taylor, Container $container) {
|
||||
$taylor->count++;
|
||||
});
|
||||
$container->resolving(SomeClass::class, function (SomeClass $taylor, Container $container) {
|
||||
$taylor->count++;
|
||||
});
|
||||
|
||||
/** @var SomeClass $someClass */
|
||||
$someClass = $container->invokeClass(SomeClass::class);
|
||||
$this->assertEquals(2, $someClass->count);
|
||||
}
|
||||
|
||||
public function testInvokeFunctionWithoutMethodThrowsException()
|
||||
{
|
||||
$this->expectException(FuncNotFoundException::class);
|
||||
$this->expectExceptionMessage('function not exists: ContainerTestCallStub()');
|
||||
$container = new Container();
|
||||
$container->invokeFunction('ContainerTestCallStub', []);
|
||||
}
|
||||
|
||||
public function testInvokeProtectionMethod()
|
||||
{
|
||||
$container = new Container();
|
||||
$this->assertTrue($container->invokeMethod([Taylor::class, 'protectionFun'], [], true));
|
||||
}
|
||||
|
||||
public function testInvoke()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
Container::setInstance($container);
|
||||
|
||||
$container->bind(Container::class, $container);
|
||||
|
||||
$stub = $this->createMock(Taylor::class);
|
||||
|
||||
$stub->expects($this->once())->method('some')->with($container)->will($this->returnSelf());
|
||||
|
||||
$container->invokeMethod([$stub, 'some']);
|
||||
|
||||
$this->assertEquals('48', $container->invoke('ord', ['0']));
|
||||
|
||||
$this->assertSame($container, $container->invoke(Taylor::class . '::test', []));
|
||||
|
||||
$this->assertSame($container, $container->invokeMethod(Taylor::class . '::test'));
|
||||
|
||||
$reflect = new ReflectionMethod($container, 'exists');
|
||||
|
||||
$this->assertTrue($container->invokeReflectMethod($container, $reflect, [Container::class]));
|
||||
|
||||
$this->assertSame($container, $container->invoke(function (Container $container) {
|
||||
return $container;
|
||||
}));
|
||||
|
||||
$this->assertSame($container, $container->invoke(Taylor::class . '::test'));
|
||||
|
||||
$object = $container->invokeClass(SomeClass::class);
|
||||
$this->assertInstanceOf(SomeClass::class, $object);
|
||||
$this->assertSame($container, $object->container);
|
||||
|
||||
$stdClass = new stdClass();
|
||||
|
||||
$container->invoke(function (Container $container, stdClass $stdObject, $key1, $lowKey, $key2 = 'default') use ($stdClass) {
|
||||
$this->assertEquals('value1', $key1);
|
||||
$this->assertEquals('default', $key2);
|
||||
$this->assertEquals('value2', $lowKey);
|
||||
$this->assertSame($stdClass, $stdObject);
|
||||
return $container;
|
||||
}, ['some' => $stdClass, 'key1' => 'value1', 'low_key' => 'value2']);
|
||||
}
|
||||
|
||||
public function testInvokeMethodNotExists()
|
||||
{
|
||||
$container = $this->resolveContainer();
|
||||
$this->expectException(FuncNotFoundException::class);
|
||||
|
||||
$container->invokeMethod([SomeClass::class, 'any']);
|
||||
}
|
||||
|
||||
public function testInvokeClassNotExists()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
Container::setInstance($container);
|
||||
|
||||
$container->bind(Container::class, $container);
|
||||
|
||||
$this->expectExceptionObject(new ClassNotFoundException('class not exists: SomeClass'));
|
||||
|
||||
$container->invokeClass('SomeClass');
|
||||
}
|
||||
|
||||
protected function resolveContainer()
|
||||
{
|
||||
$container = new Container();
|
||||
|
||||
Container::setInstance($container);
|
||||
return $container;
|
||||
}
|
||||
|
||||
}
|
||||
49
vendor/topthink/framework/tests/DbTest.php
vendored
Normal file
49
vendor/topthink/framework/tests/DbTest.php
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Cache;
|
||||
use think\cache\Driver;
|
||||
use think\Config;
|
||||
use think\Db;
|
||||
use think\Event;
|
||||
use think\Log;
|
||||
|
||||
class DbTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
public function testMake()
|
||||
{
|
||||
$event = m::mock(Event::class);
|
||||
$config = m::mock(Config::class);
|
||||
$log = m::mock(Log::class);
|
||||
$cache = m::mock(Cache::class);
|
||||
$store = m::mock(Driver::class);
|
||||
|
||||
$config->shouldReceive('get')->with('database.cache_store', null)->andReturn(null);
|
||||
$cache->shouldReceive('store')->with(null)->andReturn($store);
|
||||
|
||||
$db = Db::__make($event, $config, $log, $cache);
|
||||
|
||||
$config->shouldReceive('get')->with('database.foo', null)->andReturn('foo');
|
||||
$this->assertEquals('foo', $db->getConfig('foo'));
|
||||
|
||||
$config->shouldReceive('get')->with('database', [])->andReturn([]);
|
||||
$this->assertEquals([], $db->getConfig());
|
||||
|
||||
$callback = function () {
|
||||
};
|
||||
$event->shouldReceive('listen')->with('db.some', $callback);
|
||||
$db->event('some', $callback);
|
||||
|
||||
$event->shouldReceive('trigger')->with('db.some', null, false);
|
||||
$db->trigger('some');
|
||||
}
|
||||
|
||||
}
|
||||
32
vendor/topthink/framework/tests/DispatchTest.php
vendored
Normal file
32
vendor/topthink/framework/tests/DispatchTest.php
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Mockery;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Request;
|
||||
use think\route\Dispatch;
|
||||
use think\route\Rule;
|
||||
|
||||
class DispatchTest extends TestCase
|
||||
{
|
||||
public function testPsr7Response()
|
||||
{
|
||||
$request = Mockery::mock(Request::class);
|
||||
$rule = Mockery::mock(Rule::class);
|
||||
$dispatch = new class($request, $rule, '') extends Dispatch {
|
||||
public function exec()
|
||||
{
|
||||
return new Response(200, ['framework' => ['tp', 'thinkphp'], 'psr' => 'psr-7'], '123');
|
||||
}
|
||||
};
|
||||
|
||||
$response = $dispatch->run();
|
||||
|
||||
$this->assertInstanceOf(\think\Response::class, $response);
|
||||
$this->assertEquals('123', $response->getContent());
|
||||
$this->assertEquals('tp, thinkphp', $response->getHeader('framework'));
|
||||
$this->assertEquals('psr-7', $response->getHeader('psr'));
|
||||
}
|
||||
}
|
||||
80
vendor/topthink/framework/tests/EnvTest.php
vendored
Normal file
80
vendor/topthink/framework/tests/EnvTest.php
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Env;
|
||||
use think\Exception;
|
||||
|
||||
class EnvTest extends TestCase
|
||||
{
|
||||
public function testEnvFile()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
$envFile = vfsStream::newFile('.env')->setContent("key1=value1\nkey2=value2");
|
||||
$root->addChild($envFile);
|
||||
|
||||
$env = new Env();
|
||||
|
||||
$env->load($envFile->url());
|
||||
|
||||
$this->assertEquals('value1', $env->get('key1'));
|
||||
$this->assertEquals('value2', $env->get('key2'));
|
||||
}
|
||||
|
||||
public function testServerEnv()
|
||||
{
|
||||
$env = new Env();
|
||||
|
||||
$this->assertEquals('value2', $env->get('key2', 'value2'));
|
||||
|
||||
putenv('PHP_KEY7=value7');
|
||||
putenv('PHP_KEY8=false');
|
||||
putenv('PHP_KEY9=true');
|
||||
|
||||
$this->assertEquals('value7', $env->get('key7'));
|
||||
$this->assertFalse($env->get('KEY8'));
|
||||
$this->assertTrue($env->get('key9'));
|
||||
}
|
||||
|
||||
public function testSetEnv()
|
||||
{
|
||||
$env = new Env();
|
||||
|
||||
$env->set([
|
||||
'key1' => 'value1',
|
||||
'key2' => [
|
||||
'key1' => 'value1-2',
|
||||
],
|
||||
]);
|
||||
|
||||
$env->set('key3', 'value3');
|
||||
|
||||
$env->key4 = 'value4';
|
||||
|
||||
$env['key5'] = 'value5';
|
||||
|
||||
$this->assertEquals('value1', $env->get('key1'));
|
||||
$this->assertEquals('value1-2', $env->get('key2.key1'));
|
||||
|
||||
$this->assertEquals('value3', $env->get('key3'));
|
||||
|
||||
$this->assertEquals('value4', $env->key4);
|
||||
|
||||
$this->assertEquals('value5', $env['key5']);
|
||||
|
||||
$this->expectException(Exception::class);
|
||||
|
||||
unset($env['key5']);
|
||||
}
|
||||
|
||||
public function testHasEnv()
|
||||
{
|
||||
$env = new Env();
|
||||
$env->set(['foo' => 'bar']);
|
||||
$this->assertTrue($env->has('foo'));
|
||||
$this->assertTrue(isset($env->foo));
|
||||
$this->assertTrue($env->offsetExists('foo'));
|
||||
}
|
||||
}
|
||||
134
vendor/topthink/framework/tests/EventTest.php
vendored
Normal file
134
vendor/topthink/framework/tests/EventTest.php
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\App;
|
||||
use think\Config;
|
||||
use think\Container;
|
||||
use think\Event;
|
||||
|
||||
class EventTest extends TestCase
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var Event|MockInterface */
|
||||
protected $event;
|
||||
|
||||
/** @var Config|MockInterface */
|
||||
protected $config;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
|
||||
Container::setInstance($this->app);
|
||||
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
|
||||
$this->config = m::mock(Config::class)->makePartial();
|
||||
$this->app->shouldReceive('get')->with('config')->andReturn($this->config);
|
||||
|
||||
$this->event = new Event($this->app);
|
||||
}
|
||||
|
||||
public function testBasic()
|
||||
{
|
||||
$this->event->bind(['foo' => 'baz']);
|
||||
|
||||
$this->event->listen('foo', function ($bar) {
|
||||
$this->assertEquals('bar', $bar);
|
||||
});
|
||||
|
||||
$this->assertTrue($this->event->hasListener('foo'));
|
||||
|
||||
$this->event->trigger('baz', 'bar');
|
||||
|
||||
$this->event->remove('foo');
|
||||
|
||||
$this->assertFalse($this->event->hasListener('foo'));
|
||||
}
|
||||
|
||||
public function testOnceEvent()
|
||||
{
|
||||
$this->event->listen('AppInit', function ($bar) {
|
||||
$this->assertEquals('bar', $bar);
|
||||
return 'foo';
|
||||
});
|
||||
|
||||
$this->assertEquals('foo', $this->event->trigger('AppInit', 'bar', true));
|
||||
$this->assertEquals(['foo'], $this->event->trigger('AppInit', 'bar'));
|
||||
}
|
||||
|
||||
public function testClassListener()
|
||||
{
|
||||
$listener = m::mock("overload:SomeListener", TestListener::class);
|
||||
|
||||
$listener->shouldReceive('handle')->andReturnTrue();
|
||||
|
||||
$this->event->listen('some', "SomeListener");
|
||||
|
||||
$this->assertTrue($this->event->until('some'));
|
||||
}
|
||||
|
||||
public function testSubscribe()
|
||||
{
|
||||
$listener = m::mock("overload:SomeListener", TestListener::class);
|
||||
|
||||
$listener->shouldReceive('subscribe')->andReturnUsing(function (Event $event) use ($listener) {
|
||||
|
||||
$listener->shouldReceive('onBar')->once()->andReturnFalse();
|
||||
|
||||
$event->listenEvents(['SomeListener::onBar' => [[$listener, 'onBar']]]);
|
||||
});
|
||||
|
||||
$this->event->subscribe('SomeListener');
|
||||
|
||||
$this->assertTrue($this->event->hasListener('SomeListener::onBar'));
|
||||
|
||||
$this->event->trigger('SomeListener::onBar');
|
||||
}
|
||||
|
||||
public function testAutoObserve()
|
||||
{
|
||||
$listener = m::mock("overload:SomeListener", TestListener::class);
|
||||
|
||||
$listener->shouldReceive('onBar')->once();
|
||||
|
||||
$this->app->shouldReceive('make')->with('SomeListener')->andReturn($listener);
|
||||
|
||||
$this->event->observe('SomeListener');
|
||||
|
||||
$this->event->trigger('bar');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestListener
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function onBar()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function onFoo()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function subscribe()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
155
vendor/topthink/framework/tests/HttpTest.php
vendored
Normal file
155
vendor/topthink/framework/tests/HttpTest.php
vendored
Normal file
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\App;
|
||||
use think\Console;
|
||||
use think\Event;
|
||||
use think\event\HttpEnd;
|
||||
use think\Exception;
|
||||
use think\exception\Handle;
|
||||
use think\Http;
|
||||
use think\Log;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
use think\Route;
|
||||
|
||||
class HttpTest extends TestCase
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var Http|MockInterface */
|
||||
protected $http;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
|
||||
$this->http = m::mock(Http::class, [$this->app])->shouldAllowMockingProtectedMethods()->makePartial();
|
||||
}
|
||||
|
||||
protected function prepareApp($request, $response)
|
||||
{
|
||||
$this->app->shouldReceive('instance')->once()->with('request', $request);
|
||||
$this->app->shouldReceive('initialized')->once()->andReturnFalse();
|
||||
$this->app->shouldReceive('initialize')->once();
|
||||
$this->app->shouldReceive('get')->with('request')->andReturn($request);
|
||||
|
||||
$route = m::mock(Route::class);
|
||||
|
||||
$route->shouldReceive('dispatch')->withArgs(function ($req, $withRoute) use ($request) {
|
||||
if ($withRoute) {
|
||||
$withRoute();
|
||||
}
|
||||
return $req === $request;
|
||||
})->andReturn($response);
|
||||
|
||||
$route->shouldReceive('config')->with('route_annotation')->andReturn(true);
|
||||
|
||||
$this->app->shouldReceive('get')->with('route')->andReturn($route);
|
||||
|
||||
$console = m::mock(Console::class);
|
||||
|
||||
$console->shouldReceive('call');
|
||||
|
||||
$this->app->shouldReceive('get')->with('console')->andReturn($console);
|
||||
}
|
||||
|
||||
public function testRun()
|
||||
{
|
||||
$root = vfsStream::setup('rootDir', null, [
|
||||
'app' => [
|
||||
'controller' => [],
|
||||
'middleware.php' => '<?php return [];',
|
||||
],
|
||||
'route' => [
|
||||
'route.php' => '<?php return [];',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->app->shouldReceive('getBasePath')->andReturn($root->getChild('app')->url() . DIRECTORY_SEPARATOR);
|
||||
$this->app->shouldReceive('getRootPath')->andReturn($root->url() . DIRECTORY_SEPARATOR);
|
||||
|
||||
$request = m::mock(Request::class)->makePartial();
|
||||
$response = m::mock(Response::class)->makePartial();
|
||||
|
||||
$this->prepareApp($request, $response);
|
||||
|
||||
$this->assertEquals($response, $this->http->run($request));
|
||||
}
|
||||
|
||||
public function multiAppRunProvider()
|
||||
{
|
||||
$request1 = m::mock(Request::class)->makePartial();
|
||||
$request1->shouldReceive('subDomain')->andReturn('www');
|
||||
$request1->shouldReceive('host')->andReturn('www.domain.com');
|
||||
|
||||
$request2 = m::mock(Request::class)->makePartial();
|
||||
$request2->shouldReceive('subDomain')->andReturn('app2');
|
||||
$request2->shouldReceive('host')->andReturn('app2.domain.com');
|
||||
|
||||
$request3 = m::mock(Request::class)->makePartial();
|
||||
$request3->shouldReceive('pathinfo')->andReturn('some1/a/b/c');
|
||||
|
||||
$request4 = m::mock(Request::class)->makePartial();
|
||||
$request4->shouldReceive('pathinfo')->andReturn('app3/a/b/c');
|
||||
|
||||
$request5 = m::mock(Request::class)->makePartial();
|
||||
$request5->shouldReceive('pathinfo')->andReturn('some2/a/b/c');
|
||||
|
||||
return [
|
||||
[$request1, true, 'app1'],
|
||||
[$request2, true, 'app2'],
|
||||
[$request3, true, 'app3'],
|
||||
[$request4, true, null],
|
||||
[$request5, true, 'some2', 'path'],
|
||||
[$request1, false, 'some3'],
|
||||
];
|
||||
}
|
||||
|
||||
public function testRunWithException()
|
||||
{
|
||||
$request = m::mock(Request::class);
|
||||
$response = m::mock(Response::class);
|
||||
|
||||
$this->app->shouldReceive('instance')->once()->with('request', $request);
|
||||
$this->app->shouldReceive('initialize')->once();
|
||||
|
||||
$exception = new Exception();
|
||||
|
||||
$this->http->shouldReceive('runWithRequest')->once()->with($request)->andThrow($exception);
|
||||
|
||||
$handle = m::mock(Handle::class);
|
||||
|
||||
$handle->shouldReceive('report')->once()->with($exception);
|
||||
$handle->shouldReceive('render')->once()->with($request, $exception)->andReturn($response);
|
||||
|
||||
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
|
||||
|
||||
$this->assertEquals($response, $this->http->run($request));
|
||||
}
|
||||
|
||||
public function testEnd()
|
||||
{
|
||||
$response = m::mock(Response::class);
|
||||
$event = m::mock(Event::class);
|
||||
$event->shouldReceive('trigger')->once()->with(HttpEnd::class, $response);
|
||||
$this->app->shouldReceive('get')->once()->with('event')->andReturn($event);
|
||||
$log = m::mock(Log::class);
|
||||
$log->shouldReceive('save')->once();
|
||||
$this->app->shouldReceive('get')->once()->with('log')->andReturn($log);
|
||||
|
||||
$this->http->end($response);
|
||||
}
|
||||
|
||||
}
|
||||
30
vendor/topthink/framework/tests/InteractsWithApp.php
vendored
Normal file
30
vendor/topthink/framework/tests/InteractsWithApp.php
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use think\App;
|
||||
use think\Config;
|
||||
use think\Container;
|
||||
|
||||
trait InteractsWithApp
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var Config|MockInterface */
|
||||
protected $config;
|
||||
|
||||
protected function prepareApp()
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
Container::setInstance($this->app);
|
||||
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
|
||||
$this->app->shouldReceive('isDebug')->andReturnTrue();
|
||||
$this->config = m::mock(Config::class)->makePartial();
|
||||
$this->config->shouldReceive('get')->with('app.show_error_msg')->andReturnTrue();
|
||||
$this->app->shouldReceive('get')->with('config')->andReturn($this->config);
|
||||
$this->app->shouldReceive('runningInConsole')->andReturn(false);
|
||||
}
|
||||
}
|
||||
132
vendor/topthink/framework/tests/LogTest.php
vendored
Normal file
132
vendor/topthink/framework/tests/LogTest.php
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use InvalidArgumentException;
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Log;
|
||||
use think\log\ChannelSet;
|
||||
|
||||
class LogTest extends TestCase
|
||||
{
|
||||
use InteractsWithApp;
|
||||
|
||||
/** @var Log|MockInterface */
|
||||
protected $log;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->prepareApp();
|
||||
|
||||
$this->log = new Log($this->app);
|
||||
}
|
||||
|
||||
public function testGetConfig()
|
||||
{
|
||||
$config = [
|
||||
'default' => 'file',
|
||||
];
|
||||
|
||||
$this->config->shouldReceive('get')->with('log')->andReturn($config);
|
||||
|
||||
$this->assertEquals($config, $this->log->getConfig());
|
||||
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->log->getChannelConfig('foo');
|
||||
}
|
||||
|
||||
public function testChannel()
|
||||
{
|
||||
$this->assertInstanceOf(ChannelSet::class, $this->log->channel(['file', 'mail']));
|
||||
}
|
||||
|
||||
public function testLogManagerInstances()
|
||||
{
|
||||
$this->config->shouldReceive('get')->with("log.channels.single", null)->andReturn(['type' => 'file']);
|
||||
|
||||
$channel1 = $this->log->channel('single');
|
||||
$channel2 = $this->log->channel('single');
|
||||
|
||||
$this->assertSame($channel1, $channel2);
|
||||
}
|
||||
|
||||
public function testFileLog()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
|
||||
$this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
|
||||
|
||||
$this->config->shouldReceive('get')->with("log.channels.file", null)
|
||||
->andReturn(['type' => 'file', 'path' => $root->url()]);
|
||||
|
||||
$this->log->info('foo');
|
||||
|
||||
$this->assertEquals($this->log->getLog(), ['info' => ['foo']]);
|
||||
|
||||
$this->log->clear();
|
||||
|
||||
$this->assertEmpty($this->log->getLog());
|
||||
|
||||
$this->log->error('foo');
|
||||
$this->assertArrayHasKey('error', $this->log->getLog());
|
||||
|
||||
$this->log->emergency('foo');
|
||||
$this->assertArrayHasKey('emergency', $this->log->getLog());
|
||||
|
||||
$this->log->alert('foo');
|
||||
$this->assertArrayHasKey('alert', $this->log->getLog());
|
||||
|
||||
$this->log->critical('foo');
|
||||
$this->assertArrayHasKey('critical', $this->log->getLog());
|
||||
|
||||
$this->log->warning('foo');
|
||||
$this->assertArrayHasKey('warning', $this->log->getLog());
|
||||
|
||||
$this->log->notice('foo');
|
||||
$this->assertArrayHasKey('notice', $this->log->getLog());
|
||||
|
||||
$this->log->debug('foo');
|
||||
$this->assertArrayHasKey('debug', $this->log->getLog());
|
||||
|
||||
$this->log->sql('foo');
|
||||
$this->assertArrayHasKey('sql', $this->log->getLog());
|
||||
|
||||
$this->log->custom('foo');
|
||||
$this->assertArrayHasKey('custom', $this->log->getLog());
|
||||
|
||||
$this->log->write('foo');
|
||||
$this->assertTrue($root->hasChildren());
|
||||
$this->assertEmpty($this->log->getLog());
|
||||
|
||||
$this->log->close();
|
||||
|
||||
$this->log->info('foo');
|
||||
|
||||
$this->assertEmpty($this->log->getLog());
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
|
||||
$this->config->shouldReceive('get')->with("log.default", null)->andReturn('file');
|
||||
|
||||
$this->config->shouldReceive('get')->with("log.channels.file", null)
|
||||
->andReturn(['type' => 'file', 'path' => $root->url()]);
|
||||
|
||||
$this->log->info('foo');
|
||||
|
||||
$this->log->save();
|
||||
|
||||
$this->assertTrue($root->hasChildren());
|
||||
}
|
||||
|
||||
}
|
||||
110
vendor/topthink/framework/tests/MiddlewareTest.php
vendored
Normal file
110
vendor/topthink/framework/tests/MiddlewareTest.php
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Exception;
|
||||
use think\exception\Handle;
|
||||
use think\Middleware;
|
||||
use think\Pipeline;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
|
||||
class MiddlewareTest extends TestCase
|
||||
{
|
||||
use InteractsWithApp;
|
||||
|
||||
/** @var Middleware|MockInterface */
|
||||
protected $middleware;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->prepareApp();
|
||||
|
||||
$this->middleware = new Middleware($this->app);
|
||||
}
|
||||
|
||||
public function testSetMiddleware()
|
||||
{
|
||||
$this->middleware->add('BarMiddleware', 'bar');
|
||||
|
||||
$this->assertEquals(1, count($this->middleware->all('bar')));
|
||||
|
||||
$this->middleware->controller('BarMiddleware');
|
||||
$this->assertEquals(1, count($this->middleware->all('controller')));
|
||||
|
||||
$this->middleware->import(['FooMiddleware']);
|
||||
$this->assertEquals(1, count($this->middleware->all()));
|
||||
|
||||
$this->middleware->unshift(['BazMiddleware', 'baz']);
|
||||
$this->assertEquals(2, count($this->middleware->all()));
|
||||
$this->assertEquals([['BazMiddleware', 'handle'], 'baz'], $this->middleware->all()[0]);
|
||||
|
||||
$this->config->shouldReceive('get')->with('middleware.alias', [])
|
||||
->andReturn(['foo' => ['FooMiddleware', 'FarMiddleware']]);
|
||||
|
||||
$this->middleware->add('foo');
|
||||
$this->assertEquals(3, count($this->middleware->all()));
|
||||
$this->middleware->add(function () {
|
||||
});
|
||||
$this->middleware->add(function () {
|
||||
});
|
||||
$this->assertEquals(5, count($this->middleware->all()));
|
||||
}
|
||||
|
||||
public function testPipelineAndEnd()
|
||||
{
|
||||
$bar = m::mock("overload:BarMiddleware");
|
||||
$foo = m::mock("overload:FooMiddleware", Foo::class);
|
||||
|
||||
$request = m::mock(Request::class);
|
||||
$response = m::mock(Response::class);
|
||||
|
||||
$e = new Exception();
|
||||
|
||||
$handle = m::mock(Handle::class);
|
||||
$handle->shouldReceive('report')->with($e)->andReturnNull();
|
||||
$handle->shouldReceive('render')->with($request, $e)->andReturn($response);
|
||||
|
||||
$foo->shouldReceive('handle')->once()->andReturnUsing(function ($request, $next) {
|
||||
return $next($request);
|
||||
});
|
||||
$bar->shouldReceive('handle')->once()->andReturnUsing(function ($request, $next) use ($e) {
|
||||
$next($request);
|
||||
throw $e;
|
||||
});
|
||||
|
||||
$foo->shouldReceive('end')->once()->with($response)->andReturnNull();
|
||||
|
||||
$this->app->shouldReceive('make')->with(Handle::class)->andReturn($handle);
|
||||
|
||||
$this->config->shouldReceive('get')->once()->with('middleware.priority', [])
|
||||
->andReturn(['FooMiddleware', 'BarMiddleware']);
|
||||
|
||||
$this->middleware->import([function ($request, $next) {
|
||||
return $next($request);
|
||||
}, 'BarMiddleware', 'FooMiddleware']);
|
||||
|
||||
$this->assertInstanceOf(Pipeline::class, $pipeline = $this->middleware->pipeline());
|
||||
|
||||
$pipeline->send($request)->then(function ($request) use ($e, $response) {
|
||||
throw $e;
|
||||
});
|
||||
|
||||
$this->middleware->end($response);
|
||||
}
|
||||
}
|
||||
|
||||
class Foo
|
||||
{
|
||||
public function end(Response $response)
|
||||
{
|
||||
}
|
||||
}
|
||||
263
vendor/topthink/framework/tests/RouteTest.php
vendored
Normal file
263
vendor/topthink/framework/tests/RouteTest.php
vendored
Normal file
@@ -0,0 +1,263 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Closure;
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\exception\RouteNotFoundException;
|
||||
use think\helper\Str;
|
||||
use think\Request;
|
||||
use think\response\Redirect;
|
||||
use think\response\View;
|
||||
use think\Route;
|
||||
|
||||
class RouteTest extends TestCase
|
||||
{
|
||||
use InteractsWithApp;
|
||||
|
||||
/** @var Route|MockInterface */
|
||||
protected $route;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->prepareApp();
|
||||
|
||||
$this->config->shouldReceive('get')->with('route')->andReturn(['url_route_must' => true]);
|
||||
$this->route = new Route($this->app);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param string $method
|
||||
* @param string $host
|
||||
* @return m\Mock|Request
|
||||
*/
|
||||
protected function makeRequest($path, $method = 'GET', $host = 'localhost')
|
||||
{
|
||||
$request = m::mock(Request::class)->makePartial();
|
||||
$request->shouldReceive('host')->andReturn($host);
|
||||
$request->shouldReceive('pathinfo')->andReturn($path);
|
||||
$request->shouldReceive('url')->andReturn('/' . $path);
|
||||
$request->shouldReceive('method')->andReturn(strtoupper($method));
|
||||
return $request;
|
||||
}
|
||||
|
||||
public function testSimpleRequest()
|
||||
{
|
||||
$this->route->get('foo', function () {
|
||||
return 'get-foo';
|
||||
});
|
||||
|
||||
$this->route->put('foo', function () {
|
||||
return 'put-foo';
|
||||
});
|
||||
|
||||
$this->route->group(function () {
|
||||
$this->route->post('foo', function () {
|
||||
return 'post-foo';
|
||||
});
|
||||
});
|
||||
|
||||
$request = $this->makeRequest('foo', 'post');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals(200, $response->getCode());
|
||||
$this->assertEquals('post-foo', $response->getContent());
|
||||
|
||||
$request = $this->makeRequest('foo', 'get');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals(200, $response->getCode());
|
||||
$this->assertEquals('get-foo', $response->getContent());
|
||||
}
|
||||
|
||||
// public function testOptionsRequest()
|
||||
// {
|
||||
// $this->route->get('foo', function () {
|
||||
// return 'get-foo';
|
||||
// });
|
||||
//
|
||||
// $this->route->put('foo', function () {
|
||||
// return 'put-foo';
|
||||
// });
|
||||
//
|
||||
// $this->route->group(function () {
|
||||
// $this->route->post('foo', function () {
|
||||
// return 'post-foo';
|
||||
// });
|
||||
// });
|
||||
// $this->route->group('abc', function () {
|
||||
// $this->route->post('foo/:id', function () {
|
||||
// return 'post-abc-foo';
|
||||
// });
|
||||
// });
|
||||
//
|
||||
// $this->route->post('foo/:id', function () {
|
||||
// return 'post-abc-foo';
|
||||
// });
|
||||
//
|
||||
// $this->route->resource('bar', 'SomeClass');
|
||||
//
|
||||
// $request = $this->makeRequest('foo', 'options');
|
||||
// $response = $this->route->dispatch($request);
|
||||
// $this->assertEquals(204, $response->getCode());
|
||||
// $this->assertEquals('GET, PUT, POST', $response->getHeader('Allow'));
|
||||
//
|
||||
// $request = $this->makeRequest('bar', 'options');
|
||||
// $response = $this->route->dispatch($request);
|
||||
// $this->assertEquals(204, $response->getCode());
|
||||
// $this->assertEquals('GET, POST', $response->getHeader('Allow'));
|
||||
//
|
||||
// $request = $this->makeRequest('bar/1', 'options');
|
||||
// $response = $this->route->dispatch($request);
|
||||
// $this->assertEquals(204, $response->getCode());
|
||||
// $this->assertEquals('GET, PUT, DELETE', $response->getHeader('Allow'));
|
||||
//
|
||||
// $request = $this->makeRequest('xxxx', 'options');
|
||||
// $response = $this->route->dispatch($request);
|
||||
// $this->assertEquals(204, $response->getCode());
|
||||
// $this->assertEquals('GET, POST, PUT, DELETE', $response->getHeader('Allow'));
|
||||
// }
|
||||
|
||||
public function testAllowCrossDomain()
|
||||
{
|
||||
$this->route->get('foo', function () {
|
||||
return 'get-foo';
|
||||
})->allowCrossDomain(['some' => 'bar']);
|
||||
|
||||
$request = $this->makeRequest('foo', 'get');
|
||||
$response = $this->route->dispatch($request);
|
||||
|
||||
$this->assertEquals('bar', $response->getHeader('some'));
|
||||
$this->assertArrayHasKey('Access-Control-Allow-Credentials', $response->getHeader());
|
||||
|
||||
//$this->expectException(RouteNotFoundException::class);
|
||||
$request = $this->makeRequest('foo2', 'options');
|
||||
$this->route->dispatch($request);
|
||||
}
|
||||
|
||||
public function testControllerDispatch()
|
||||
{
|
||||
$this->route->get('foo', 'foo/bar');
|
||||
|
||||
$controller = m::Mock(\stdClass::class);
|
||||
|
||||
$this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
|
||||
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
|
||||
|
||||
$controller->shouldReceive('bar')->andReturn('bar');
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals('bar', $response->getContent());
|
||||
}
|
||||
|
||||
public function testEmptyControllerDispatch()
|
||||
{
|
||||
$this->route->get('foo', 'foo/bar');
|
||||
|
||||
$controller = m::Mock(\stdClass::class);
|
||||
|
||||
$this->app->shouldReceive('parseClass')->with('controller', 'Error')->andReturn($controller->mockery_getName());
|
||||
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
|
||||
|
||||
$controller->shouldReceive('bar')->andReturn('bar');
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals('bar', $response->getContent());
|
||||
}
|
||||
|
||||
protected function createMiddleware($times = 1)
|
||||
{
|
||||
$middleware = m::mock(Str::random(5));
|
||||
$middleware->shouldReceive('handle')->times($times)->andReturnUsing(function ($request, Closure $next) {
|
||||
return $next($request);
|
||||
});
|
||||
$this->app->shouldReceive('make')->with($middleware->mockery_getName())->andReturn($middleware);
|
||||
|
||||
return $middleware;
|
||||
}
|
||||
|
||||
public function testControllerWithMiddleware()
|
||||
{
|
||||
$this->route->get('foo', 'foo/bar');
|
||||
|
||||
$controller = m::mock(FooClass::class);
|
||||
|
||||
$controller->middleware = [
|
||||
$this->createMiddleware()->mockery_getName() . ":params1:params2",
|
||||
$this->createMiddleware(0)->mockery_getName() => ['except' => 'bar'],
|
||||
$this->createMiddleware()->mockery_getName() => ['only' => 'bar'],
|
||||
[
|
||||
'middleware' => [$this->createMiddleware()->mockery_getName(), [new \stdClass()]],
|
||||
'options' => ['only' => 'bar'],
|
||||
],
|
||||
];
|
||||
|
||||
$this->app->shouldReceive('parseClass')->with('controller', 'Foo')->andReturn($controller->mockery_getName());
|
||||
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
|
||||
|
||||
$controller->shouldReceive('bar')->once()->andReturn('bar');
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals('bar', $response->getContent());
|
||||
}
|
||||
|
||||
public function testRedirectDispatch()
|
||||
{
|
||||
$this->route->redirect('foo', 'http://localhost', 302);
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$this->app->shouldReceive('make')->with(Request::class)->andReturn($request);
|
||||
$response = $this->route->dispatch($request);
|
||||
|
||||
$this->assertInstanceOf(Redirect::class, $response);
|
||||
$this->assertEquals(302, $response->getCode());
|
||||
$this->assertEquals('http://localhost', $response->getData());
|
||||
}
|
||||
|
||||
public function testViewDispatch()
|
||||
{
|
||||
$this->route->view('foo', 'index/hello', ['city' => 'shanghai']);
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$response = $this->route->dispatch($request);
|
||||
|
||||
$this->assertInstanceOf(View::class, $response);
|
||||
$this->assertEquals(['city' => 'shanghai'], $response->getVars());
|
||||
$this->assertEquals('index/hello', $response->getData());
|
||||
}
|
||||
|
||||
public function testDomainBindResponse()
|
||||
{
|
||||
$this->route->domain('test', function () {
|
||||
$this->route->get('/', function () {
|
||||
return 'Hello,ThinkPHP';
|
||||
});
|
||||
});
|
||||
|
||||
$request = $this->makeRequest('', 'get', 'test.domain.com');
|
||||
$response = $this->route->dispatch($request);
|
||||
|
||||
$this->assertEquals('Hello,ThinkPHP', $response->getContent());
|
||||
$this->assertEquals(200, $response->getCode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class FooClass
|
||||
{
|
||||
public $middleware = [];
|
||||
|
||||
public function bar()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
225
vendor/topthink/framework/tests/SessionTest.php
vendored
Normal file
225
vendor/topthink/framework/tests/SessionTest.php
vendored
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\App;
|
||||
use think\cache\Driver;
|
||||
use think\Config;
|
||||
use think\Container;
|
||||
use think\contract\SessionHandlerInterface;
|
||||
use think\helper\Str;
|
||||
use think\Session;
|
||||
use think\session\driver\Cache;
|
||||
use think\session\driver\File;
|
||||
|
||||
class SessionTest extends TestCase
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var Session|MockInterface */
|
||||
protected $session;
|
||||
|
||||
/** @var Config|MockInterface */
|
||||
protected $config;
|
||||
|
||||
protected $handler;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
Container::setInstance($this->app);
|
||||
|
||||
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
|
||||
$this->config = m::mock(Config::class)->makePartial();
|
||||
|
||||
$this->app->shouldReceive('get')->with('config')->andReturn($this->config);
|
||||
$handlerClass = "\\think\\session\\driver\\Test" . Str::random(10);
|
||||
$this->config->shouldReceive("get")->with("session.type", "file")->andReturn($handlerClass);
|
||||
$this->session = new Session($this->app);
|
||||
|
||||
$this->handler = m::mock('overload:' . $handlerClass, SessionHandlerInterface::class);
|
||||
}
|
||||
|
||||
public function testLoadData()
|
||||
{
|
||||
$data = [
|
||||
"bar" => 'foo',
|
||||
];
|
||||
|
||||
$id = md5(uniqid());
|
||||
|
||||
$this->handler->shouldReceive("read")->once()->with($id)->andReturn(serialize($data));
|
||||
|
||||
$this->session->setId($id);
|
||||
$this->session->init();
|
||||
|
||||
$this->assertEquals('foo', $this->session->get('bar'));
|
||||
$this->assertTrue($this->session->has('bar'));
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
|
||||
$this->session->set('foo', 'bar');
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
|
||||
$this->assertEquals('bar', $this->session->pull('foo'));
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
}
|
||||
|
||||
public function testSave()
|
||||
{
|
||||
|
||||
$id = md5(uniqid());
|
||||
|
||||
$this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
|
||||
|
||||
$this->handler->shouldReceive('write')->once()->with($id, serialize([
|
||||
"bar" => 'foo',
|
||||
]))->andReturnTrue();
|
||||
|
||||
$this->session->setId($id);
|
||||
$this->session->init();
|
||||
|
||||
$this->session->set('bar', 'foo');
|
||||
|
||||
$this->session->save();
|
||||
}
|
||||
|
||||
public function testFlash()
|
||||
{
|
||||
$this->session->flash('foo', 'bar');
|
||||
$this->session->flash('bar', 0);
|
||||
$this->session->flash('baz', true);
|
||||
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
$this->assertEquals('bar', $this->session->get('foo'));
|
||||
$this->assertEquals(0, $this->session->get('bar'));
|
||||
$this->assertTrue($this->session->get('baz'));
|
||||
|
||||
$this->session->clearFlashData();
|
||||
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
$this->assertEquals('bar', $this->session->get('foo'));
|
||||
$this->assertEquals(0, $this->session->get('bar'));
|
||||
|
||||
$this->session->clearFlashData();
|
||||
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
$this->assertNull($this->session->get('foo'));
|
||||
|
||||
$this->session->flash('foo', 'bar');
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
$this->session->clearFlashData();
|
||||
$this->session->reflash();
|
||||
$this->session->clearFlashData();
|
||||
|
||||
$this->assertTrue($this->session->has('foo'));
|
||||
}
|
||||
|
||||
public function testClear()
|
||||
{
|
||||
$this->session->set('bar', 'foo');
|
||||
$this->assertEquals('foo', $this->session->get('bar'));
|
||||
$this->session->clear();
|
||||
$this->assertFalse($this->session->has('foo'));
|
||||
}
|
||||
|
||||
public function testSetName()
|
||||
{
|
||||
$this->session->setName('foo');
|
||||
$this->assertEquals('foo', $this->session->getName());
|
||||
}
|
||||
|
||||
public function testDestroy()
|
||||
{
|
||||
$id = md5(uniqid());
|
||||
|
||||
$this->handler->shouldReceive('read')->once()->with($id)->andReturn("");
|
||||
$this->handler->shouldReceive('delete')->once()->with($id)->andReturnTrue();
|
||||
|
||||
$this->session->setId($id);
|
||||
$this->session->init();
|
||||
|
||||
$this->session->set('bar', 'foo');
|
||||
|
||||
$this->session->destroy();
|
||||
|
||||
$this->assertFalse($this->session->has('bar'));
|
||||
|
||||
$this->assertNotEquals($id, $this->session->getId());
|
||||
}
|
||||
|
||||
public function testFileHandler()
|
||||
{
|
||||
$root = vfsStream::setup();
|
||||
|
||||
vfsStream::newFile('bar')
|
||||
->at($root)
|
||||
->lastModified(time());
|
||||
|
||||
vfsStream::newFile('bar')
|
||||
->at(vfsStream::newDirectory("foo")->at($root))
|
||||
->lastModified(100);
|
||||
|
||||
$this->assertTrue($root->hasChild("bar"));
|
||||
$this->assertTrue($root->hasChild("foo/bar"));
|
||||
|
||||
$handler = new TestFileHandle($this->app, [
|
||||
'path' => $root->url(),
|
||||
'gc_probability' => 1,
|
||||
'gc_divisor' => 1,
|
||||
]);
|
||||
|
||||
$this->assertTrue($root->hasChild("bar"));
|
||||
$this->assertFalse($root->hasChild("foo/bar"));
|
||||
|
||||
$id = md5(uniqid());
|
||||
$handler->write($id, "bar");
|
||||
|
||||
$this->assertTrue($root->hasChild("sess_{$id}"));
|
||||
|
||||
$this->assertEquals("bar", $handler->read($id));
|
||||
|
||||
$handler->delete($id);
|
||||
|
||||
$this->assertFalse($root->hasChild("sess_{$id}"));
|
||||
}
|
||||
|
||||
public function testCacheHandler()
|
||||
{
|
||||
$id = md5(uniqid());
|
||||
|
||||
$cache = m::mock(\think\Cache::class);
|
||||
|
||||
$store = m::mock(Driver::class);
|
||||
|
||||
$cache->shouldReceive('store')->once()->with('redis')->andReturn($store);
|
||||
|
||||
$handler = new Cache($cache, ['store' => 'redis']);
|
||||
|
||||
$store->shouldReceive("set")->with($id, "bar", 1440)->once()->andReturnTrue();
|
||||
$handler->write($id, "bar");
|
||||
|
||||
$store->shouldReceive("get")->with($id)->once()->andReturn("bar");
|
||||
$this->assertEquals("bar", $handler->read($id));
|
||||
|
||||
$store->shouldReceive("delete")->with($id)->once()->andReturnTrue();
|
||||
$handler->delete($id);
|
||||
}
|
||||
}
|
||||
|
||||
class TestFileHandle extends File
|
||||
{
|
||||
protected function writeFile($path, $content): bool
|
||||
{
|
||||
return (bool) file_put_contents($path, $content);
|
||||
}
|
||||
}
|
||||
60
vendor/topthink/framework/tests/UrlRouteTest.php
vendored
Normal file
60
vendor/topthink/framework/tests/UrlRouteTest.php
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery as m;
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\Request;
|
||||
use think\Route;
|
||||
|
||||
class UrlRouteTest extends TestCase
|
||||
{
|
||||
use InteractsWithApp;
|
||||
|
||||
/** @var Route|MockInterface */
|
||||
protected $route;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->prepareApp();
|
||||
|
||||
$this->route = new Route($this->app);
|
||||
}
|
||||
|
||||
public function testUrlDispatch()
|
||||
{
|
||||
$controller = m::mock(FooClass::class);
|
||||
$controller->shouldReceive('index')->andReturn('bar');
|
||||
|
||||
$this->app->shouldReceive('parseClass')->once()->with('controller', 'Foo')
|
||||
->andReturn($controller->mockery_getName());
|
||||
$this->app->shouldReceive('make')->with($controller->mockery_getName(), [], true)->andReturn($controller);
|
||||
|
||||
$request = $this->makeRequest('foo');
|
||||
$response = $this->route->dispatch($request);
|
||||
$this->assertEquals('bar', $response->getContent());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $path
|
||||
* @param string $method
|
||||
* @param string $host
|
||||
* @return m\Mock|Request
|
||||
*/
|
||||
protected function makeRequest($path, $method = 'GET', $host = 'localhost')
|
||||
{
|
||||
$request = m::mock(Request::class)->makePartial();
|
||||
$request->shouldReceive('host')->andReturn($host);
|
||||
$request->shouldReceive('pathinfo')->andReturn($path);
|
||||
$request->shouldReceive('url')->andReturn('/' . $path);
|
||||
$request->shouldReceive('method')->andReturn(strtoupper($method));
|
||||
return $request;
|
||||
}
|
||||
|
||||
}
|
||||
122
vendor/topthink/framework/tests/ViewTest.php
vendored
Normal file
122
vendor/topthink/framework/tests/ViewTest.php
vendored
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace think\tests;
|
||||
|
||||
use Mockery\MockInterface;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use think\App;
|
||||
use think\Config;
|
||||
use think\Container;
|
||||
use think\contract\TemplateHandlerInterface;
|
||||
use think\View;
|
||||
use Mockery as m;
|
||||
|
||||
class ViewTest extends TestCase
|
||||
{
|
||||
/** @var App|MockInterface */
|
||||
protected $app;
|
||||
|
||||
/** @var View|MockInterface */
|
||||
protected $view;
|
||||
|
||||
/** @var Config|MockInterface */
|
||||
protected $config;
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->app = m::mock(App::class)->makePartial();
|
||||
Container::setInstance($this->app);
|
||||
|
||||
$this->app->shouldReceive('make')->with(App::class)->andReturn($this->app);
|
||||
$this->config = m::mock(Config::class)->makePartial();
|
||||
$this->app->shouldReceive('get')->with('config')->andReturn($this->config);
|
||||
|
||||
$this->view = new View($this->app);
|
||||
}
|
||||
|
||||
public function testAssignData()
|
||||
{
|
||||
$this->view->assign('foo', 'bar');
|
||||
$this->view->assign(['baz' => 'boom']);
|
||||
$this->view->qux = "corge";
|
||||
|
||||
$this->assertEquals('bar', $this->view->foo);
|
||||
$this->assertEquals('boom', $this->view->baz);
|
||||
$this->assertEquals('corge', $this->view->qux);
|
||||
$this->assertTrue(isset($this->view->qux));
|
||||
}
|
||||
|
||||
public function testRender()
|
||||
{
|
||||
$this->config->shouldReceive("get")->with("view.type", 'php')->andReturn(TestTemplate::class);
|
||||
|
||||
$this->view->filter(function ($content) {
|
||||
return $content;
|
||||
});
|
||||
|
||||
$this->assertEquals("fetch", $this->view->fetch('foo'));
|
||||
$this->assertEquals("display", $this->view->display('foo'));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class TestTemplate implements TemplateHandlerInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* 检测是否存在模板文件
|
||||
* @param string $template 模板文件或者模板规则
|
||||
* @return bool
|
||||
*/
|
||||
public function exists(string $template): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染模板文件
|
||||
* @param string $template 模板文件
|
||||
* @param array $data 模板变量
|
||||
* @return void
|
||||
*/
|
||||
public function fetch(string $template, array $data = []): void
|
||||
{
|
||||
echo "fetch";
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染模板内容
|
||||
* @param string $content 模板内容
|
||||
* @param array $data 模板变量
|
||||
* @return void
|
||||
*/
|
||||
public function display(string $content, array $data = []): void
|
||||
{
|
||||
echo "display";
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置模板引擎
|
||||
* @param array $config 参数
|
||||
* @return void
|
||||
*/
|
||||
public function config(array $config): void
|
||||
{
|
||||
// TODO: Implement config() method.
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取模板引擎配置
|
||||
* @param string $name 参数名
|
||||
* @return mixed
|
||||
*/
|
||||
public function getConfig(string $name)
|
||||
{
|
||||
// TODO: Implement getConfig() method.
|
||||
}
|
||||
}
|
||||
3
vendor/topthink/framework/tests/bootstrap.php
vendored
Normal file
3
vendor/topthink/framework/tests/bootstrap.php
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
<?php
|
||||
|
||||
include __DIR__.'/../src/helper.php';
|
||||
Reference in New Issue
Block a user