ServiceProviderTest.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 4.2.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\Core;
  17. use Cake\Core\Container;
  18. use Cake\TestSuite\TestCase;
  19. use LogicException;
  20. use TestApp\ServiceProvider\EmptyServiceProvider;
  21. use TestApp\ServiceProvider\PersonServiceProvider;
  22. /**
  23. * ServiceProviderTest
  24. */
  25. class ServiceProviderTest extends TestCase
  26. {
  27. public function testBootstrapHook(): void
  28. {
  29. $container = new Container();
  30. $container->addServiceProvider(new PersonServiceProvider());
  31. $this->assertTrue(
  32. $container->has('boot'),
  33. 'Should have service defined in bootstrap.'
  34. );
  35. $this->assertSame('boot', $container->get('boot')->name);
  36. }
  37. public function testServicesHook(): void
  38. {
  39. $container = new Container();
  40. $container->addServiceProvider(new PersonServiceProvider());
  41. $this->assertTrue($container->has('sally'), 'Should have service');
  42. $this->assertSame('sally', $container->get('sally')->name);
  43. }
  44. public function testEmptyProvides(): void
  45. {
  46. $this->expectException(LogicException::class);
  47. $provider = new EmptyServiceProvider();
  48. $provider->provides('sally');
  49. }
  50. }