AppTest.php 5.7 KB

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