1
0
Fork 0
composer/tests/Composer/Test/Util/ZipTest.php

125 lines
3.9 KiB
PHP
Raw Normal View History

2022-02-23 15:58:18 +00:00
<?php declare(strict_types=1);
2019-03-04 10:08:59 +00:00
/*
* 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
{
public function testThrowsExceptionIfZipExtensionIsNotLoaded(): void
2019-03-04 10:08:59 +00:00
{
if (extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is loaded.');
}
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('The Zip Util requires PHP\'s zip extension');
2019-03-04 10:08:59 +00:00
Zip::getComposerJson('');
}
public function testReturnsNullifTheZipIsNotFound(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/invalid.zip');
self::assertNull($result);
2019-03-04 10:08:59 +00:00
}
public function testReturnsNullIfTheZipIsEmpty(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/empty.zip');
self::assertNull($result);
2019-03-04 10:08:59 +00:00
}
public function testThrowsExceptionIfTheZipHasNoComposerJson(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('No composer.json found either at the top level or within the topmost directory');
Zip::getComposerJson(__DIR__.'/Fixtures/Zip/nojson.zip');
2019-03-04 10:08:59 +00:00
}
public function testThrowsExceptionIfTheComposerJsonIsInASubSubfolder(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('No composer.json found either at the top level or within the topmost directory');
Zip::getComposerJson(__DIR__.'/Fixtures/Zip/subfolders.zip');
2019-03-04 10:08:59 +00:00
}
public function testReturnsComposerJsonInZipRoot(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/root.zip');
self::assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
2019-03-04 10:08:59 +00:00
}
public function testReturnsComposerJsonInFirstFolder(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/folder.zip');
self::assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
2019-03-04 10:08:59 +00:00
}
public function testMultipleTopLevelDirsIsInvalid(): void
2019-03-04 10:08:59 +00:00
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
self::expectExceptionMessage('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/');
Zip::getComposerJson(__DIR__.'/Fixtures/Zip/multiple.zip');
}
public function testReturnsComposerJsonFromFirstSubfolder(): void
{
if (!extension_loaded('zip')) {
$this->markTestSkipped('The PHP zip extension is not loaded.');
}
$result = Zip::getComposerJson(__DIR__.'/Fixtures/Zip/single-sub.zip');
self::assertEquals("{\n \"name\": \"foo/bar\"\n}\n", $result);
2019-03-04 10:08:59 +00:00
}
}