ZipTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\Zip;
  13. use Composer\Test\TestCase;
  14. /**
  15. * @author Andreas Schempp <andreas.schempp@terminal42.ch>
  16. */
  17. class ZipTest extends TestCase
  18. {
  19. public function testThrowsExceptionIfZipExcentionIsNotLoaded()
  20. {
  21. if (extension_loaded('zip')) {
  22. $this->markTestSkipped('The PHP zip extension is loaded.');
  23. }
  24. $this->setExpectedException('\RuntimeException', 'The Zip Util requires PHP\'s zip extension');
  25. Zip::getComposerJson('');
  26. }
  27. public function testReturnsNullifTheZipIsNotFound()
  28. {
  29. if (!extension_loaded('zip')) {
  30. $this->markTestSkipped('The PHP zip extension is not loaded.');
  31. return;
  32. }
  33. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip');
  34. $this->assertNull($result);
  35. }
  36. public function testReturnsNullIfTheZipIsEmpty()
  37. {
  38. if (!extension_loaded('zip')) {
  39. $this->markTestSkipped('The PHP zip extension is not loaded.');
  40. return;
  41. }
  42. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip');
  43. $this->assertNull($result);
  44. }
  45. public function testReturnsNullIfTheZipHasNoComposerJson()
  46. {
  47. if (!extension_loaded('zip')) {
  48. $this->markTestSkipped('The PHP zip extension is not loaded.');
  49. return;
  50. }
  51. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/nojson.zip');
  52. $this->assertNull($result);
  53. }
  54. public function testReturnsNullIfTheComposerJsonIsInASubSubfolder()
  55. {
  56. if (!extension_loaded('zip')) {
  57. $this->markTestSkipped('The PHP zip extension is not loaded.');
  58. return;
  59. }
  60. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/subfolder.zip');
  61. $this->assertNull($result);
  62. }
  63. public function testReturnsComposerJsonInZipRoot()
  64. {
  65. if (!extension_loaded('zip')) {
  66. $this->markTestSkipped('The PHP zip extension is not loaded.');
  67. return;
  68. }
  69. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip');
  70. $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
  71. }
  72. public function testReturnsComposerJsonInFirstFolder()
  73. {
  74. if (!extension_loaded('zip')) {
  75. $this->markTestSkipped('The PHP zip extension is not loaded.');
  76. return;
  77. }
  78. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip');
  79. $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
  80. }
  81. public function testReturnsRootComposerJsonAndSkipsSubfolders()
  82. {
  83. if (!extension_loaded('zip')) {
  84. $this->markTestSkipped('The PHP zip extension is not loaded.');
  85. return;
  86. }
  87. $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple.zip');
  88. $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
  89. }
  90. }