2019-03-04 10:08:59 +00:00
< ? php
/*
* This file is part of Composer .
*
* ( c ) Nils Adermann < naderman @ naderman . de >
* Jordi Boggiano < j . boggiano @ seld . be >
*
* 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 ;
2020-02-07 03:18:45 +00:00
use Composer\Test\TestCase ;
2019-03-04 10:08:59 +00:00
/**
* @ author Andreas Schempp < andreas . schempp @ terminal42 . ch >
*/
class ZipTest extends TestCase
{
2020-07-16 15:00:29 +00:00
public function testThrowsExceptionIfZipExtensionIsNotLoaded ()
2019-03-04 10:08:59 +00:00
{
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 );
}
2020-07-21 15:10:26 +00:00
public function testThrowsExceptionIfTheZipHasNoComposerJson ()
2019-03-04 10:08:59 +00:00
{
if ( ! extension_loaded ( 'zip' )) {
$this -> markTestSkipped ( 'The PHP zip extension is not loaded.' );
return ;
}
2020-09-10 15:21:11 +00:00
$this -> setExpectedException ( '\RuntimeException' , 'No composer.json found either at the top level or within the topmost directory' );
2020-07-21 15:10:26 +00:00
Zip :: getComposerJson ( __DIR__ . '/Fixtures/Zip/nojson.zip' );
2019-03-04 10:08:59 +00:00
}
2020-07-21 15:10:26 +00:00
public function testThrowsExceptionIfTheComposerJsonIsInASubSubfolder ()
2019-03-04 10:08:59 +00:00
{
if ( ! extension_loaded ( 'zip' )) {
$this -> markTestSkipped ( 'The PHP zip extension is not loaded.' );
return ;
}
2020-09-10 15:21:11 +00:00
$this -> setExpectedException ( '\RuntimeException' , 'No composer.json found either at the top level or within the topmost directory' );
2020-07-21 15:10:26 +00:00
Zip :: getComposerJson ( __DIR__ . '/Fixtures/Zip/subfolders.zip' );
2019-03-04 10:08:59 +00:00
}
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 );
}
2020-07-16 15:00:29 +00:00
public function testMultipleTopLevelDirsIsInvalid ()
2019-03-04 10:08:59 +00:00
{
if ( ! extension_loaded ( 'zip' )) {
$this -> markTestSkipped ( 'The PHP zip extension is not loaded.' );
return ;
}
2020-09-10 15:21:11 +00:00
$this -> setExpectedException ( '\RuntimeException' , 'Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: folder1/,folder2/' );
2020-07-21 15:10:26 +00:00
Zip :: getComposerJson ( __DIR__ . '/Fixtures/Zip/multiple.zip' );
2020-07-16 15:00:29 +00:00
}
public function testReturnsComposerJsonFromFirstSubfolder ()
{
if ( ! extension_loaded ( 'zip' )) {
$this -> markTestSkipped ( 'The PHP zip extension is not loaded.' );
return ;
}
$result = Zip :: getComposerJson ( __DIR__ . '/Fixtures/Zip/single-sub.zip' );
2019-03-04 10:08:59 +00:00
$this -> assertEquals ( " { \n \" name \" : \" foo/bar \" \n } \n " , $result );
}
2020-07-21 15:10:26 +00:00
public function testThrowsExceptionIfMultipleComposerInSubFoldersWereFound ()
{
if ( ! extension_loaded ( 'zip' )) {
$this -> markTestSkipped ( 'The PHP zip extension is not loaded.' );
return ;
}
2020-09-10 15:21:11 +00:00
$this -> setExpectedException ( '\RuntimeException' , 'Archive has more than one top level directories, and no composer.json was found on the top level, so it\'s an invalid archive. Top level paths found were: foo3/,__MACOSX/' );
2020-07-21 15:10:26 +00:00
Zip :: getComposerJson ( __DIR__ . '/Fixtures/Zip/multiple_subfolders.zip' );
}
2019-03-04 10:08:59 +00:00
}