diff --git a/Tests/Converter/NpmPackageUtilTest.php b/Tests/Converter/NpmPackageUtilTest.php index d7fe3b59..e9df7249 100644 --- a/Tests/Converter/NpmPackageUtilTest.php +++ b/Tests/Converter/NpmPackageUtilTest.php @@ -44,6 +44,8 @@ public function getLicenses() array(array('MIT'), array('MIT')), array(array('type' => 'MIT'), array('MIT')), array(array('name' => 'MIT'), array('MIT')), + array(array(array('type' => 'MIT')), array('MIT')), + array(array(array('name' => 'MIT')), array('MIT')), ); } diff --git a/Tests/Fixtures/Repository/Vcs/MockVcsDriverWithException.php b/Tests/Fixtures/Repository/Vcs/MockVcsDriverWithException.php new file mode 100644 index 00000000..6b76176d --- /dev/null +++ b/Tests/Fixtures/Repository/Vcs/MockVcsDriverWithException.php @@ -0,0 +1,30 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs; + +/** + * Mock vcs driver for packages test. + * + * @author François Pluchino + */ +class MockVcsDriverWithException extends MockVcsDriver +{ + /** + * {@inheritdoc} + * + * @throws + */ + public function getTags() + { + throw new \ErrorException('Error to retrieve the tags'); + } +} diff --git a/Tests/Installer/AssetInstallerTest.php b/Tests/Installer/AssetInstallerTest.php index 529a8504..5ecac046 100644 --- a/Tests/Installer/AssetInstallerTest.php +++ b/Tests/Installer/AssetInstallerTest.php @@ -122,7 +122,7 @@ protected function tearDown() $fs = new Filesystem(); $fs->remove(sys_get_temp_dir().'/composer-test-repo-cache'); - $fs->remove(sys_get_temp_dir().'/composer-test/vendor'); + $fs->remove(sys_get_temp_dir().'/composer-test'); } public function testDefaultVendorDir() diff --git a/Tests/Installer/IgnoreFactoryTest.php b/Tests/Installer/IgnoreFactoryTest.php index 0a7fb59e..379cbe5b 100644 --- a/Tests/Installer/IgnoreFactoryTest.php +++ b/Tests/Installer/IgnoreFactoryTest.php @@ -15,6 +15,7 @@ use Composer\Config; use Composer\Package\PackageInterface; use Composer\Package\RootPackageInterface; +use Composer\Util\Filesystem; use Fxp\Composer\AssetPlugin\Config\ConfigBuilder; use Fxp\Composer\AssetPlugin\Installer\IgnoreFactory; use Fxp\Composer\AssetPlugin\Installer\IgnoreManager; @@ -95,6 +96,10 @@ protected function tearDown() $this->config = null; $this->rootPackage = null; $this->package = null; + + $fs = new Filesystem(); + $fs->remove(sys_get_temp_dir().'/composer-test-repo-cache'); + $fs->remove(sys_get_temp_dir().'/composer-test'); } public function testCreateWithoutIgnoreFiles() diff --git a/Tests/Repository/AbstractAssetsRepositoryTest.php b/Tests/Repository/AbstractAssetsRepositoryTest.php index 8ca1e6dd..f7907097 100644 --- a/Tests/Repository/AbstractAssetsRepositoryTest.php +++ b/Tests/Repository/AbstractAssetsRepositoryTest.php @@ -22,6 +22,7 @@ use Fxp\Composer\AssetPlugin\Repository\AssetRepositoryManager; use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository; use Fxp\Composer\AssetPlugin\Repository\VcsPackageFilter; +use Symfony\Component\Filesystem\Filesystem; /** * Abstract class for Tests of assets repository. @@ -72,7 +73,7 @@ protected function setUp() $config->merge(array( 'config' => array( 'home' => sys_get_temp_dir().'/composer-test', - 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-cache-repo', + 'cache-repo-dir' => sys_get_temp_dir().'/composer-test-repo-cache', ), )); /** @var VcsPackageFilter $filter */ @@ -101,6 +102,10 @@ protected function tearDown() $this->rm = null; $this->registry = null; $this->pool = null; + + $fs = new Filesystem(); + $fs->remove(sys_get_temp_dir().'/composer-test-repo-cache'); + $fs->remove(sys_get_temp_dir().'/composer-test'); } public function testFindPackageMustBeAlwaysNull() diff --git a/Tests/Repository/AssetVcsRepositoryTest.php b/Tests/Repository/AssetVcsRepositoryTest.php index 5decd71a..66954cc0 100644 --- a/Tests/Repository/AssetVcsRepositoryTest.php +++ b/Tests/Repository/AssetVcsRepositoryTest.php @@ -201,6 +201,38 @@ public function testSkipParsingFile($type, $url, $class, $verbose) static::assertSame($validTraces, $this->io->getTraces()); } + /** + * Data provider. + * + * @return array + */ + public function getMockDriversWithExceptions() + { + return array( + array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithException'), + array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithException'), + array('npm-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithException'), + array('bower-mock', 'http://example.org/foo', 'Fxp\Composer\AssetPlugin\Tests\Fixtures\Repository\Vcs\MockVcsDriverWithException'), + ); + } + + /** + * @dataProvider getMockDriversWithExceptions + * + * @param string $type + * @param string $url + * @param string $class + * + * @expectedException \ErrorException + * @expectedExceptionMessage Error to retrieve the tags + */ + public function testInitFullDriverWithUncachedException($type, $url, $class) + { + $this->init(true, $type, $url, $class); + + $this->repository->getComposerPackageName(); + } + /** * Data provider. * diff --git a/Tests/Repository/Vcs/GitDriverTest.php b/Tests/Repository/Vcs/GitDriverTest.php index 48cd2120..b0a3568d 100644 --- a/Tests/Repository/Vcs/GitDriverTest.php +++ b/Tests/Repository/Vcs/GitDriverTest.php @@ -39,15 +39,7 @@ final class GitDriverTest extends \PHPUnit\Framework\TestCase protected function setUp() { - $assetConfig = new \Fxp\Composer\AssetPlugin\Config\Config(array('git-skip-update' => '1 hour')); - - /* @var AssetRepositoryManager|\PHPUnit_Framework_MockObject_MockObject $arm */ $this->assetRepositoryManager = $this->getMockBuilder(AssetRepositoryManager::class)->disableOriginalConstructor()->getMock(); - $this->assetRepositoryManager->expects(static::any()) - ->method('getConfig') - ->willReturn($assetConfig) - ; - $this->config = new Config(); $this->config->merge(array( 'config' => array( @@ -66,6 +58,11 @@ protected function setUp() protected function tearDown() { $fs = new Filesystem(); + + if (file_exists(sys_get_temp_dir().'/composer-test-cache')) { + chmod(sys_get_temp_dir().'/composer-test-cache', 0777); + } + $fs->removeDirectory(sys_get_temp_dir().'/composer-test'); $fs->removeDirectory(sys_get_temp_dir().'/composer-test-cache'); } @@ -86,6 +83,11 @@ public function getAssetTypes() */ public function testPublicRepositoryWithEmptyComposer($type, $filename) { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin'; $identifier = 'v0.0.0'; $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); @@ -121,6 +123,58 @@ public function testPublicRepositoryWithEmptyComposer($type, $filename) static::assertSame($validEmpty, $gitDriver->getComposerInformation($identifier)); } + /** + * @dataProvider getAssetTypes + * + * @param string $type + * @param string $filename + */ + public function testLocalRepositoryWithEmptyComposer($type, $filename) + { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + + $path = sys_get_temp_dir().'/composer-test/local-repository.git'; + $fs = new Filesystem(); + $fs->ensureDirectoryExists($path); + + $repoUrl = 'file://'.$path; + $identifier = 'v0.0.0'; + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + + $repoConfig = array( + 'url' => $repoUrl, + 'asset-type' => $type, + 'filename' => $filename, + 'asset-repository-manager' => $this->assetRepositoryManager, + ); + + $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + $process->expects(static::any()) + ->method('splitLines') + ->willReturn(array()) + ; + $process->expects(static::any()) + ->method('execute') + ->willReturnCallback(function () { + return 0; + }) + ; + + /** @var IOInterface $io */ + /** @var ProcessExecutor $process */ + $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null); + $gitDriver->initialize(); + + $validEmpty = array( + '_nonexistent_package' => true, + ); + + static::assertSame($validEmpty, $gitDriver->getComposerInformation($identifier)); + } + /** * @dataProvider getAssetTypes * @@ -129,6 +183,11 @@ public function testPublicRepositoryWithEmptyComposer($type, $filename) */ public function testPublicRepositoryWithSkipUpdate($type, $filename) { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array('git-skip-update' => '1 hour'))) + ; + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git'; $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); @@ -189,6 +248,11 @@ public function testPublicRepositoryWithSkipUpdate($type, $filename) */ public function testPublicRepositoryWithCodeCache($type, $filename) { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git'; $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; $repoConfig = array( @@ -237,6 +301,11 @@ public function testPublicRepositoryWithCodeCache($type, $filename) */ public function testPublicRepositoryWithFilesystemCache($type, $filename) { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git'; $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; $repoConfig = array( @@ -279,6 +348,132 @@ public function testPublicRepositoryWithFilesystemCache($type, $filename) static::assertSame($composer1, $composer2); } + /** + * @dataProvider getAssetTypes + * + * @param string $type + * @param string $filename + * + * @expectedException \RuntimeException + * @expectedExceptionMessageRegExp /Can not clone https:\/\/github.com\/fxpio\/composer-asset-plugin.git to access package information. The "([\s\S]+)" directory is not writable by the current user./ + */ + public function testPublicRepositoryWithUnwritableFilesystemCache($type, $filename) + { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + + chmod($this->config->get('cache-vcs-dir'), 0400); + $isWritable = is_writable($this->config->get('cache-vcs-dir')); + static::assertFalse($isWritable); + + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git'; + $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; + $repoConfig = array( + 'url' => $repoUrl, + 'asset-type' => $type, + 'filename' => $filename, + 'asset-repository-manager' => $this->assetRepositoryManager, + ); + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + + /** @var IOInterface $io */ + /** @var ProcessExecutor $process */ + $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null); + + $gitDriver->initialize(); + $gitDriver->getComposerInformation($identifier); + } + + /** + * @dataProvider getAssetTypes + * + * @param string $type + * @param string $filename + * + * @expectedException \InvalidArgumentException + * @expectedExceptionMessage The source URL ssh://git@github.com:port/fxpio/composer-asset-plugin.git is invalid, ssh URLs should have a port number after ":". + */ + public function testPublicRepositoryWithInvalidSShUrl($type, $filename) + { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + + $repoUrl = 'ssh://git@github.com:port/fxpio/composer-asset-plugin.git'; + $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; + $repoConfig = array( + 'url' => $repoUrl, + 'asset-type' => $type, + 'filename' => $filename, + 'asset-repository-manager' => $this->assetRepositoryManager, + ); + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + + /** @var IOInterface $io */ + /** @var ProcessExecutor $process */ + $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null); + + $gitDriver->initialize(); + $gitDriver->getComposerInformation($identifier); + } + + /** + * @dataProvider getAssetTypes + * + * @param string $type + * @param string $filename + */ + public function testPublicRepositoryWithFailedToUpdatePackage($type, $filename) + { + $this->assetRepositoryManager->expects(static::any()) + ->method('getConfig') + ->willReturn(new \Fxp\Composer\AssetPlugin\Config\Config(array())) + ; + + $repoUrl = 'https://github.com/fxpio/composer-asset-plugin.git'; + $identifier = '92bebbfdcde75ef2368317830e54b605bc938123'; + $repoConfig = array( + 'url' => $repoUrl, + 'asset-type' => $type, + 'filename' => $filename, + 'asset-repository-manager' => $this->assetRepositoryManager, + ); + $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $io->expects(static::at(0)) + ->method('writeError') + ->with('Failed to update https://github.com/fxpio/composer-asset-plugin.git, package information from this repository may be outdated') + ; + + $process = $this->getMockBuilder('Composer\Util\ProcessExecutor')->getMock(); + $process->expects(static::atLeastOnce()) + ->method('splitLines') + ->willReturn(array()) + ; + $process->expects(static::atLeastOnce()) + ->method('execute') + ->willReturnCallback(static function ($command, &$output = null) { + if ('git rev-parse --git-dir' === $command) { + $output = '.'; + } elseif ('git remote set-url origin '.escapeshellarg('https://github.com/fxpio/composer-asset-plugin.git').' && git remote update --prune origin' === $command) { + throw new \Exception('Skip sync mirror'); + } + + return 0; + }) + ; + + /** @var IOInterface $io */ + /** @var ProcessExecutor $process */ + $gitDriver = new GitDriver($repoConfig, $io, $this->config, $process, null); + $gitDriver->initialize(); + $gitDriver->getComposerInformation($identifier); + } + protected function setAttribute($object, $attribute, $value) { $attr = new \ReflectionProperty($object, $attribute);