ComplexityCollectionTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/complexity.
  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\Complexity;
  11. use PHPUnit\Framework\TestCase;
  12. /**
  13. * @covers \SebastianBergmann\Complexity\ComplexityCollection
  14. * @covers \SebastianBergmann\Complexity\ComplexityCollectionIterator
  15. *
  16. * @uses \SebastianBergmann\Complexity\Complexity
  17. *
  18. * @small
  19. */
  20. final class ComplexityCollectionTest extends TestCase
  21. {
  22. /**
  23. * @psalm-var list<Complexity>
  24. */
  25. private $array;
  26. protected function setUp(): void
  27. {
  28. $this->array = [
  29. new Complexity('Class::method', 1),
  30. new Complexity('function', 2),
  31. ];
  32. }
  33. /**
  34. * @testdox Can be created from list of Complexity objects
  35. */
  36. public function testCanBeCreatedFromListOfObjects(): void
  37. {
  38. $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);
  39. $this->assertSame($this->array, $collection->asArray());
  40. }
  41. public function testCanBeCounted(): void
  42. {
  43. $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);
  44. $this->assertCount(2, $collection);
  45. $this->assertFalse($collection->isEmpty());
  46. }
  47. public function testCanBeIterated(): void
  48. {
  49. $array = [];
  50. foreach (ComplexityCollection::fromList($this->array[0], $this->array[1]) as $key => $value) {
  51. $array[$key] = $value;
  52. }
  53. $this->assertCount(2, $array);
  54. $this->assertArrayHasKey(0, $array);
  55. $this->assertSame($this->array[0], $array[0]);
  56. $this->assertArrayHasKey(1, $array);
  57. $this->assertSame($this->array[1], $array[1]);
  58. }
  59. public function testHasCyclomaticComplexity(): void
  60. {
  61. $collection = ComplexityCollection::fromList($this->array[0], $this->array[1]);
  62. $this->assertSame(3, $collection->cyclomaticComplexity());
  63. }
  64. }