BuilderTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace AlexLcDee\RegistryContainer\Tests;
  3. use AlexLcDee\RegistryContainer\Builder;
  4. use AlexLcDee\RegistryContainer\Exceptions\InvalidArgumentException;
  5. use AlexLcDee\RegistryContainer\Interfaces\RegistryContainerInterface;
  6. use AlexLcDee\RegistryContainer\Tests\Mocks\Registry as MockRegistry;
  7. use PHPUnit\Framework\TestCase;
  8. class BuilderTest extends TestCase
  9. {
  10. /**
  11. * @var Builder
  12. */
  13. private $builder;
  14. public function setUp()
  15. {
  16. $this->builder = Builder::create();
  17. }
  18. public function test_create()
  19. {
  20. $this->assertInstanceOf(Builder::class, $this->builder);
  21. }
  22. public function test_build()
  23. {
  24. $this->assertInstanceOf(RegistryContainerInterface::class, $this->builder->build());
  25. }
  26. public function test_withRegistryClass()
  27. {
  28. $registry = $this->builder->withRegistryClass(MockRegistry::class)->build();
  29. $this->assertInstanceOf(MockRegistry::class, $registry);
  30. }
  31. public function test_withRegistryClass_throws_InvalidArgumentException()
  32. {
  33. $this->expectException(InvalidArgumentException::class);
  34. $this->builder->withRegistryClass(\stdClass::class);
  35. }
  36. public function test_withRegistryClass_returns_cloned()
  37. {
  38. $this->assertNotSame($this->builder, $this->builder->withRegistryClass(MockRegistry::class));
  39. }
  40. public function test_withTypesMap()
  41. {
  42. /** @var MockRegistry $registry */
  43. $registry = $this->builder
  44. ->withRegistryClass(MockRegistry::class)
  45. ->withTypesMap(['test' => 'test'])
  46. ->build();
  47. $this->assertArrayHasKey('test', $registry->getTypesMap());
  48. }
  49. public function test_withTypesMap_rewrites_map()
  50. {
  51. /** @var MockRegistry $registry */
  52. $registry = $this->builder
  53. ->withRegistryClass(MockRegistry::class)
  54. ->withTypesMap(['test' => 'test'])
  55. ->withTypesMap(['test2' => 'test2'])
  56. ->build();
  57. $this->assertArrayHasKey('test2', $registry->getTypesMap());
  58. $this->assertArrayNotHasKey('test', $registry->getTypesMap());
  59. }
  60. public function test_withTypesMap_merges_maps()
  61. {
  62. /** @var MockRegistry $registry */
  63. $registry = $this->builder
  64. ->withRegistryClass(MockRegistry::class)
  65. ->withTypesMap(['test' => 'test'])
  66. ->withTypesMap(['test2' => 'test2'], true)
  67. ->build();
  68. $this->assertArrayHasKey('test', $registry->getTypesMap());
  69. $this->assertArrayHasKey('test2', $registry->getTypesMap());
  70. }
  71. public function test_withTypesMap_returns_cloned()
  72. {
  73. $this->assertNotSame($this->builder, $this->builder->withTypesMap(['test2' => 'test2']));
  74. }
  75. }