ManifestDocumentTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace PharIo\Manifest;
  3. class ManifestDocumentTest extends \PHPUnit\Framework\TestCase {
  4. public function testThrowsExceptionWhenFileDoesNotExist() {
  5. $this->expectException(ManifestDocumentException::class);
  6. ManifestDocument::fromFile('/does/not/exist');
  7. }
  8. public function testCanBeCreatedFromFile() {
  9. $this->assertInstanceOf(
  10. ManifestDocument::class,
  11. ManifestDocument::fromFile(__DIR__ . '/../_fixture/phpunit-5.6.5.xml')
  12. );
  13. }
  14. public function testCaneBeConstructedFromString() {
  15. $content = file_get_contents(__DIR__ . '/../_fixture/phpunit-5.6.5.xml');
  16. $this->assertInstanceOf(
  17. ManifestDocument::class,
  18. ManifestDocument::fromString($content)
  19. );
  20. }
  21. public function testThrowsExceptionOnInvalidXML() {
  22. $this->expectException(ManifestDocumentLoadingException::class);
  23. ManifestDocument::fromString('<?xml version="1.0" ?><root>');
  24. }
  25. public function testLoadingDocumentWithWrongRootNameThrowsException() {
  26. $this->expectException(ManifestDocumentException::class);
  27. ManifestDocument::fromString('<?xml version="1.0" ?><root />');
  28. }
  29. public function testLoadingDocumentWithWrongNamespaceThrowsException() {
  30. $this->expectException(ManifestDocumentException::class);
  31. ManifestDocument::fromString('<?xml version="1.0" ?><phar xmlns="foo:bar" />');
  32. }
  33. public function testContainsElementCanBeRetrieved() {
  34. $this->assertInstanceOf(
  35. ContainsElement::class,
  36. $this->loadFixture()->getContainsElement()
  37. );
  38. }
  39. public function testRequiresElementCanBeRetrieved() {
  40. $this->assertInstanceOf(
  41. RequiresElement::class,
  42. $this->loadFixture()->getRequiresElement()
  43. );
  44. }
  45. public function testCopyrightElementCanBeRetrieved() {
  46. $this->assertInstanceOf(
  47. CopyrightElement::class,
  48. $this->loadFixture()->getCopyrightElement()
  49. );
  50. }
  51. public function testBundlesElementCanBeRetrieved() {
  52. $this->assertInstanceOf(
  53. BundlesElement::class,
  54. $this->loadFixture()->getBundlesElement()
  55. );
  56. }
  57. public function testThrowsExceptionWhenContainsIsMissing() {
  58. $this->expectException(ManifestDocumentException::class);
  59. $this->loadEmptyFixture()->getContainsElement();
  60. }
  61. public function testThrowsExceptionWhenCopyirhgtIsMissing() {
  62. $this->expectException(ManifestDocumentException::class);
  63. $this->loadEmptyFixture()->getCopyrightElement();
  64. }
  65. public function testThrowsExceptionWhenRequiresIsMissing() {
  66. $this->expectException(ManifestDocumentException::class);
  67. $this->loadEmptyFixture()->getRequiresElement();
  68. }
  69. public function testThrowsExceptionWhenBundlesIsMissing() {
  70. $this->expectException(ManifestDocumentException::class);
  71. $this->loadEmptyFixture()->getBundlesElement();
  72. }
  73. public function testHasBundlesReturnsTrueWhenBundlesNodeIsPresent() {
  74. $this->assertTrue(
  75. $this->loadFixture()->hasBundlesElement()
  76. );
  77. }
  78. public function testHasBundlesReturnsFalseWhenBundlesNoNodeIsPresent() {
  79. $this->assertFalse(
  80. $this->loadEmptyFixture()->hasBundlesElement()
  81. );
  82. }
  83. private function loadFixture() {
  84. return ManifestDocument::fromFile(__DIR__ . '/../_fixture/phpunit-5.6.5.xml');
  85. }
  86. private function loadEmptyFixture() {
  87. return ManifestDocument::fromString(
  88. '<?xml version="1.0" ?><phar xmlns="https://phar.io/xml/manifest/1.0" />'
  89. );
  90. }
  91. }