CodeUnitCollectionTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/code-unit.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\CodeUnit;
  11. use PHPUnit\Framework\TestCase;
  12. use SebastianBergmann\CodeUnit\Fixture\FixtureClass;
  13. use SebastianBergmann\CodeUnit\Fixture\FixtureInterface;
  14. /**
  15. * @covers \SebastianBergmann\CodeUnit\CodeUnitCollection
  16. * @covers \SebastianBergmann\CodeUnit\CodeUnitCollectionIterator
  17. *
  18. * @uses \SebastianBergmann\CodeUnit\CodeUnit
  19. * @uses \SebastianBergmann\CodeUnit\Mapper
  20. *
  21. * @testdox CodeUnitCollection
  22. */
  23. final class CodeUnitCollectionTest extends TestCase
  24. {
  25. /**
  26. * @var InterfaceUnit
  27. */
  28. private $interface;
  29. /**
  30. * @var ClassUnit
  31. */
  32. private $class;
  33. protected function setUp(): void
  34. {
  35. $this->interface = CodeUnit::forInterface(FixtureInterface::class);
  36. $this->class = CodeUnit::forClass(FixtureClass::class);
  37. }
  38. /**
  39. * @testdox Can be created from array of CodeUnit objects
  40. */
  41. public function testCanBeCreatedFromArrayOfObjects(): void
  42. {
  43. $collection = CodeUnitCollection::fromArray([$this->interface, $this->class]);
  44. $this->assertSame([$this->interface, $this->class], $collection->asArray());
  45. }
  46. /**
  47. * @testdox Can be created from list of CodeUnit objects
  48. */
  49. public function testCanBeCreatedFromListOfObjects(): void
  50. {
  51. $collection = CodeUnitCollection::fromList($this->interface, $this->class);
  52. $this->assertSame([$this->interface, $this->class], $collection->asArray());
  53. }
  54. public function testCanBeCounted(): void
  55. {
  56. $collection = CodeUnitCollection::fromList($this->interface, $this->class);
  57. $this->assertCount(2, $collection);
  58. $this->assertFalse($collection->isEmpty());
  59. }
  60. public function testCanBeIterated(): void
  61. {
  62. $array = [];
  63. foreach (CodeUnitCollection::fromList($this->interface, $this->class) as $key => $value) {
  64. $array[$key] = $value;
  65. }
  66. $this->assertCount(2, $array);
  67. $this->assertArrayHasKey(0, $array);
  68. $this->assertSame($this->interface, $array[0]);
  69. $this->assertArrayHasKey(1, $array);
  70. $this->assertSame($this->class, $array[1]);
  71. }
  72. public function testCanBeMergedWithAnotherCollectionOfCodeUnitObjects(): void
  73. {
  74. $this->assertSame(
  75. [
  76. $this->class,
  77. $this->interface,
  78. ],
  79. CodeUnitCollection::fromList($this->class)->mergeWith(
  80. CodeUnitCollection::fromList($this->interface)
  81. )->asArray()
  82. );
  83. }
  84. }