ServiceContainerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. use MediaWiki\Services\ServiceContainer;
  3. /**
  4. * @covers MediaWiki\Services\ServiceContainer
  5. *
  6. * @group MediaWiki
  7. */
  8. class ServiceContainerTest extends PHPUnit\Framework\TestCase {
  9. use MediaWikiCoversValidator;
  10. use PHPUnit4And6Compat;
  11. private function newServiceContainer( $extraArgs = [] ) {
  12. return new ServiceContainer( $extraArgs );
  13. }
  14. public function testGetServiceNames() {
  15. $services = $this->newServiceContainer();
  16. $names = $services->getServiceNames();
  17. $this->assertInternalType( 'array', $names );
  18. $this->assertEmpty( $names );
  19. $name = 'TestService92834576';
  20. $services->defineService( $name, function () {
  21. return null;
  22. } );
  23. $names = $services->getServiceNames();
  24. $this->assertContains( $name, $names );
  25. }
  26. public function testHasService() {
  27. $services = $this->newServiceContainer();
  28. $name = 'TestService92834576';
  29. $this->assertFalse( $services->hasService( $name ) );
  30. $services->defineService( $name, function () {
  31. return null;
  32. } );
  33. $this->assertTrue( $services->hasService( $name ) );
  34. }
  35. public function testGetService() {
  36. $services = $this->newServiceContainer( [ 'Foo' ] );
  37. $theService = new stdClass();
  38. $name = 'TestService92834576';
  39. $count = 0;
  40. $services->defineService(
  41. $name,
  42. function ( $actualLocator, $extra ) use ( $services, $theService, &$count ) {
  43. $count++;
  44. PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
  45. PHPUnit_Framework_Assert::assertSame( $extra, 'Foo' );
  46. return $theService;
  47. }
  48. );
  49. $this->assertSame( $theService, $services->getService( $name ) );
  50. $services->getService( $name );
  51. $this->assertSame( 1, $count, 'instantiator should be called exactly once!' );
  52. }
  53. public function testGetService_fail_unknown() {
  54. $services = $this->newServiceContainer();
  55. $name = 'TestService92834576';
  56. $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
  57. $services->getService( $name );
  58. }
  59. public function testPeekService() {
  60. $services = $this->newServiceContainer();
  61. $services->defineService(
  62. 'Foo',
  63. function () {
  64. return new stdClass();
  65. }
  66. );
  67. $services->defineService(
  68. 'Bar',
  69. function () {
  70. return new stdClass();
  71. }
  72. );
  73. // trigger instantiation of Foo
  74. $services->getService( 'Foo' );
  75. $this->assertInternalType(
  76. 'object',
  77. $services->peekService( 'Foo' ),
  78. 'Peek should return the service object if it had been accessed before.'
  79. );
  80. $this->assertNull(
  81. $services->peekService( 'Bar' ),
  82. 'Peek should return null if the service was never accessed.'
  83. );
  84. }
  85. public function testPeekService_fail_unknown() {
  86. $services = $this->newServiceContainer();
  87. $name = 'TestService92834576';
  88. $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
  89. $services->peekService( $name );
  90. }
  91. public function testDefineService() {
  92. $services = $this->newServiceContainer();
  93. $theService = new stdClass();
  94. $name = 'TestService92834576';
  95. $services->defineService( $name, function ( $actualLocator ) use ( $services, $theService ) {
  96. PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
  97. return $theService;
  98. } );
  99. $this->assertTrue( $services->hasService( $name ) );
  100. $this->assertSame( $theService, $services->getService( $name ) );
  101. }
  102. public function testDefineService_fail_duplicate() {
  103. $services = $this->newServiceContainer();
  104. $theService = new stdClass();
  105. $name = 'TestService92834576';
  106. $services->defineService( $name, function () use ( $theService ) {
  107. return $theService;
  108. } );
  109. $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException::class );
  110. $services->defineService( $name, function () use ( $theService ) {
  111. return $theService;
  112. } );
  113. }
  114. public function testApplyWiring() {
  115. $services = $this->newServiceContainer();
  116. $wiring = [
  117. 'Foo' => function () {
  118. return 'Foo!';
  119. },
  120. 'Bar' => function () {
  121. return 'Bar!';
  122. },
  123. ];
  124. $services->applyWiring( $wiring );
  125. $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
  126. $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
  127. }
  128. public function testImportWiring() {
  129. $services = $this->newServiceContainer();
  130. $wiring = [
  131. 'Foo' => function () {
  132. return 'Foo!';
  133. },
  134. 'Bar' => function () {
  135. return 'Bar!';
  136. },
  137. 'Car' => function () {
  138. return 'FUBAR!';
  139. },
  140. ];
  141. $services->applyWiring( $wiring );
  142. $services->addServiceManipulator( 'Foo', function ( $service ) {
  143. return $service . '+X';
  144. } );
  145. $services->addServiceManipulator( 'Car', function ( $service ) {
  146. return $service . '+X';
  147. } );
  148. $newServices = $this->newServiceContainer();
  149. // create a service with manipulator
  150. $newServices->defineService( 'Foo', function () {
  151. return 'Foo!';
  152. } );
  153. $newServices->addServiceManipulator( 'Foo', function ( $service ) {
  154. return $service . '+Y';
  155. } );
  156. // create a service before importing, so we can later check that
  157. // existing service instances survive importWiring()
  158. $newServices->defineService( 'Car', function () {
  159. return 'Car!';
  160. } );
  161. // force instantiation
  162. $newServices->getService( 'Car' );
  163. // Define another service, so we can later check that extra wiring
  164. // is not lost.
  165. $newServices->defineService( 'Xar', function () {
  166. return 'Xar!';
  167. } );
  168. // import wiring, but skip `Bar`
  169. $newServices->importWiring( $services, [ 'Bar' ] );
  170. $this->assertNotContains( 'Bar', $newServices->getServiceNames(), 'Skip `Bar` service' );
  171. $this->assertSame( 'Foo!+Y+X', $newServices->getService( 'Foo' ) );
  172. // import all wiring, but preserve existing service instance
  173. $newServices->importWiring( $services );
  174. $this->assertContains( 'Bar', $newServices->getServiceNames(), 'Import all services' );
  175. $this->assertSame( 'Bar!', $newServices->getService( 'Bar' ) );
  176. $this->assertSame( 'Car!', $newServices->getService( 'Car' ), 'Use existing service instance' );
  177. $this->assertSame( 'Xar!', $newServices->getService( 'Xar' ), 'Predefined services are kept' );
  178. }
  179. public function testLoadWiringFiles() {
  180. $services = $this->newServiceContainer();
  181. $wiringFiles = [
  182. __DIR__ . '/TestWiring1.php',
  183. __DIR__ . '/TestWiring2.php',
  184. ];
  185. $services->loadWiringFiles( $wiringFiles );
  186. $this->assertSame( 'Foo!', $services->getService( 'Foo' ) );
  187. $this->assertSame( 'Bar!', $services->getService( 'Bar' ) );
  188. }
  189. public function testLoadWiringFiles_fail_duplicate() {
  190. $services = $this->newServiceContainer();
  191. $wiringFiles = [
  192. __DIR__ . '/TestWiring1.php',
  193. __DIR__ . '/./TestWiring1.php',
  194. ];
  195. // loading the same file twice should fail, because
  196. $this->setExpectedException( MediaWiki\Services\ServiceAlreadyDefinedException::class );
  197. $services->loadWiringFiles( $wiringFiles );
  198. }
  199. public function testRedefineService() {
  200. $services = $this->newServiceContainer( [ 'Foo' ] );
  201. $theService1 = new stdClass();
  202. $name = 'TestService92834576';
  203. $services->defineService( $name, function () {
  204. PHPUnit_Framework_Assert::fail(
  205. 'The original instantiator function should not get called'
  206. );
  207. } );
  208. // redefine before instantiation
  209. $services->redefineService(
  210. $name,
  211. function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
  212. PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
  213. PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
  214. return $theService1;
  215. }
  216. );
  217. // force instantiation, check result
  218. $this->assertSame( $theService1, $services->getService( $name ) );
  219. }
  220. public function testRedefineService_disabled() {
  221. $services = $this->newServiceContainer( [ 'Foo' ] );
  222. $theService1 = new stdClass();
  223. $name = 'TestService92834576';
  224. $services->defineService( $name, function () {
  225. return 'Foo';
  226. } );
  227. // disable the service. we should be able to redefine it anyway.
  228. $services->disableService( $name );
  229. $services->redefineService( $name, function () use ( $theService1 ) {
  230. return $theService1;
  231. } );
  232. // force instantiation, check result
  233. $this->assertSame( $theService1, $services->getService( $name ) );
  234. }
  235. public function testRedefineService_fail_undefined() {
  236. $services = $this->newServiceContainer();
  237. $theService = new stdClass();
  238. $name = 'TestService92834576';
  239. $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
  240. $services->redefineService( $name, function () use ( $theService ) {
  241. return $theService;
  242. } );
  243. }
  244. public function testRedefineService_fail_in_use() {
  245. $services = $this->newServiceContainer( [ 'Foo' ] );
  246. $theService = new stdClass();
  247. $name = 'TestService92834576';
  248. $services->defineService( $name, function () {
  249. return 'Foo';
  250. } );
  251. // create the service, so it can no longer be redefined
  252. $services->getService( $name );
  253. $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException::class );
  254. $services->redefineService( $name, function () use ( $theService ) {
  255. return $theService;
  256. } );
  257. }
  258. public function testAddServiceManipulator() {
  259. $services = $this->newServiceContainer( [ 'Foo' ] );
  260. $theService1 = new stdClass();
  261. $theService2 = new stdClass();
  262. $name = 'TestService92834576';
  263. $services->defineService(
  264. $name,
  265. function ( $actualLocator, $extra ) use ( $services, $theService1 ) {
  266. PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
  267. PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
  268. return $theService1;
  269. }
  270. );
  271. $services->addServiceManipulator(
  272. $name,
  273. function (
  274. $theService, $actualLocator, $extra
  275. ) use (
  276. $services, $theService1, $theService2
  277. ) {
  278. PHPUnit_Framework_Assert::assertSame( $theService1, $theService );
  279. PHPUnit_Framework_Assert::assertSame( $services, $actualLocator );
  280. PHPUnit_Framework_Assert::assertSame( 'Foo', $extra );
  281. return $theService2;
  282. }
  283. );
  284. // force instantiation, check result
  285. $this->assertSame( $theService2, $services->getService( $name ) );
  286. }
  287. public function testAddServiceManipulator_fail_undefined() {
  288. $services = $this->newServiceContainer();
  289. $theService = new stdClass();
  290. $name = 'TestService92834576';
  291. $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
  292. $services->addServiceManipulator( $name, function () use ( $theService ) {
  293. return $theService;
  294. } );
  295. }
  296. public function testAddServiceManipulator_fail_in_use() {
  297. $services = $this->newServiceContainer( [ 'Foo' ] );
  298. $theService = new stdClass();
  299. $name = 'TestService92834576';
  300. $services->defineService( $name, function () use ( $theService ) {
  301. return $theService;
  302. } );
  303. // create the service, so it can no longer be redefined
  304. $services->getService( $name );
  305. $this->setExpectedException( MediaWiki\Services\CannotReplaceActiveServiceException::class );
  306. $services->addServiceManipulator( $name, function () {
  307. return 'Foo';
  308. } );
  309. }
  310. public function testDisableService() {
  311. $services = $this->newServiceContainer( [ 'Foo' ] );
  312. $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService::class )
  313. ->getMock();
  314. $destructible->expects( $this->once() )
  315. ->method( 'destroy' );
  316. $services->defineService( 'Foo', function () use ( $destructible ) {
  317. return $destructible;
  318. } );
  319. $services->defineService( 'Bar', function () {
  320. return new stdClass();
  321. } );
  322. $services->defineService( 'Qux', function () {
  323. return new stdClass();
  324. } );
  325. // instantiate Foo and Bar services
  326. $services->getService( 'Foo' );
  327. $services->getService( 'Bar' );
  328. // disable service, should call destroy() once.
  329. $services->disableService( 'Foo' );
  330. // disabled service should still be listed
  331. $this->assertContains( 'Foo', $services->getServiceNames() );
  332. // getting other services should still work
  333. $services->getService( 'Bar' );
  334. // disable non-destructible service, and not-yet-instantiated service
  335. $services->disableService( 'Bar' );
  336. $services->disableService( 'Qux' );
  337. $this->assertNull( $services->peekService( 'Bar' ) );
  338. $this->assertNull( $services->peekService( 'Qux' ) );
  339. // disabled service should still be listed
  340. $this->assertContains( 'Bar', $services->getServiceNames() );
  341. $this->assertContains( 'Qux', $services->getServiceNames() );
  342. $this->setExpectedException( MediaWiki\Services\ServiceDisabledException::class );
  343. $services->getService( 'Qux' );
  344. }
  345. public function testDisableService_fail_undefined() {
  346. $services = $this->newServiceContainer();
  347. $theService = new stdClass();
  348. $name = 'TestService92834576';
  349. $this->setExpectedException( MediaWiki\Services\NoSuchServiceException::class );
  350. $services->redefineService( $name, function () use ( $theService ) {
  351. return $theService;
  352. } );
  353. }
  354. public function testDestroy() {
  355. $services = $this->newServiceContainer();
  356. $destructible = $this->getMockBuilder( MediaWiki\Services\DestructibleService::class )
  357. ->getMock();
  358. $destructible->expects( $this->once() )
  359. ->method( 'destroy' );
  360. $services->defineService( 'Foo', function () use ( $destructible ) {
  361. return $destructible;
  362. } );
  363. $services->defineService( 'Bar', function () {
  364. return new stdClass();
  365. } );
  366. // create the service
  367. $services->getService( 'Foo' );
  368. // destroy the container
  369. $services->destroy();
  370. $this->setExpectedException( MediaWiki\Services\ContainerDisabledException::class );
  371. $services->getService( 'Bar' );
  372. }
  373. }