From 0e2215dc6c40c67ca720fe4863eaff5624d8ead1 Mon Sep 17 00:00:00 2001 From: Andreas Schempp Date: Mon, 4 Mar 2019 11:08:59 +0100 Subject: [PATCH] Added full unit test coverage --- src/Composer/Util/Zip.php | 2 +- .../Composer/Test/Util/Fixtures/Zip/empty.zip | Bin 0 -> 22 bytes .../Test/Util/Fixtures/Zip/folder.zip | Bin 0 -> 314 bytes .../Test/Util/Fixtures/Zip/multiple.zip | Bin 0 -> 2569 bytes .../Test/Util/Fixtures/Zip/nojson.zip | Bin 0 -> 134 bytes .../Composer/Test/Util/Fixtures/Zip/root.zip | Bin 0 -> 194 bytes .../Test/Util/Fixtures/Zip/subfolder.zip | Bin 0 -> 1328 bytes tests/Composer/Test/Util/ZipTest.php | 117 ++++++++++++++++++ 8 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/empty.zip create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/folder.zip create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/multiple.zip create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/nojson.zip create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/root.zip create mode 100644 tests/Composer/Test/Util/Fixtures/Zip/subfolder.zip create mode 100644 tests/Composer/Test/Util/ZipTest.php diff --git a/src/Composer/Util/Zip.php b/src/Composer/Util/Zip.php index 6698616ff..8c79d106c 100644 --- a/src/Composer/Util/Zip.php +++ b/src/Composer/Util/Zip.php @@ -13,7 +13,7 @@ namespace Composer\Util; /** - * @author Jordi Boggiano + * @author Andreas Schempp */ class Zip { diff --git a/tests/Composer/Test/Util/Fixtures/Zip/empty.zip b/tests/Composer/Test/Util/Fixtures/Zip/empty.zip new file mode 100644 index 0000000000000000000000000000000000000000..15cb0ecb3e219d1701294bfdf0fe3f5cb5d208e7 GIT binary patch literal 22 NcmWIWW@Tf*000g10H*)| literal 0 HcmV?d00001 diff --git a/tests/Composer/Test/Util/Fixtures/Zip/folder.zip b/tests/Composer/Test/Util/Fixtures/Zip/folder.zip new file mode 100644 index 0000000000000000000000000000000000000000..72b17b542f11eaaf47e413832e50a5538eb75e9c GIT binary patch literal 314 zcmWIWW@h1H0D+>Q6hANnO0X~pFr?+@>xV}0Fsyl76SD${zcPw21ORo2FmM22Fq#fQ zsE*|P+=Be#)FQpC;`}_A_B^Qe)z5+$n3nE2awkMpn|0}yKQ(`s98pqT7o`U@n4P0O zuXwu@&;cME;LXS+%8bi#JTSL9ymbUIAx`ChI~AfE;ZS6g1sM>!moyqdb)z{OVid^P T0p6@^AS;-FuoFnn25}ewNhC+U literal 0 HcmV?d00001 diff --git a/tests/Composer/Test/Util/Fixtures/Zip/multiple.zip b/tests/Composer/Test/Util/Fixtures/Zip/multiple.zip new file mode 100644 index 0000000000000000000000000000000000000000..db8c50302d78e7512c043c4598fd62f518af151c GIT binary patch literal 2569 zcmWIWW@h1H0D9J_iOYJ9Y%ed5Eum*72cZaN; zaO1hyoCT}rHJO$_(7EzMR7prBVnNskyOY0^Ds%kJwjF24`F0_sf&IaR+?UBr$ITWk zIBoJHw=et7;;j7DKSRxJuOFOPWPgm;YD-4jPyO)b75;*!*R2!J-G1ZSj^|&WJ^lFQ zWsTOVpKe!P@t|$qy1u1*_P$Z zvM=ps&z`pNhrzl%+Hq%qa`wMc760+NN7~K(FCKsWzf*^O&(yDb<~`i2ATjaWtqr-; zcQZc7?E9#?;6#6G`i~d*(ws90894P&B}z0J}C&6u`A1#~!j)?6D_-u2nA{XGA6^h$g2eBqefhbjzQbzgzyynQ}!*S#@2M9@x2TPq(|s0i6oM2s&*) zb0=Up7d^*_qBz%3A0@y+84i(itoaTZ2)Ly8a(kQTY2Gbh$V+fN$+?4pHGIMMi;A}d zH;DuVs5Vtj`Wn_%pDE+daO1;{_y!(@gT2OflWvH$tTQ{O*dpN6QJcK3h17he(UnQoQ2wV@9g5>*eL1`PU;LD zxtoANhd-%<0s$q>LZTfJj)c>$0xl!a(lBz6QY{TbQX+byKuw8;`WPt@3CQR8j6inDU&pP;Mj(3#VGP1U$c32%iibX+8-tP#vHK0ZDC0pg@vUPu z6C{8UMIyv|pt1}(ph2b}ms*lYrYvdPie?I0I^lp9kH{g0p0ZILrp1ixFi@t2I1H4u zksSsq`m&J915p%*rJ$LDk`iFK7THngDFNBIzmAzKNGSmnjff%zJyl?jP6ZU#t-)m+ zTC}1%6k;+c^pG>h~n5SP$Mynf|UUQ-mGk({K^Z2 L*MQ+J3+4d;JZ$R> literal 0 HcmV?d00001 diff --git a/tests/Composer/Test/Util/Fixtures/Zip/nojson.zip b/tests/Composer/Test/Util/Fixtures/Zip/nojson.zip new file mode 100644 index 0000000000000000000000000000000000000000..e536b956ce1f7c30410c96e53079a7a272bee0d6 GIT binary patch literal 134 zcmWIWW@h1H0D;-TDSluElwe^HU`Wf)*AI>0VYvOiCgvIte`OS52=HcP5@p7vhX-ba d!&^rX6Ji1f+=KvcRyL40BM{mFX(JGa0RSi#7c&3= literal 0 HcmV?d00001 diff --git a/tests/Composer/Test/Util/Fixtures/Zip/root.zip b/tests/Composer/Test/Util/Fixtures/Zip/root.zip new file mode 100644 index 0000000000000000000000000000000000000000..fd08f4d34dca94997e0a368500e9db6d9458ee46 GIT binary patch literal 194 zcmWIWW@Zs#-~htlpcFp_B*4ocz>u7uTaaIzTBMg%oSzpO!NV~BZB0xb5PxM9VOaev zh=FP8o+EccRJB=`ZuwL5cgYbY)pb#N3<2Kk9QAp{+ogcUfpCB~Ba9<*fOmYO^o~FaN4*AxTAik* z2{)dL%~_!BcW{<YzU)LgyQ-2i#pKZKtBI5c|F|oE_%ktS7iS*FE=|M50!NPqi(^6jtqz$w;lm-d`6t`T9;Jrwua zFSm-5p}cqtKcnZLNQ*=1`#}c0c612b^@tNyd=1r@oS$2eUz}Q`msOmf2TR3y zZy|xW`dJVI)6zXh?u4jnvo788r{?dHBTB04qV&KHt + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Util; + +use Composer\Util\Zip; +use PHPUnit\Framework\TestCase; + +/** + * @author Andreas Schempp + */ +class ZipTest extends TestCase +{ + public function testThrowsExceptionIfZipExcentionIsNotLoaded() + { + if (extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is loaded.'); + } + + $this->setExpectedException('\RuntimeException', 'The Zip Util requires PHP\'s zip extension'); + + Zip::getComposerJson(''); + } + + public function testReturnsNullifTheZipIsNotFound() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip'); + + $this->assertNull($result); + } + + public function testReturnsNullIfTheZipIsEmpty() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip'); + + $this->assertNull($result); + } + + public function testReturnsNullIfTheZipHasNoComposerJson() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/nojson.zip'); + + $this->assertNull($result); + } + + public function testReturnsNullIfTheComposerJsonIsInASubSubfolder() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/subfolder.zip'); + + $this->assertNull($result); + } + + public function testReturnsComposerJsonInZipRoot() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip'); + + $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result); + } + + public function testReturnsComposerJsonInFirstFolder() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip'); + + $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result); + } + + public function testReturnsRootComposerJsonAndSkipsSubfolders() + { + if (!extension_loaded('zip')) { + $this->markTestSkipped('The PHP zip extension is not loaded.'); + return; + } + + $result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple.zip'); + + $this->assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result); + } +}