From 9681362895d8176fb9bac7591c693a7ef5486e46 Mon Sep 17 00:00:00 2001 From: Gaozhen Ying Date: Fri, 8 Sep 2017 21:00:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9EInstance=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../library/traits/controller/jumpTest.php | 20 +++--- .../library/traits/think/instanceTest.php | 67 +++++++++++++++++++ 2 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 tests/thinkphp/library/traits/think/instanceTest.php diff --git a/tests/thinkphp/library/traits/controller/jumpTest.php b/tests/thinkphp/library/traits/controller/jumpTest.php index 2623c226..47e9dbfe 100644 --- a/tests/thinkphp/library/traits/controller/jumpTest.php +++ b/tests/thinkphp/library/traits/controller/jumpTest.php @@ -52,13 +52,14 @@ class jumpTest extends \PHPUnit_Framework_TestCase try { call_user_func_array([$mock, 'success'], $arguments); + $this->setExpectedException('\think\exception\HttpResponseException'); } catch (\Exception $e) { - $this->assertInstanceOf('\\think\\exception\\HttpResponseException', $e); + $this->assertInstanceOf('\think\exception\HttpResponseException', $e); /** @var Response $response */ $response = $e->getResponse(); - $this->assertInstanceOf('\\Think\\Response', $response); + $this->assertInstanceOf('\Think\Response', $response); $this->assertEquals($expected['header'], $response->getHeader()); $this->assertEquals($expected['data'], $response->getData()); } @@ -78,13 +79,14 @@ class jumpTest extends \PHPUnit_Framework_TestCase try { call_user_func_array([$mock, 'error'], $arguments); + $this->setExpectedException('\think\exception\HttpResponseException'); } catch (\Exception $e) { - $this->assertInstanceOf('\\think\\exception\\HttpResponseException', $e); + $this->assertInstanceOf('\think\exception\HttpResponseException', $e); /** @var Response $response */ $response = $e->getResponse(); - $this->assertInstanceOf('\\Think\\Response', $response); + $this->assertInstanceOf('\Think\Response', $response); $this->assertEquals($expected['header'], $response->getHeader()); $this->assertEquals($expected['data'], $response->getData()); } @@ -104,13 +106,14 @@ class jumpTest extends \PHPUnit_Framework_TestCase try { call_user_func_array([$mock, 'result'], $arguments); + $this->setExpectedException('\think\exception\HttpResponseException'); } catch (\Exception $e) { - $this->assertInstanceOf('\\think\\exception\\HttpResponseException', $e); + $this->assertInstanceOf('\think\exception\HttpResponseException', $e); /** @var Response $response */ $response = $e->getResponse(); - $this->assertInstanceOf('\\Think\\Response', $response); + $this->assertInstanceOf('\Think\Response', $response); $this->assertEquals($expected['header'], $response->getHeader()); $this->assertEquals($expected['data'], $response->getData()); } @@ -123,13 +126,14 @@ class jumpTest extends \PHPUnit_Framework_TestCase { try { call_user_func_array([$this->testClass, 'redirect'], $arguments); + $this->setExpectedException('\think\exception\HttpResponseException'); } catch (\Exception $e) { - $this->assertInstanceOf('\\think\\exception\\HttpResponseException', $e); + $this->assertInstanceOf('\think\exception\HttpResponseException', $e); /** @var Redirect $response */ $response = $e->getResponse(); - $this->assertInstanceOf('\\think\\response\\Redirect', $response); + $this->assertInstanceOf('\think\response\Redirect', $response); $this->assertEquals($expected['url'], $response->getTargetUrl()); $this->assertEquals($expected['code'], $response->getCode()); } diff --git a/tests/thinkphp/library/traits/think/instanceTest.php b/tests/thinkphp/library/traits/think/instanceTest.php new file mode 100644 index 00000000..fae37a09 --- /dev/null +++ b/tests/thinkphp/library/traits/think/instanceTest.php @@ -0,0 +1,67 @@ +assertInstanceOf('\tests\thinkphp\library\traits\think\InstanceTestFather', $father); + $this->assertEquals([], $father->options); + + $father2 = InstanceTestFather::instance(['father']); + $this->assertEquals([], $father2->options); + + $father2->options = ['father']; + $this->assertEquals(['father'], $father->options); + + $son = InstanceTestSon::instance(['son']); + $this->assertInstanceOf('\tests\thinkphp\library\traits\think\InstanceTestFather', $son); + $this->assertEquals(['father'], $son->options); + } + + public function testCallStatic() + { + $father = InstanceTestFather::instance(); + $this->assertEquals([], $father->options); + + $this->assertEquals($father::__protectedStaticFunc(['thinkphp']), 'protectedStaticFunc["thinkphp"]'); + + try { + $father::_protectedStaticFunc(); + $this->setExpectedException('\think\Exception'); + } catch (\Exception $e) { + $this->assertInstanceOf('\think\Exception', $e); + } + } + + protected function tearDown() + { + call_user_func(\Closure::bind(function () { + InstanceTestFather::$instance = null; + }, null, '\tests\thinkphp\library\traits\think\InstanceTestFather')); + } +} + +class InstanceTestFather +{ + use Instance; + + public $options = null; + + public function __construct($options) + { + $this->options = $options; + } + + protected static function _protectedStaticFunc($params) + { + return 'protectedStaticFunc' . json_encode($params); + } +} + +class InstanceTestSon extends InstanceTestFather +{ +}