AbstractBlockTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
  9. * - (c) John MacFarlane
  10. *
  11. * For the full copyright and license information, please view the LICENSE
  12. * file that was distributed with this source code.
  13. */
  14. namespace League\CommonMark\Tests\Unit\Node\Block;
  15. use League\CommonMark\Exception\InvalidArgumentException;
  16. use League\CommonMark\Node\Block\AbstractBlock;
  17. use League\CommonMark\Node\Inline\AbstractInline;
  18. use PHPUnit\Framework\TestCase;
  19. final class AbstractBlockTest extends TestCase
  20. {
  21. public function testSetParent(): void
  22. {
  23. $block = $this->getMockForAbstractClass(AbstractBlock::class);
  24. $parent = $this->createMock(AbstractBlock::class);
  25. self::getMethod('setParent')->invoke($block, $parent);
  26. $this->assertSame($parent, $block->parent());
  27. self::getMethod('setParent')->invoke($block, null);
  28. $this->assertNull($block->parent());
  29. }
  30. public function testSetParentWithInvalidNode(): void
  31. {
  32. $this->expectException(InvalidArgumentException::class);
  33. $block = $this->getMockForAbstractClass(AbstractBlock::class);
  34. $inline = $this->getMockForAbstractClass(AbstractInline::class);
  35. self::getMethod('setParent')->invoke($block, $inline);
  36. }
  37. public function testGetStartLine(): void
  38. {
  39. $block = $this->getMockForAbstractClass(AbstractBlock::class);
  40. self::getProperty('startLine')->setValue($block, 42);
  41. $this->assertEquals(42, $block->getStartLine());
  42. }
  43. public function testGetSetEndLine(): void
  44. {
  45. $block = $this->getMockForAbstractClass(AbstractBlock::class);
  46. $block->setEndLine(42);
  47. $this->assertEquals(42, $block->getEndLine());
  48. }
  49. private static function getMethod(string $name): \ReflectionMethod
  50. {
  51. $class = new \ReflectionClass(AbstractBlock::class);
  52. $method = $class->getMethod($name);
  53. $method->setAccessible(true);
  54. return $method;
  55. }
  56. private static function getProperty(string $name): \ReflectionProperty
  57. {
  58. $class = new \ReflectionClass(AbstractBlock::class);
  59. $property = $class->getProperty($name);
  60. $property->setAccessible(true);
  61. return $property;
  62. }
  63. }