AppTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace think\tests;
  3. use Mockery as m;
  4. use org\bovigo\vfs\vfsStream;
  5. use org\bovigo\vfs\vfsStreamDirectory;
  6. use PHPUnit\Framework\TestCase;
  7. use stdClass;
  8. use think\App;
  9. use think\Env;
  10. use think\Event;
  11. use think\exception\ClassNotFoundException;
  12. use think\Service;
  13. class SomeService extends Service
  14. {
  15. public $bind = [
  16. 'some' => 'class',
  17. ];
  18. public function register()
  19. {
  20. }
  21. public function boot()
  22. {
  23. }
  24. }
  25. /**
  26. * @property array initializers
  27. */
  28. class AppTest extends TestCase
  29. {
  30. /** @var App */
  31. protected $app;
  32. protected function setUp()
  33. {
  34. $this->app = new App();
  35. }
  36. protected function tearDown(): void
  37. {
  38. m::close();
  39. }
  40. public function testService()
  41. {
  42. $this->app->register(stdClass::class);
  43. $this->assertInstanceOf(stdClass::class, $this->app->getService(stdClass::class));
  44. $service = m::mock(SomeService::class);
  45. $service->shouldReceive('register')->once();
  46. $this->app->register($service);
  47. $this->assertEquals($service, $this->app->getService(SomeService::class));
  48. $service2 = m::mock(SomeService::class);
  49. $service2->shouldReceive('register')->once();
  50. $this->app->register($service2);
  51. $this->assertEquals($service, $this->app->getService(SomeService::class));
  52. $this->app->register($service2, true);
  53. $this->assertEquals($service2, $this->app->getService(SomeService::class));
  54. $service->shouldReceive('boot')->once();
  55. $service2->shouldReceive('boot')->once();
  56. $this->app->boot();
  57. }
  58. public function testDebug()
  59. {
  60. $this->app->debug(false);
  61. $this->assertFalse($this->app->isDebug());
  62. $this->app->debug(true);
  63. $this->assertTrue($this->app->isDebug());
  64. }
  65. public function testNamespace()
  66. {
  67. $namespace = 'test';
  68. $this->app->setNamespace($namespace);
  69. $this->assertEquals($namespace, $this->app->getNamespace());
  70. }
  71. public function testVersion()
  72. {
  73. $this->assertEquals(App::VERSION, $this->app->version());
  74. }
  75. public function testPath()
  76. {
  77. $rootPath = __DIR__ . DIRECTORY_SEPARATOR;
  78. $app = new App($rootPath);
  79. $this->assertEquals($rootPath, $app->getRootPath());
  80. $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath());
  81. $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath());
  82. $appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
  83. $app->setAppPath($appPath);
  84. $this->assertEquals($appPath, $app->getAppPath());
  85. $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath());
  86. $this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath());
  87. $this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath());
  88. $runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
  89. $app->setRuntimePath($runtimePath);
  90. $this->assertEquals($runtimePath, $app->getRuntimePath());
  91. }
  92. /**
  93. * @param vfsStreamDirectory $root
  94. * @param bool $debug
  95. * @return App
  96. */
  97. protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true)
  98. {
  99. $rootPath = $root->url() . DIRECTORY_SEPARATOR;
  100. $app = new App($rootPath);
  101. $initializer = m::mock();
  102. $initializer->shouldReceive('init')->once()->with($app);
  103. $app->instance($initializer->mockery_getName(), $initializer);
  104. (function () use ($initializer) {
  105. $this->initializers = [$initializer->mockery_getName()];
  106. })->call($app);
  107. $env = m::mock(Env::class);
  108. $env->shouldReceive('load')->once()->with($rootPath . '.env');
  109. $env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php');
  110. $env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug);
  111. $event = m::mock(Event::class);
  112. $event->shouldReceive('trigger')->once()->with('AppInit');
  113. $event->shouldReceive('bind')->once()->with([]);
  114. $event->shouldReceive('listenEvents')->once()->with([]);
  115. $event->shouldReceive('subscribe')->once()->with([]);
  116. $app->instance('env', $env);
  117. $app->instance('event', $event);
  118. return $app;
  119. }
  120. public function testInitialize()
  121. {
  122. $root = vfsStream::setup('rootDir', null, [
  123. '.env' => '',
  124. 'app' => [
  125. 'common.php' => '',
  126. 'event.php' => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
  127. 'provider.php' => '<?php return [];',
  128. ],
  129. 'config' => [
  130. 'app.php' => '<?php return [];',
  131. ],
  132. ]);
  133. $app = $this->prepareAppForInitialize($root, true);
  134. $app->debug(false);
  135. $app->initialize();
  136. $this->assertIsInt($app->getBeginMem());
  137. $this->assertIsFloat($app->getBeginTime());
  138. $this->assertTrue($app->initialized());
  139. }
  140. public function testParseName()
  141. {
  142. $this->assertEquals('SomeName', App::parseName('some_name', 1));
  143. $this->assertEquals('someName', App::parseName('some_name', 1, false));
  144. $this->assertEquals('some_name', App::parseName('SomeName'));
  145. $this->assertEquals('some_name', App::parseName('someName'));
  146. }
  147. public function testClassBaseName()
  148. {
  149. $this->assertEquals('stdClass', App::classBaseName(stdClass::class));
  150. $this->assertEquals('App', App::classBaseName($this->app));
  151. }
  152. public function testFactory()
  153. {
  154. $this->assertInstanceOf(stdClass::class, App::factory(stdClass::class));
  155. $this->expectException(ClassNotFoundException::class);
  156. App::factory('SomeClass');
  157. }
  158. public function testParseClass()
  159. {
  160. $this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
  161. $this->app->setNamespace('app2');
  162. $this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
  163. }
  164. }