diff --git a/src/Config/Config.php b/src/Config/Config.php index 94ebd42..bfd0864 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -20,7 +20,7 @@ * * @author François Pluchino */ -final class Config +class Config { private array $cacheEnv = []; diff --git a/tests/Asset/AssetManager.php b/tests/Asset/AssetManager.php index 3e8c793..572ce34 100644 --- a/tests/Asset/AssetManager.php +++ b/tests/Asset/AssetManager.php @@ -1,5 +1,7 @@ config = new Config(array()); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $this->config = new Config([]); + $this->io = $this->createMock(IOInterface::class); $this->executor = new ProcessExecutorMock($this->io); - $this->fs = $this->getMockBuilder('Composer\Util\Filesystem')->disableOriginalConstructor()->getMock(); + $this->fs = $this->createMock(Filesystem::class); $this->sfs = new \Symfony\Component\Filesystem\Filesystem(); - $this->fallback = $this->getMockBuilder('Foxy\Fallback\FallbackInterface')->getMock(); + $this->fallback = $this->createMock(FallbackInterface::class); $this->manager = $this->getManager(); $this->oldCwd = getcwd(); $this->cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_asset_manager_test_', true); $this->sfs->mkdir($this->cwd); - chdir($this->cwd); + + \chdir($this->cwd); } protected function tearDown(): void { parent::tearDown(); - chdir($this->oldCwd); + \chdir($this->oldCwd); + $this->sfs->remove($this->cwd); $this->config = null; $this->io = null; @@ -107,58 +77,59 @@ protected function tearDown(): void $this->cwd = null; } - public function testGetName() + public function testGetName(): void { - static::assertSame($this->getValidName(), $this->manager->getName()); + $this->assertSame($this->getValidName(), $this->manager->getName()); } - public function testGetLockPackageName() + public function testGetLockPackageName(): void { - static::assertSame($this->getValidLockPackageName(), $this->manager->getLockPackageName()); + $this->assertSame($this->getValidLockPackageName(), $this->manager->getLockPackageName()); } - public function testGetPackageName() + public function testGetPackageName(): void { - static::assertSame('package.json', $this->manager->getPackageName()); + $this->assertSame('package.json', $this->manager->getPackageName()); } - public function testHasLockFile() + public function testHasLockFile(): void { - static::assertFalse($this->manager->hasLockFile()); + $this->assertFalse($this->manager->hasLockFile()); } - public function testIsInstalled() + public function testIsInstalled(): void { - static::assertFalse($this->manager->isInstalled()); + $this->assertFalse($this->manager->isInstalled()); } - public function testIsUpdatable() + public function testIsUpdatable(): void { - static::assertFalse($this->manager->isUpdatable()); + $this->assertFalse($this->manager->isUpdatable()); } - public function testSetUpdatable() + public function testSetUpdatable(): void { $res = $this->manager->setUpdatable(false); - static::assertInstanceOf('Foxy\Asset\AssetManagerInterface', $res); + + $this->assertInstanceOf(AssetManagerInterface::class, $res); } - public function testValidateWithoutInstalledManager() + public function testValidateWithoutInstalledManager(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessageMatches('/The binary of "(\w+)" must be installed/'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessageMatches('/The binary of "(\w+)" must be installed/'); $this->manager->validate(); } - public function testValidateWithInstalledManagerAndWithoutValidVersion() + public function testValidateWithInstalledManagerAndWithoutValidVersion(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessageMatches('/The installed (\w+) version "42.0.0" doesn\'t match with the constraint version ">=50.0"/'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessageMatches( + '/The installed (\w+) version "42.0.0" doesn\'t match with the constraint version ">=50.0"/' + ); - $this->config = new Config(array(), array( - 'manager-version' => '>=50.0', - )); + $this->config = new Config([], ['manager-version' => '>=50.0']); $this->manager = $this->getManager(); $this->executor->addExpectedValues(0, '42.0.0'); @@ -166,200 +137,167 @@ public function testValidateWithInstalledManagerAndWithoutValidVersion() $this->manager->validate(); } - public function testValidateWithInstalledManagerAndWithValidVersion() + public function testValidateWithInstalledManagerAndWithValidVersion(): void { - $this->config = new Config(array(), array( - 'manager-version' => '>=41.0', - )); + $this->config = new Config([], ['manager-version' => '>=41.0']); $this->manager = $this->getManager(); $this->executor->addExpectedValues(0, '42.0.0'); $this->manager->validate(); - static::assertSame('>=41.0', $this->config->get('manager-version')); + $this->assertSame('>=41.0', $this->config->get('manager-version')); } - public function testValidateWithInstalledManagerAndWithoutValidationVersion() + public function testValidateWithInstalledManagerAndWithoutValidationVersion(): void { $this->executor->addExpectedValues(0, '42.0.0'); $this->manager->validate(); - static::assertNull($this->config->get('manager-version')); + $this->assertNull($this->config->get('manager-version')); } - public function testAddDependenciesForInstallCommand() + public function testAddDependenciesForInstallCommand(): void { - $expectedPackage = array( - 'dependencies' => array( + $expectedPackage = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', - '@composer-asset/new--dependency' => 'file:./path/new/dependency', - ), - ); - $allDependencies = array( + '@composer-asset/new--dependency' => 'file:./path/new/dependency' + ], + ]; + $allDependencies = [ '@composer-asset/foo--bar' => 'path/foo/bar/package.json', '@composer-asset/new--dependency' => 'path/new/dependency/package.json', - ); + ]; - /** @var \PHPUnit_Framework_MockObject_MockObject|RootPackageInterface $rootPackage */ - $rootPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); - $rootPackage->expects(static::any()) - ->method('getLicense') - ->willReturn(array()) - ; + /** @var MockObject|RootPackageInterface $rootPackage */ + $rootPackage = $this->createMock(RootPackageInterface::class); - static::assertFalse($this->manager->isInstalled()); - static::assertFalse($this->manager->isUpdatable()); + $rootPackage->expects($this->any())->method('getLicense')->willReturn([]); + + $this->assertFalse($this->manager->isInstalled()); + $this->assertFalse($this->manager->isUpdatable()); $assetPackage = $this->manager->addDependencies($rootPackage, $allDependencies); - static::assertInstanceOf('Foxy\Asset\AssetPackageInterface', $assetPackage); - static::assertEquals($expectedPackage, $assetPackage->getPackage()); + $this->assertInstanceOf(\Foxy\Asset\AssetPackageInterface::class, $assetPackage); + + $this->assertEquals($expectedPackage, $assetPackage->getPackage()); } - public function testAddDependenciesForUpdateCommand() + public function testAddDependenciesForUpdateCommand(): void { $this->actionForTestAddDependenciesForUpdateCommand(); - $expectedPackage = array( - 'dependencies' => array( + $expectedPackage = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', - '@composer-asset/new--dependency' => 'file:./path/new/dependency', - ), - ); - $package = array( - 'dependencies' => array( + '@composer-asset/new--dependency' => 'file:./path/new/dependency' + ], + ]; + $package = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@composer-asset/baz--bar' => 'file:./path/baz/bar', - ), - ); - $allDependencies = array( + ], + ]; + $allDependencies = [ '@composer-asset/foo--bar' => 'path/foo/bar/package.json', '@composer-asset/new--dependency' => 'path/new/dependency/package.json', - ); + ]; + $jsonFile = new JsonFile($this->cwd . '/package.json'); - /** @var \PHPUnit_Framework_MockObject_MockObject|RootPackageInterface $rootPackage */ - $rootPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); - $rootPackage->expects(static::any()) - ->method('getLicense') - ->willReturn(array()) - ; + /** @var MockObject|RootPackageInterface $rootPackage */ + $rootPackage = $this->createMock(RootPackageInterface::class); + + $rootPackage->expects($this->any())->method('getLicense')->willReturn([]); + $nodeModulePath = $this->cwd . ltrim(AbstractAssetManager::NODE_MODULES_PATH, '.'); $jsonFile->write($package); - static::assertFileExists($jsonFile->getPath()); + + $this->assertFileExists($jsonFile->getPath()); $this->sfs->mkdir($nodeModulePath); - static::assertFileExists($nodeModulePath); + $this->assertFileExists($nodeModulePath); + $lockFilePath = $this->cwd . \DIRECTORY_SEPARATOR . $this->manager->getLockPackageName(); - file_put_contents($lockFilePath, '{}'); - static::assertFileExists($lockFilePath); - static::assertTrue($this->manager->isInstalled()); - static::assertTrue($this->manager->isUpdatable()); + + \file_put_contents($lockFilePath, '{}'); + + $this->assertFileExists($lockFilePath); + $this->assertTrue($this->manager->isInstalled()); + $this->assertTrue($this->manager->isUpdatable()); $assetPackage = $this->manager->addDependencies($rootPackage, $allDependencies); - static::assertInstanceOf('Foxy\Asset\AssetPackageInterface', $assetPackage); - static::assertEquals($expectedPackage, $assetPackage->getPackage()); + $this->assertInstanceOf(\Foxy\Asset\AssetPackageInterface::class, $assetPackage); + $this->assertEquals($expectedPackage, $assetPackage->getPackage()); } - public function testRunWithDisableOption() + public function testRunWithDisableOption(): void { - $this->config = new Config(array(), array( - 'run-asset-manager' => false, - )); + $this->config = new Config([], ['run-asset-manager' => false]); - static::assertSame(0, $this->getManager()->run()); + $this->assertSame(0, $this->getManager()->run()); } public static function getRunData(): array { - return array( - array(0, 'install'), - array(0, 'update'), - array(1, 'install'), - array(1, 'update'), - ); + return [[0, 'install'], [0, 'update'], [1, 'install'], [1, 'update']]; } /** * @dataProvider getRunData - * - * @param int $expectedRes - * @param string $action */ - public function testRunForInstallCommand($expectedRes, $action) + public function testRunForInstallCommand(int $expectedRes, string $action): void { $this->actionForTestRunForInstallCommand($action); - $this->config = new Config(array(), array( - 'run-asset-manager' => true, - 'fallback-asset' => true, - )); + $this->config = new Config([], ['run-asset-manager' => true, 'fallback-asset' => true]); $this->manager = $this->getManager(); if ('install' === $action) { $expectedCommand = $this->getValidInstallCommand(); } else { $expectedCommand = $this->getValidUpdateCommand(); - file_put_contents($this->cwd . \DIRECTORY_SEPARATOR . $this->manager->getPackageName(), '{}'); + + \file_put_contents($this->cwd . \DIRECTORY_SEPARATOR . $this->manager->getPackageName(), '{}'); + $nodeModulePath = $this->cwd . ltrim(AbstractAssetManager::NODE_MODULES_PATH, '.'); + $this->sfs->mkdir($nodeModulePath); - static::assertFileExists($nodeModulePath); + $this->assertFileExists($nodeModulePath); + $lockFilePath = $this->cwd . \DIRECTORY_SEPARATOR . $this->manager->getLockPackageName(); - file_put_contents($lockFilePath, '{}'); - static::assertFileExists($lockFilePath); - static::assertTrue($this->manager->isInstalled()); - static::assertTrue($this->manager->isUpdatable()); + + \file_put_contents($lockFilePath, '{}'); + + $this->assertFileExists($lockFilePath); + $this->assertTrue($this->manager->isInstalled()); + $this->assertTrue($this->manager->isUpdatable()); } if (0 === $expectedRes) { - $this->fallback->expects(static::never()) - ->method('restore') - ; + $this->fallback->expects($this->never())->method('restore'); } else { - $this->fallback->expects(static::once()) - ->method('restore') - ; + $this->fallback->expects($this->once())->method('restore'); } $this->executor->addExpectedValues($expectedRes, 'ASSET MANAGER OUTPUT'); - static::assertSame($expectedRes, $this->getManager()->run()); - static::assertSame($expectedCommand, $this->executor->getLastCommand()); - static::assertSame('ASSET MANAGER OUTPUT', $this->executor->getLastOutput()); + $this->assertSame($expectedRes, $this->getManager()->run()); + $this->assertSame($expectedCommand, $this->executor->getLastCommand()); + $this->assertSame('ASSET MANAGER OUTPUT', $this->executor->getLastOutput()); } - /** - * @return AssetManagerInterface - */ - abstract protected function getManager(); - - /** - * @return string - */ - abstract protected function getValidName(); - - /** - * @return string - */ - abstract protected function getValidLockPackageName(); - - /** - * @return string - */ - abstract protected function getValidVersionCommand(); - - /** - * @return string - */ - abstract protected function getValidInstallCommand(); - - /** - * @return string - */ - abstract protected function getValidUpdateCommand(); + abstract protected function getManager(): AssetManagerInterface; + abstract protected function getValidName(): string; + abstract protected function getValidLockPackageName(): string; + abstract protected function getValidVersionCommand(): string; + abstract protected function getValidInstallCommand(): string; + abstract protected function getValidUpdateCommand(): string; - protected function actionForTestAddDependenciesForUpdateCommand() + protected function actionForTestAddDependenciesForUpdateCommand(): void { // do nothing by default } @@ -367,7 +305,7 @@ protected function actionForTestAddDependenciesForUpdateCommand() /** * @param string $action The action */ - protected function actionForTestRunForInstallCommand($action) + protected function actionForTestRunForInstallCommand(string $action): void { // do nothing by default } diff --git a/tests/Asset/AssetManagerFinderTest.php b/tests/Asset/AssetManagerFinderTest.php index bded1d9..985c0bb 100644 --- a/tests/Asset/AssetManagerFinderTest.php +++ b/tests/Asset/AssetManagerFinderTest.php @@ -1,5 +1,7 @@ getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); + $am = $this->createMock(\Foxy\Asset\AssetManagerInterface::class); - $am->expects(static::once()) - ->method('getName') - ->willReturn('foo') - ; + $am->expects($this->once())->method('getName')->willReturn('foo'); - $amf = new AssetManagerFinder(array($am)); + $amf = new AssetManagerFinder([$am]); $res = $amf->findManager('foo'); - static::assertSame($am, $res); + $this->assertSame($am, $res); } - public function testFindManagerWithInvalidManager() + public function testFindManagerWithInvalidManager(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessage('The asset manager "bar" doesn\'t exist'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessage('The asset manager "bar" doesn\'t exist'); + + $am = $this->createMock(\Foxy\Asset\AssetManagerInterface::class); - $am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); + $am->expects($this->once())->method('getName')->willReturn('foo'); - $am->expects(static::once()) - ->method('getName') - ->willReturn('foo') - ; + $amf = new AssetManagerFinder([$am]); - $amf = new AssetManagerFinder(array($am)); $amf->findManager('bar'); } - public function testFindManagerWithAutoManagerAndAvailableManagerByLockFile() + public function testFindManagerWithAutoManagerAndAvailableManagerByLockFile(): void { - $am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); - - $am->expects(static::once()) - ->method('getName') - ->willReturn('foo') - ; + $am = $this->createMock(\Foxy\Asset\AssetManagerInterface::class); - $am->expects(static::once()) - ->method('hasLockFile') - ->willReturn(true) - ; + $am->expects($this->once())->method('getName')->willReturn('foo'); + $am->expects($this->once())->method('hasLockFile')->willReturn(true); + $am->expects($this->never())->method('isAvailable'); - $am->expects(static::never()) - ->method('isAvailable') - ; + $amf = new AssetManagerFinder([$am]); - $amf = new AssetManagerFinder(array($am)); $res = $amf->findManager(null); - static::assertSame($am, $res); + $this->assertSame($am, $res); } - public function testFindManagerWithAutoManagerAndAvailableManagerByAvailability() + public function testFindManagerWithAutoManagerAndAvailableManagerByAvailability(): void { - $am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); + $am = $this->createMock(\Foxy\Asset\AssetManagerInterface::class); - $am->expects(static::once()) - ->method('getName') - ->willReturn('foo') - ; + $am->expects($this->once())->method('getName')->willReturn('foo'); + $am->expects($this->once())->method('hasLockFile')->willReturn(false); + $am->expects($this->once())->method('isAvailable')->willReturn(true); - $am->expects(static::once()) - ->method('hasLockFile') - ->willReturn(false) - ; + $amf = new AssetManagerFinder([$am]); - $am->expects(static::once()) - ->method('isAvailable') - ->willReturn(true) - ; - - $amf = new AssetManagerFinder(array($am)); $res = $amf->findManager(null); - static::assertSame($am, $res); + $this->assertSame($am, $res); } - public function testFindManagerWithAutoManagerAndNoAvailableManager() + public function testFindManagerWithAutoManagerAndNoAvailableManager(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessage('No asset manager is found'); - - $am = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessage('No asset manager is found'); - $am->expects(static::atLeastOnce()) - ->method('getName') - ->willReturn('foo') - ; + $am = $this->getMockBuilder(\Foxy\Asset\AssetManagerInterface::class)->getMock(); - $am->expects(static::once()) - ->method('hasLockFile') - ->willReturn(false) - ; + $am->expects($this->atLeastOnce())->method('getName')->willReturn('foo'); + $am->expects($this->once())->method('hasLockFile')->willReturn(false); + $am->expects($this->once())->method('isAvailable')->willReturn(false); - $am->expects(static::once()) - ->method('isAvailable') - ->willReturn(false) - ; + $amf = new AssetManagerFinder([$am]); - $amf = new AssetManagerFinder(array($am)); $amf->findManager(null); } } diff --git a/tests/Asset/AssetPackageTest.php b/tests/Asset/AssetPackageTest.php index 7098a5e..52e4e41 100644 --- a/tests/Asset/AssetPackageTest.php +++ b/tests/Asset/AssetPackageTest.php @@ -1,5 +1,7 @@ cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_asset_package_test_', true); $this->sfs = new Filesystem(); - $this->rootPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); - $this->jsonFile = $this->getMockBuilder('Composer\Json\JsonFile') + $this->rootPackage = $this->createMock(RootPackageInterface::class); + $this->jsonFile = $this->getMockBuilder(JsonFile::class) ->disableOriginalConstructor() - ->onlyMethods(array('exists', 'getPath', 'read', 'write')) + ->onlyMethods(['exists', 'getPath', 'read', 'write']) ->getMock() ; - $this->rootPackage->expects(static::any()) - ->method('getLicense') - ->willReturn(array()) - ; + $this->rootPackage->expects($this->any())->method('getLicense')->willReturn([]); $this->sfs->mkdir($this->cwd); } @@ -77,206 +62,179 @@ protected function tearDown(): void $this->cwd = null; } - public function testGetPackageWithExistingFile() + public function testGetPackageWithExistingFile(): void { - $package = array( - 'name' => '@foo/bar', - ); + $package = ['name' => '@foo/bar']; $contentString = json_encode($package); $this->addPackageFile($package, $contentString); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); - static::assertSame($package, $assetPackage->getPackage()); + $this->assertSame($package, $assetPackage->getPackage()); } - public function testWrite() + public function testWrite(): void { - $package = array( - 'name' => '@foo/bar', - ); - - $this->jsonFile->expects(static::once()) - ->method('exists') - ->willReturn(false) - ; + $package = ['name' => '@foo/bar']; - $this->jsonFile->expects(static::once()) - ->method('write') - ->with($package) - ; + $this->jsonFile->expects($this->once())->method('exists')->willReturn(false); + $this->jsonFile->expects($this->once())->method('write')->with($package); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); + $assetPackage->setPackage($package); $assetPackage->write(); } public static function getDataRequiredKeys(): array { - return array( - array( - array( + return [ + [ + [ 'name' => '@foo/bar', 'license' => 'MIT', - ), - array( + ], + [ 'name' => '@foo/bar', 'license' => 'MIT', - ), + ], 'proprietary', - ), - array( - array( + ], + [ + [ 'name' => '@foo/bar', 'license' => 'MIT', - ), - array( + ], + [ 'name' => '@foo/bar', - ), + ], 'MIT', - ), - array( - array( + ], + [ + [ 'name' => '@foo/bar', 'private' => true, - ), - array( + ], + [ 'name' => '@foo/bar', - ), + ], 'proprietary', - ), - ); + ], + ]; } /** * @dataProvider getDataRequiredKeys - * - * @param string $license */ - public function testInjectionOfRequiredKeys(array $expected, array $package, $license) + public function testInjectionOfRequiredKeys(array $expected, array $package, string $license): void { $this->addPackageFile($package); - $this->rootPackage = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); - $this->rootPackage->expects(static::any()) - ->method('getLicense') - ->willReturn(array($license)) - ; + $this->rootPackage = $this->createMock(RootPackageInterface::class); + + $this->rootPackage->expects($this->any())->method('getLicense')->willReturn([$license]); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); - static::assertEquals($expected, $assetPackage->getPackage()); + $this->assertEquals($expected, $assetPackage->getPackage()); } - public function testGetInstalledDependencies() + public function testGetInstalledDependencies(): void { - $expected = array( + $expected = [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@composer-asset/baz--bar' => 'file:./path/baz/bar', - ); - $package = array( - 'dependencies' => array( + ]; + $package = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@bar/foo' => '^1.0.0', '@composer-asset/baz--bar' => 'file:./path/baz/bar', - ), - ); + ], + ]; + $this->addPackageFile($package); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); - static::assertEquals($expected, $assetPackage->getInstalledDependencies()); + $this->assertEquals($expected, $assetPackage->getInstalledDependencies()); } - public function testAddNewDependencies() + public function testAddNewDependencies(): void { - $expected = array( - 'dependencies' => array( + $expected = [ + 'dependencies' => [ '@bar/foo' => '^1.0.0', '@composer-asset/baz--bar' => 'file:./path/baz/bar', '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@composer-asset/new--dependency' => 'file:./path/new/dependency', - ), - ); - $expectedExisting = array( - '@composer-asset/foo--bar', - '@composer-asset/baz--bar', - ); - - $package = array( - 'dependencies' => array( + ], + ]; + $expectedExisting = ['@composer-asset/foo--bar', '@composer-asset/baz--bar']; + + $package = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@bar/foo' => '^1.0.0', '@composer-asset/baz--bar' => 'file:./path/baz/bar', - ), - ); - $dependencies = array( + ], + ]; + $dependencies = [ '@composer-asset/foo--bar' => 'path/foo/bar/package.json', '@composer-asset/baz--bar' => 'path/baz/bar/package.json', '@composer-asset/new--dependency' => 'path/new/dependency/package.json', - ); + ]; + $this->addPackageFile($package); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); $existing = $assetPackage->addNewDependencies($dependencies); - static::assertSame($expected, $assetPackage->getPackage()); - static::assertSame($expectedExisting, $existing); + $this->assertSame($expected, $assetPackage->getPackage()); + $this->assertSame($expectedExisting, $existing); } - public function testRemoveUnusedDependencies() + public function testRemoveUnusedDependencies(): void { - $expected = array( - 'dependencies' => array( + $expected = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@bar/foo' => '^1.0.0', - ), - ); + ], + ]; - $package = array( - 'dependencies' => array( + $package = [ + 'dependencies' => [ '@composer-asset/foo--bar' => 'file:./path/foo/bar', '@bar/foo' => '^1.0.0', '@composer-asset/baz--bar' => 'file:./path/baz/bar', - ), - ); - $dependencies = array( - '@composer-asset/foo--bar' => 'file:./path/foo/bar', - ); + ], + ]; + $dependencies = ['@composer-asset/foo--bar' => 'file:./path/foo/bar']; + $this->addPackageFile($package); $assetPackage = new AssetPackage($this->rootPackage, $this->jsonFile); $assetPackage->removeUnusedDependencies($dependencies); - static::assertEquals($expected, $assetPackage->getPackage()); + $this->assertEquals($expected, $assetPackage->getPackage()); } /** * Add the package in file. * - * @param array $package The package - * @param null|string $contentString The string content of package + * @param array $package The package. + * @param null|string $contentString The string content of package. */ - protected function addPackageFile(array $package, $contentString = null) + protected function addPackageFile(array $package, $contentString = null): void { $filename = $this->cwd . '/package.json'; - $contentString = null !== $contentString ? $contentString : json_encode($package); - - $this->jsonFile->expects(static::any()) - ->method('exists') - ->willReturn(true) - ; + $contentString ??= json_encode($package); - $this->jsonFile->expects(static::any()) - ->method('getPath') - ->willReturn($filename) - ; - - $this->jsonFile->expects(static::any()) - ->method('read') - ->willReturn($package) - ; + $this->jsonFile->expects($this->any())->method('exists')->willReturn(true); + $this->jsonFile->expects($this->any())->method('getPath')->willReturn($filename); + $this->jsonFile->expects($this->any())->method('read')->willReturn($package); - file_put_contents($filename, $contentString); + \file_put_contents($filename, $contentString); } } diff --git a/tests/Asset/NpmAssetManagerTest.php b/tests/Asset/NpmAssetManagerTest.php index 574e42e..a7ab14a 100644 --- a/tests/Asset/NpmAssetManagerTest.php +++ b/tests/Asset/NpmAssetManagerTest.php @@ -1,5 +1,7 @@ io, $this->config, $this->executor, $this->fs, $this->fallback); } - - protected function getValidName() + protected function getValidName(): string { return 'npm'; } - - protected function getValidLockPackageName() + protected function getValidLockPackageName(): string { return 'package-lock.json'; } - - protected function getValidVersionCommand() + protected function getValidVersionCommand(): string { return 'npm --version'; } - - protected function getValidInstallCommand() + protected function getValidInstallCommand(): string { return 'npm install'; } - - protected function getValidUpdateCommand() + protected function getValidUpdateCommand(): string { return 'npm update'; } diff --git a/tests/Asset/PnpmAssetManagerTest.php b/tests/Asset/PnpmAssetManagerTest.php index eb70b48..738c514 100644 --- a/tests/Asset/PnpmAssetManagerTest.php +++ b/tests/Asset/PnpmAssetManagerTest.php @@ -1,5 +1,7 @@ io, $this->config, $this->executor, $this->fs, $this->fallback); } - - protected function getValidName() + protected function getValidName(): string { return 'pnpm'; } - - protected function getValidLockPackageName() + protected function getValidLockPackageName(): string { return 'pnpm-lock.yaml'; } - - protected function getValidVersionCommand() + protected function getValidVersionCommand(): string { return 'pnpm --version'; } - - protected function getValidInstallCommand() + protected function getValidInstallCommand(): string { return 'pnpm install'; } - - protected function getValidUpdateCommand() + protected function getValidUpdateCommand(): string { return 'pnpm update'; } diff --git a/tests/Asset/YarnAssetManagerTest.php b/tests/Asset/YarnAssetManagerTest.php index 976d908..f3e2dfc 100644 --- a/tests/Asset/YarnAssetManagerTest.php +++ b/tests/Asset/YarnAssetManagerTest.php @@ -1,5 +1,7 @@ executor->addExpectedValues(0, '1.0.0'); @@ -33,44 +35,37 @@ public function actionForTestRunForInstallCommand($action) } } - - protected function getManager() + protected function getManager(): YarnManager { return new YarnManager($this->io, $this->config, $this->executor, $this->fs, $this->fallback); } - - protected function getValidName() + protected function getValidName(): string { return 'yarn'; } - - protected function getValidLockPackageName() + protected function getValidLockPackageName(): string { return 'yarn.lock'; } - - protected function getValidVersionCommand() + protected function getValidVersionCommand(): string { return 'yarn --version'; } - - protected function getValidInstallCommand() + protected function getValidInstallCommand(): string { return 'yarn install --non-interactive'; } - - protected function getValidUpdateCommand() + protected function getValidUpdateCommand(): string { return 'yarn upgrade --non-interactive'; } - - protected function actionForTestAddDependenciesForUpdateCommand() + protected function actionForTestAddDependenciesForUpdateCommand(): void { $this->executor->addExpectedValues(0, '1.0.0'); $this->executor->addExpectedValues(0, 'CHECK OUTPUT'); diff --git a/tests/Asset/YarnNextAssetManagerTest.php b/tests/Asset/YarnNextAssetManagerTest.php index ba9d2c3..bb660c6 100644 --- a/tests/Asset/YarnNextAssetManagerTest.php +++ b/tests/Asset/YarnNextAssetManagerTest.php @@ -1,5 +1,7 @@ executor->addExpectedValues(0, '2.0.0'); @@ -31,44 +33,37 @@ public function actionForTestRunForInstallCommand($action) } } - - protected function getManager() + protected function getManager(): YarnManager { return new YarnManager($this->io, $this->config, $this->executor, $this->fs, $this->fallback); } - - protected function getValidName() + protected function getValidName(): string { return 'yarn'; } - - protected function getValidLockPackageName() + protected function getValidLockPackageName(): string { return 'yarn.lock'; } - - protected function getValidVersionCommand() + protected function getValidVersionCommand(): string { return 'yarn --version'; } - - protected function getValidInstallCommand() + protected function getValidInstallCommand(): string { return 'yarn install'; } - - protected function getValidUpdateCommand() + protected function getValidUpdateCommand(): string { return 'yarn up'; } - - protected function actionForTestAddDependenciesForUpdateCommand() + protected function actionForTestAddDependenciesForUpdateCommand(): void { $this->executor->addExpectedValues(0, '2.0.0'); $this->executor->addExpectedValues(0, 'CHECK OUTPUT'); diff --git a/tests/Config/ConfigTest.php b/tests/Config/ConfigTest.php index 1a72930..01222ab 100644 --- a/tests/Config/ConfigTest.php +++ b/tests/Config/ConfigTest.php @@ -1,5 +1,7 @@ composer = $this->getMockBuilder('Composer\Composer')->disableOriginalConstructor()->getMock(); - $this->composerConfig = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock(); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); + $this->composer = $this->createMock(Composer::class); + $this->composerConfig = $this->createMock(Config::class); + $this->io = $this->createMock(IOInterface::class); + $this->package = $this->createMock(RootPackageInterface::class); - $this->composer->expects(static::any()) - ->method('getPackage') - ->willReturn($this->package) - ; - - $this->composer->expects(static::any()) - ->method('getConfig') - ->willReturn($this->composerConfig) - ; + $this->composer->expects($this->any())->method('getPackage')->willReturn($this->package); + $this->composer->expects($this->any())->method('getConfig')->willReturn($this->composerConfig); } public static function getDataForGetConfig(): array { - return array( - array('foo', 42, 42), - array('bar', 'foo', 'empty'), - array('baz', false, true), - array('test', 0, 0), - array('manager-bar', 23, 0), - array('manager-baz', 0, 0), - array('global-composer-foo', 90, 0), - array('global-composer-bar', 70, 0), - array('global-config-foo', 23, 0), - array('env-boolean', false, true, 'FOXY__ENV_BOOLEAN=false'), - array('env-integer', -32, 0, 'FOXY__ENV_INTEGER=-32'), - array('env-json', array('foo' => 'bar'), array(), 'FOXY__ENV_JSON="{"foo": "bar"}"'), - array('env-json-array', array(array('foo' => 'bar')), array(), 'FOXY__ENV_JSON_ARRAY="[{"foo": "bar"}]"'), - array('env-string', 'baz', 'foo', 'FOXY__ENV_STRING=baz'), - array('test-p1', 'def', 'def', null, array()), - array('test-p1', 'def', 'def', null, array('test-p1' => 'ok')), - array('test-p1', 'ok', null, null, array('test-p1' => 'ok')), - ); + return [ + ['foo', 42, 42], + ['bar', 'foo', 'empty'], + ['baz', false, true], + ['test', 0, 0], + ['manager-bar', 23, 0], + ['manager-baz', 0, 0], + ['global-composer-foo', 90, 0], + ['global-composer-bar', 70, 0], + ['global-config-foo', 23, 0], + ['env-boolean', false, true, 'FOXY__ENV_BOOLEAN=false'], + ['env-integer', -32, 0, 'FOXY__ENV_INTEGER=-32'], + ['env-json', ['foo' => 'bar'], [], 'FOXY__ENV_JSON="{"foo": "bar"}"'], + ['env-json-array', [['foo' => 'bar']], [], 'FOXY__ENV_JSON_ARRAY="[{"foo": "bar"}]"'], + ['env-string', 'baz', 'foo', 'FOXY__ENV_STRING=baz'], + ['test-p1', 'def', 'def', null, []], + ['test-p1', 'def', 'def', null, ['test-p1' => 'ok']], + ['test-p1', 'ok', null, null, ['test-p1' => 'ok']], + ]; } /** * @dataProvider getDataForGetConfig * - * @param string $key The key - * @param mixed $expected The expected value - * @param null|mixed $default The default value - * @param null|string $env The env variable - * @param array $defaults The configured default values + * @param string $key The key. + * @param mixed $expected The expected value. + * @param mixed $default The default value. + * @param null|string $env The env variable. + * @param array $defaults The configured default values. */ - public function testGetConfig($key, $expected, $default = null, $env = null, array $defaults = array()) - { + public function testGetConfig( + string $key, + mixed $expected, + mixed $default = null, + string $env = null, + array $defaults = [] + ): void { // add env variables if (null !== $env) { - putenv($env); + \putenv($env); } $globalLogComposer = true; $globalLogConfig = true; $globalPath = realpath(__DIR__ . '/../Fixtures/package/global'); - $this->composerConfig->expects(static::any()) - ->method('has') - ->with('home') - ->willReturn(true) - ; - - $this->composerConfig->expects(static::any()) - ->method('get') - ->with('home') - ->willReturn($globalPath) - ; - - $this->package->expects(static::any()) + + $this->composerConfig->expects($this->any())->method('has')->with('home')->willReturn(true); + $this->composerConfig->expects($this->any())->method('get')->with('home')->willReturn($globalPath); + + $this->package + ->expects($this->any()) ->method('getConfig') - ->willReturn(array( - 'foxy' => array( - 'bar' => 'foo', - 'baz' => false, - 'env-foo' => 55, - 'manager' => 'quill', - 'manager-bar' => array( - 'peter' => 42, - 'quill' => 23, - ), - 'manager-baz' => array( - 'peter' => 42, - ), - ), - )) - ; - - if (0 === strpos($key, 'global-')) { - $this->io->expects(static::atLeast(2)) - ->method('isDebug') - ->willReturn(true) - ; + ->willReturn( + [ + 'foxy' => [ + 'bar' => 'foo', + 'baz' => false, + 'env-foo' => 55, + 'manager' => 'quill', + 'manager-bar' => [ + 'peter' => 42, + 'quill' => 23 + ], + 'manager-baz' => [ + 'peter' => 42 + ], + ], + ], + ); + + if (\str_starts_with($key, 'global-')) { + $this->io->expects($this->atLeast(2))->method('isDebug')->willReturn(true); $globalLogComposer = false; $globalLogConfig = false; - $this->io->expects(static::atLeastOnce()) + $this->io + ->expects($this->atLeastOnce()) ->method('writeError') - ->willReturnCallback(static function ($message) use ($globalPath, &$globalLogComposer, &$globalLogConfig) { - if (sprintf('Loading Foxy config in file %s/composer.json', $globalPath)) { - $globalLogComposer = true; + ->willReturnCallback( + static function ($message) use ($globalPath, &$globalLogComposer, &$globalLogConfig): void { + if (\sprintf('Loading Foxy config in file %s/composer.json', $globalPath)) { + $globalLogComposer = true; + } + + if (\sprintf('Loading Foxy config in file %s/config.json', $globalPath)) { + $globalLogConfig = true; + } } - - if (sprintf('Loading Foxy config in file %s/config.json', $globalPath)) { - $globalLogConfig = true; - } - }) - ; + ); } $config = ConfigBuilder::build($this->composer, $defaults, $this->io); @@ -166,50 +145,48 @@ public function testGetConfig($key, $expected, $default = null, $env = null, arr // remove env variables if (null !== $env) { - $envKey = substr($env, 0, strpos($env, '=')); - putenv($envKey); - static::assertFalse(getenv($envKey)); - } + $envKey = \substr($env, 0, \strpos($env, '=')); - static::assertTrue($globalLogComposer); - static::assertTrue($globalLogConfig); + \putenv($envKey); - static::assertSame($expected, $value); + $this->assertFalse(\getenv($envKey)); + } + + $this->assertTrue($globalLogComposer); + $this->assertTrue($globalLogConfig); + $this->assertSame($expected, $value); // test cache - static::assertSame($expected, $config->get($key, $default)); + $this->assertSame($expected, $config->get($key, $default)); } public static function getDataForGetArrayConfig(): array { - return array( - array('foo', array(), array()), - array('foo', array(42), array(42)), - array('foo', array(42), array(), array('foo' => array(42))), - ); + return [['foo', [], []], ['foo', [42], [42]], ['foo', [42], [], ['foo' => [42]]]]; } /** * @dataProvider getDataForGetArrayConfig * - * @param string $key The key - * @param array $expected The expected value - * @param array $default The default value - * @param array $defaults The configured default values + * @param string $key The key. + * @param array $expected The expected value. + * @param array $default The default value. + * @param array $defaults The configured default values. */ - public function testGetArrayConfig($key, array $expected, array $default, array $defaults = array()) + public function testGetArrayConfig(string $key, array $expected, array $default, array $defaults = []): void { $config = ConfigBuilder::build($this->composer, $defaults, $this->io); - static::assertSame($expected, $config->getArray($key, $default)); + $this->assertSame($expected, $config->getArray($key, $default)); } - public function testGetEnvConfigWithInvalidJson() + public function testGetEnvConfigWithInvalidJson(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessage('The "FOXY__ENV_JSON" environment variable isn\'t a valid JSON'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessage('The "FOXY__ENV_JSON" environment variable isn\'t a valid JSON'); + + \putenv('FOXY__ENV_JSON="{"foo"}"'); - putenv('FOXY__ENV_JSON="{"foo"}"'); - $config = ConfigBuilder::build($this->composer, array(), $this->io); + $config = ConfigBuilder::build($this->composer, [], $this->io); $ex = null; try { @@ -218,8 +195,8 @@ public function testGetEnvConfigWithInvalidJson() $ex = $e; } - putenv('FOXY__ENV_JSON'); - static::assertFalse(getenv('FOXY__ENV_JSON')); + \putenv('FOXY__ENV_JSON'); + $this->assertFalse(\getenv('FOXY__ENV_JSON')); if (null === $ex) { throw new \Exception('The expected exception was not thrown'); diff --git a/tests/Converter/SemverConverterTest.php b/tests/Converter/SemverConverterTest.php index 53e5f51..196b879 100644 --- a/tests/Converter/SemverConverterTest.php +++ b/tests/Converter/SemverConverterTest.php @@ -1,5 +1,7 @@ converter->convertVersion($semver)); + $this->assertEquals($composer, $this->converter->convertVersion($semver)); - if (!ctype_alpha((string) $semver) && !\in_array($semver, array(null, ''), true)) { - static::assertEquals('v' . $composer, $this->converter->convertVersion('v' . $semver)); + if (!\ctype_alpha((string) $semver) && !\in_array($semver, [null, ''], true)) { + $this->assertEquals('v' . $composer, $this->converter->convertVersion('v' . $semver)); } } public static function getTestVersions(): array { - return array( - array('1.2.3', '1.2.3'), - array('1.2.3alpha', '1.2.3-alpha1'), - array('1.2.3-alpha', '1.2.3-alpha1'), - array('1.2.3a', '1.2.3-alpha1'), - array('1.2.3a1', '1.2.3-alpha1'), - array('1.2.3-a', '1.2.3-alpha1'), - array('1.2.3-a1', '1.2.3-alpha1'), - array('1.2.3b', '1.2.3-beta1'), - array('1.2.3b1', '1.2.3-beta1'), - array('1.2.3-b', '1.2.3-beta1'), - array('1.2.3-b1', '1.2.3-beta1'), - array('1.2.3beta', '1.2.3-beta1'), - array('1.2.3-beta', '1.2.3-beta1'), - array('1.2.3beta1', '1.2.3-beta1'), - array('1.2.3-beta1', '1.2.3-beta1'), - array('1.2.3rc1', '1.2.3-RC1'), - array('1.2.3-rc1', '1.2.3-RC1'), - array('1.2.3rc2', '1.2.3-RC2'), - array('1.2.3-rc2', '1.2.3-RC2'), - array('1.2.3rc.2', '1.2.3-RC.2'), - array('1.2.3-rc.2', '1.2.3-RC.2'), - array('1.2.3+0', '1.2.3-patch0'), - array('1.2.3-0', '1.2.3-patch0'), - array('1.2.3pre', '1.2.3-beta1'), - array('1.2.3-pre', '1.2.3-beta1'), - array('1.2.3dev', '1.2.3-dev'), - array('1.2.3-dev', '1.2.3-dev'), - array('1.2.3+build2012', '1.2.3-patch2012'), - array('1.2.3-build2012', '1.2.3-patch2012'), - array('1.2.3+build.2012', '1.2.3-patch.2012'), - array('1.2.3-build.2012', '1.2.3-patch.2012'), - array('1.3.0–rc30.79', '1.3.0-RC30.79'), - array('1.2.3-SNAPSHOT', '1.2.3-dev'), - array('1.2.3-20123131.3246', '1.2.3-patch20123131.3246'), - array('1.x.x-dev', '1.x-dev'), - array('20170124.0.0', '20170124.000000'), - array('20170124.1.0', '20170124.001000'), - array('20170124.1.1', '20170124.001001'), - array('20170124.100.200', '20170124.100200'), - array('20170124.0', '20170124.000000'), - array('20170124.1', '20170124.001000'), - array('20170124', '20170124'), - array('latest', 'default || *'), - array(null, '*'), - array('', '*'), - ); + return [ + ['1.2.3', '1.2.3'], + ['1.2.3alpha', '1.2.3-alpha1'], + ['1.2.3-alpha', '1.2.3-alpha1'], + ['1.2.3a', '1.2.3-alpha1'], + ['1.2.3a1', '1.2.3-alpha1'], + ['1.2.3-a', '1.2.3-alpha1'], + ['1.2.3-a1', '1.2.3-alpha1'], + ['1.2.3b', '1.2.3-beta1'], + ['1.2.3b1', '1.2.3-beta1'], + ['1.2.3-b', '1.2.3-beta1'], + ['1.2.3-b1', '1.2.3-beta1'], + ['1.2.3beta', '1.2.3-beta1'], + ['1.2.3-beta', '1.2.3-beta1'], + ['1.2.3beta1', '1.2.3-beta1'], + ['1.2.3-beta1', '1.2.3-beta1'], + ['1.2.3rc1', '1.2.3-RC1'], + ['1.2.3-rc1', '1.2.3-RC1'], + ['1.2.3rc2', '1.2.3-RC2'], + ['1.2.3-rc2', '1.2.3-RC2'], + ['1.2.3rc.2', '1.2.3-RC.2'], + ['1.2.3-rc.2', '1.2.3-RC.2'], + ['1.2.3+0', '1.2.3-patch0'], + ['1.2.3-0', '1.2.3-patch0'], + ['1.2.3pre', '1.2.3-beta1'], + ['1.2.3-pre', '1.2.3-beta1'], + ['1.2.3dev', '1.2.3-dev'], + ['1.2.3-dev', '1.2.3-dev'], + ['1.2.3+build2012', '1.2.3-patch2012'], + ['1.2.3-build2012', '1.2.3-patch2012'], + ['1.2.3+build.2012', '1.2.3-patch.2012'], + ['1.2.3-build.2012', '1.2.3-patch.2012'], + ['1.3.0–rc30.79', '1.3.0-RC30.79'], + ['1.2.3-SNAPSHOT', '1.2.3-dev'], + ['1.2.3-20123131.3246', '1.2.3-patch20123131.3246'], + ['1.x.x-dev', '1.x-dev'], + ['20170124.0.0', '20170124.000000'], + ['20170124.1.0', '20170124.001000'], + ['20170124.1.1', '20170124.001001'], + ['20170124.100.200', '20170124.100200'], + ['20170124.0', '20170124.000000'], + ['20170124.1', '20170124.001000'], + ['20170124', '20170124'], + ['latest', 'default || *'], + [null, '*'], + ['', '*'], + ]; } } diff --git a/tests/Event/GetAssetsEventTest.php b/tests/Event/GetAssetsEventTest.php index 6ecab9b..ab92da7 100644 --- a/tests/Event/GetAssetsEventTest.php +++ b/tests/Event/GetAssetsEventTest.php @@ -1,5 +1,7 @@ 'file:./vendor/foxy/composer-asset/foo/bar', - ); + private array $assets = ['@composer-asset/foo--bar' => 'file:./vendor/foxy/composer-asset/foo/bar']; /** * @return GetAssetsEvent */ - public function getEvent() + public function getEvent(): GetAssetsEvent { return new GetAssetsEvent($this->assetDir, $this->packages, $this->assets); } - public function testHasAsset() + public function testHasAsset(): void { $event = $this->getEvent(); - static::assertTrue($event->hasAsset('@composer-asset/foo--bar')); + + $this->assertTrue($event->hasAsset('@composer-asset/foo--bar')); } - public function testAddAsset() + public function testAddAsset(): void { $assetPackageName = '@composer-asset/bar--foo'; $assetPackagePath = 'file:./vendor/foxy/composer-asset/bar/foo'; $event = $this->getEvent(); - static::assertFalse($event->hasAsset($assetPackageName)); + $this->assertFalse($event->hasAsset($assetPackageName)); + $event->addAsset($assetPackageName, $assetPackagePath); - static::assertTrue($event->hasAsset($assetPackageName)); + + $this->assertTrue($event->hasAsset($assetPackageName)); } - public function testGetAssets() + public function testGetAssets(): void { $event = $this->getEvent(); - static::assertSame($this->assets, $event->getAssets()); + $this->assertSame($this->assets, $event->getAssets()); - $expectedAssets = array( + $expectedAssets = [ '@composer-asset/foo--bar' => 'file:./vendor/foxy/composer-asset/foo/bar', '@composer-asset/bar--foo' => 'file:./vendor/foxy/composer-asset/bar/foo', - ); + ]; $event->addAsset('@composer-asset/bar--foo', 'file:./vendor/foxy/composer-asset/bar/foo'); - static::assertSame($expectedAssets, $event->getAssets()); + + $this->assertSame($expectedAssets, $event->getAssets()); } } diff --git a/tests/Event/PostSolveEventTest.php b/tests/Event/PostSolveEventTest.php index 35a6f43..96a4c1c 100644 --- a/tests/Event/PostSolveEventTest.php +++ b/tests/Event/PostSolveEventTest.php @@ -1,5 +1,7 @@ assetDir, $this->packages, 42); } - public function testGetRunResult() + public function testGetRunResult(): void { $event = $this->getEvent(); - static::assertSame(42, $event->getRunResult()); + + $this->assertSame(42, $event->getRunResult()); } } diff --git a/tests/Event/PreSolveEventTest.php b/tests/Event/PreSolveEventTest.php index e2fc966..5e5eef1 100644 --- a/tests/Event/PreSolveEventTest.php +++ b/tests/Event/PreSolveEventTest.php @@ -1,5 +1,7 @@ assetDir, $this->packages); } diff --git a/tests/Event/SolveEvent.php b/tests/Event/SolveEvent.php index e76078e..d1baec2 100644 --- a/tests/Event/SolveEvent.php +++ b/tests/Event/SolveEvent.php @@ -1,5 +1,7 @@ assetDir = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_event_test_', true); - $this->packages = array( - $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(), - ); + $this->packages = [$this->createMock(PackageInterface::class)]; } protected function tearDown(): void { - $this->assetDir = null; + $this->assetDir = ''; $this->packages = null; } @@ -52,15 +46,17 @@ protected function tearDown(): void */ abstract public function getEvent(); - public function testGetAssetDir() + public function testGetAssetDir(): void { $event = $this->getEvent(); - static::assertSame($this->assetDir, $event->getAssetDir()); + + $this->assertSame($this->assetDir, $event->getAssetDir()); } - public function testGetPackages() + public function testGetPackages(): void { $event = $this->getEvent(); - static::assertSame($this->packages, $event->getPackages()); + + $this->assertSame($this->packages, $event->getPackages()); } } diff --git a/tests/Fallback/AssetFallbackTest.php b/tests/Fallback/AssetFallbackTest.php index 9089121..a221885 100644 --- a/tests/Fallback/AssetFallbackTest.php +++ b/tests/Fallback/AssetFallbackTest.php @@ -1,5 +1,7 @@ oldCwd = getcwd(); $this->cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_asset_fallback_test_', true); - $this->config = new Config(array( - 'fallback-asset' => true, - )); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $this->fs = $this->getMockBuilder('Composer\Util\Filesystem') - ->disableOriginalConstructor() - ->onlyMethods(array('remove')) - ->getMock(); + $this->config = new Config(['fallback-asset' => true]); + $this->io = $this->createMock(IOInterface::class); + $this->fs = $this->createMock(Filesystem::class); $this->sfs = new \Symfony\Component\Filesystem\Filesystem(); $this->sfs->mkdir($this->cwd); - chdir($this->cwd); + + \chdir($this->cwd); $this->assetFallback = new AssetFallback($this->io, $this->config, 'package.json', $this->fs); } @@ -85,7 +61,8 @@ protected function tearDown(): void { parent::tearDown(); - chdir($this->oldCwd); + \chdir($this->oldCwd); + $this->sfs->remove($this->cwd); $this->config = null; $this->io = null; @@ -98,58 +75,42 @@ protected function tearDown(): void public static function getSaveData(): array { - return array( - array(true), - array(false), - ); + return [[true], [false]]; } /** * @dataProvider getSaveData - * - * @param bool $withPackageFile */ - public function testSave($withPackageFile) + public function testSave(bool $withPackageFile): void { if ($withPackageFile) { - file_put_contents($this->cwd . '/package.json', '{}'); + \file_put_contents($this->cwd . '/package.json', '{}'); } - static::assertInstanceOf('Foxy\Fallback\AssetFallback', $this->assetFallback->save()); + $this->assertInstanceOf(AssetFallback::class, $this->assetFallback->save()); } - public function testRestoreWithDisableOption() + public function testRestoreWithDisableOption(): void { - $config = new Config(array( - 'fallback-asset' => false, - )); + $config = new Config(['fallback-asset' => false]); $assetFallback = new AssetFallback($this->io, $config, 'package.json', $this->fs); - $this->io->expects(static::never()) - ->method('write') - ; + $this->io->expects($this->never())->method('write'); - $this->fs->expects(static::never()) - ->method('remove') - ; + $this->fs->expects($this->never())->method('remove'); $assetFallback->restore(); } public static function getRestoreData(): array { - return array( - array(true), - array(false), - ); + return [[true], [false]]; } /** * @dataProvider getRestoreData - * - * @param bool $withPackageFile */ - public function testRestore($withPackageFile) + public function testRestore(bool $withPackageFile): void { $content = '{}'; $path = $this->cwd . '/package.json'; @@ -158,23 +119,18 @@ public function testRestore($withPackageFile) file_put_contents($path, $content); } - $this->io->expects(static::once()) - ->method('write') - ; + $this->io->expects($this->once())->method('write'); - $this->fs->expects(static::once()) - ->method('remove') - ->with('package.json') - ; + $this->fs->expects($this->once())->method('remove')->with('package.json'); $this->assetFallback->save(); $this->assetFallback->restore(); if ($withPackageFile) { - static::assertFileExists($path); - static::assertSame($content, file_get_contents($path)); + $this->assertFileExists($path); + $this->assertSame($content, file_get_contents($path)); } else { - static::assertFileDoesNotExist($path); + $this->assertFileDoesNotExist($path); } } } diff --git a/tests/Fallback/ComposerFallbackTest.php b/tests/Fallback/ComposerFallbackTest.php index 80a6374..2b5ae87 100644 --- a/tests/Fallback/ComposerFallbackTest.php +++ b/tests/Fallback/ComposerFallbackTest.php @@ -1,5 +1,7 @@ oldCwd = getcwd(); $this->cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_composer_fallback_test_', true); - $this->config = new Config(array( - 'fallback-composer' => true, - )); - $this->composer = $this->getMockBuilder('Composer\Composer')->disableOriginalConstructor()->getMock(); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $this->input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); - $this->fs = $this->getMockBuilder('Composer\Util\Filesystem') + $this->config = new Config(['fallback-composer' => true]); + $this->composer = $this->createMock(Composer::class); + $this->io = $this->createMock(IOInterface::class); + $this->input = $this->createMock(InputInterface::class); + $this->fs = $this->createMock(Filesystem::class); + /** @var Installer|MockObject */ + $this->installer = $this + ->getMockBuilder(Installer::class) ->disableOriginalConstructor() - ->onlyMethods(array('remove')) - ->getMock(); - $this->installer = $this->getMockBuilder('Composer\Installer') - ->disableOriginalConstructor() - ->onlyMethods(array('run')) + ->onlyMethods(['run']) ->getMock(); $this->sfs = new \Symfony\Component\Filesystem\Filesystem(); $this->sfs->mkdir($this->cwd); - chdir($this->cwd); - $this->composerFallback = new ComposerFallback($this->composer, $this->io, $this->config, $this->input, $this->fs, $this->installer); + \chdir($this->cwd); + + $this->composerFallback = new ComposerFallback( + $this->composer, + $this->io, + $this->config, + $this->input, + $this->fs, + $this->installer + ); } protected function tearDown(): void { parent::tearDown(); - chdir($this->oldCwd); + \chdir($this->oldCwd); + $this->sfs->remove($this->cwd); $this->config = null; $this->composer = null; @@ -126,150 +99,108 @@ protected function tearDown(): void public static function getSaveData(): array { - return array( - array(true), - array(false), - ); + return [[true], [false]]; } /** * @dataProvider getSaveData - * - * @param bool $withLockFile */ - public function testSave($withLockFile) + public function testSave(bool $withLockFile): void { - $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) - ->method('getRepositoryManager') - ->willReturn($rm) - ; + $rm = $this->createMock(RepositoryManager::class); - $im = $this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) - ->method('getInstallationManager') - ->willReturn($im) - ; + $this->composer->expects($this->any())->method('getRepositoryManager')->willReturn($rm); - file_put_contents($this->cwd . '/composer.json', '{}'); + $im = $this->createMock(InstallationManager::class); + + $this->composer->expects($this->any())->method('getInstallationManager')->willReturn($im); + + \file_put_contents($this->cwd . '/composer.json', '{}'); if ($withLockFile) { - file_put_contents($this->cwd . '/composer.lock', json_encode(array('content-hash' => 'HASH_VALUE'))); + \file_put_contents($this->cwd . '/composer.lock', json_encode(['content-hash' => 'HASH_VALUE'])); } - static::assertInstanceOf('Foxy\Fallback\ComposerFallback', $this->composerFallback->save()); + $this->assertInstanceOf(ComposerFallback::class, $this->composerFallback->save()); } - public function testRestoreWithDisableOption() + public function testRestoreWithDisableOption(): void { - $config = new Config(array( - 'fallback-composer' => false, - )); + $config = new Config(['fallback-composer' => false]); $composerFallback = new ComposerFallback($this->composer, $this->io, $config, $this->input); - $this->io->expects(static::never()) - ->method('write') - ; + $this->io->expects($this->never())->method('write'); $composerFallback->restore(); } public static function getRestoreData(): array { - return array( - array(array()), - array(array( - array( - 'name' => 'foo/bar', - 'version' => '1.0.0.0', - ), - )), - ); + return [[[]], [[['name' => 'foo/bar', 'version' => '1.0.0.0']]]]; } /** * @dataProvider getRestoreData */ - public function testRestore(array $packages) + public function testRestore(array $packages): void { $composerFile = 'composer.json'; $composerContent = '{}'; $lockFile = 'composer.lock'; $vendorDir = $this->cwd . '/vendor/'; - file_put_contents($this->cwd . '/' . $composerFile, $composerContent); - file_put_contents($this->cwd . '/' . $lockFile, json_encode(array( - 'content-hash' => 'HASH_VALUE', - 'packages' => $packages, - 'packages-dev' => array(), - 'prefer-stable' => true, - ))); + \file_put_contents($this->cwd . '/' . $composerFile, $composerContent); + \file_put_contents( + $this->cwd . '/' . $lockFile, + json_encode( + [ + 'content-hash' => 'HASH_VALUE', + 'packages' => $packages, + 'packages-dev' => [], + 'prefer-stable' => true, + ] + ) + ); - $this->input->expects(static::any()) + $this->input + ->expects($this->any()) ->method('getOption') - ->willReturnCallback(function ($option) { - return 'verbose' === $option ? false : null; - }) - ; - - $ed = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) - ->method('getEventDispatcher') - ->willReturn($ed) - ; - - $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) - ->method('getRepositoryManager') - ->willReturn($rm) - ; - - $im = $this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) - ->method('getInstallationManager') - ->willReturn($im) - ; - - $this->io->expects(static::once()) - ->method('write') - ; + ->willReturnCallback(fn ($option) => 'verbose' === $option ? false : null); + + $ed = $this->createMock(EventDispatcher::class); + + $this->composer->expects($this->any())->method('getEventDispatcher')->willReturn($ed); + + $rm = $this->createMock(RepositoryManager::class); + + $this->composer->expects($this->any())->method('getRepositoryManager')->willReturn($rm); + + $im = $this->createMock(InstallationManager::class); + + $this->composer->expects($this->any())->method('getInstallationManager')->willReturn($im); + $this->io->expects($this->once())->method('write'); $locker = LockerUtil::getLocker($this->io, $im, $composerFile); - $this->composer->expects(static::atLeastOnce()) - ->method('getLocker') - ->willReturn($locker) - ; + $this->composer->expects($this->atLeastOnce())->method('getLocker')->willReturn($locker); - $config = $this->getMockBuilder('Composer\Config') + $config = $this->getMockBuilder(\Composer\Config::class) ->disableOriginalConstructor() - ->onlyMethods(array('get')) + ->onlyMethods(['get']) ->getMock(); - $this->composer->expects(static::atLeastOnce()) - ->method('getConfig') - ->willReturn($config) - ; - $config->expects(static::atLeastOnce()) + $this->composer->expects($this->atLeastOnce())->method('getConfig')->willReturn($config); + + $config + ->expects($this->atLeastOnce()) ->method('get') - ->willReturnCallback(function ($key, $default = null) use ($vendorDir) { - return 'vendor-dir' === $key ? $vendorDir : $default; - }) - ; + ->willReturnCallback(fn ($key, $default = null) => 'vendor-dir' === $key ? $vendorDir : $default); if (0 === \count($packages)) { - $this->fs->expects(static::once()) - ->method('remove') - ->with($vendorDir) - ; + $this->fs->expects($this->once())->method('remove')->with($vendorDir); } else { - $this->fs->expects(static::never()) - ->method('remove') - ; - - $this->installer->expects(static::once()) - ->method('run') - ; + $this->fs->expects($this->never())->method('remove'); + $this->installer->expects($this->once())->method('run'); } $this->composerFallback->save(); diff --git a/tests/Fixtures/Util/AbstractProcessExecutorMock.php b/tests/Fixtures/Util/AbstractProcessExecutorMock.php index 3661d63..8e31c81 100644 --- a/tests/Fixtures/Util/AbstractProcessExecutorMock.php +++ b/tests/Fixtures/Util/AbstractProcessExecutorMock.php @@ -23,12 +23,12 @@ abstract class AbstractProcessExecutorMock extends ProcessExecutor /** * @var array */ - private $expectedValues = array(); + private $expectedValues = []; /** * @var array */ - private $executedCommands = array(); + private $executedCommands = []; /** * @var int @@ -37,12 +37,10 @@ abstract class AbstractProcessExecutorMock extends ProcessExecutor public function doExecute($command, &$output = null, ?string $cwd = null): int { - $expected = isset($this->expectedValues[$this->position]) - ? $this->expectedValues[$this->position] - : array(0, $output); + $expected = $this->expectedValues[$this->position] ?? [0, $output]; - list($returnedCode, $output) = $expected; - $this->executedCommands[] = array($command, $returnedCode, $output); + [$returnedCode, $output] = $expected; + $this->executedCommands[] = [$command, $returnedCode, $output]; ++$this->position; return $returnedCode; @@ -56,7 +54,7 @@ public function doExecute($command, &$output = null, ?string $cwd = null): int */ public function addExpectedValues($returnedCode = 0, $output = null) { - $this->expectedValues[] = array($returnedCode, $output); + $this->expectedValues[] = [$returnedCode, $output]; return $this; } @@ -135,7 +133,7 @@ public function getLastOutput() * * @return null|int|string */ - private function getExecutedValue($position, $index) + private function getExecutedValue($position, int $index) { return isset($this->executedCommands[$position]) ? $this->executedCommands[$position][$index] diff --git a/tests/Fixtures/Util/ProcessExecutorMock.php b/tests/Fixtures/Util/ProcessExecutorMock.php index c044db6..76a59c2 100644 --- a/tests/Fixtures/Util/ProcessExecutorMock.php +++ b/tests/Fixtures/Util/ProcessExecutorMock.php @@ -22,7 +22,7 @@ if (version_compare(Composer::VERSION, '2.3.0', '<')) { class ProcessExecutorMock extends AbstractProcessExecutorMock { - public function execute($command, &$output = null, $cwd = null) + public function execute($command, &$output = null, $cwd = null): int { return $this->doExecute($command, $output, $cwd); } diff --git a/tests/Fixtures/Util/ProcessExecutorMockTest.php b/tests/Fixtures/Util/ProcessExecutorMockTest.php index 39b1087..d3ac51e 100644 --- a/tests/Fixtures/Util/ProcessExecutorMockTest.php +++ b/tests/Fixtures/Util/ProcessExecutorMockTest.php @@ -22,28 +22,28 @@ */ final class ProcessExecutorMockTest extends \PHPUnit\Framework\TestCase { - public function testExecuteWithoutExpectedValues() + public function testExecuteWithoutExpectedValues(): void { $executor = new ProcessExecutorMock(); $executor->execute('run', $output); - static::assertSame('run', $executor->getExecutedCommand(0)); - static::assertEquals(0, $executor->getExecutedReturnedCode(0)); - static::assertNull($executor->getExecutedOutput(0)); + $this->assertSame('run', $executor->getExecutedCommand(0)); + $this->assertEquals(0, $executor->getExecutedReturnedCode(0)); + $this->assertNull($executor->getExecutedOutput(0)); - static::assertNull($executor->getExecutedCommand(1)); - static::assertNull($executor->getExecutedReturnedCode(1)); - static::assertNull($executor->getExecutedOutput(1)); + $this->assertNull($executor->getExecutedCommand(1)); + $this->assertNull($executor->getExecutedReturnedCode(1)); + $this->assertNull($executor->getExecutedOutput(1)); - static::assertSame('run', $executor->getLastCommand()); - static::assertEquals(0, $executor->getLastReturnedCode()); - static::assertNull($executor->getLastOutput()); + $this->assertSame('run', $executor->getLastCommand()); + $this->assertEquals(0, $executor->getLastReturnedCode()); + $this->assertNull($executor->getLastOutput()); - static::assertNull($output); + $this->assertNull($output); } - public function testExecuteWithExpectedValues() + public function testExecuteWithExpectedValues(): void { $executor = new ProcessExecutorMock(); @@ -53,23 +53,23 @@ public function testExecuteWithExpectedValues() $executor->execute('run', $output); $executor->execute('run2', $output2); - static::assertSame('run', $executor->getExecutedCommand(0)); - static::assertSame(0, $executor->getExecutedReturnedCode(0)); - static::assertSame('TEST', $executor->getExecutedOutput(0)); + $this->assertSame('run', $executor->getExecutedCommand(0)); + $this->assertSame(0, $executor->getExecutedReturnedCode(0)); + $this->assertSame('TEST', $executor->getExecutedOutput(0)); - static::assertSame('run2', $executor->getExecutedCommand(1)); - static::assertSame(42, $executor->getExecutedReturnedCode(1)); - static::assertSame('TEST 2', $executor->getExecutedOutput(1)); + $this->assertSame('run2', $executor->getExecutedCommand(1)); + $this->assertSame(42, $executor->getExecutedReturnedCode(1)); + $this->assertSame('TEST 2', $executor->getExecutedOutput(1)); - static::assertNull($executor->getExecutedCommand(2)); - static::assertNull($executor->getExecutedReturnedCode(2)); - static::assertNull($executor->getExecutedOutput(2)); + $this->assertNull($executor->getExecutedCommand(2)); + $this->assertNull($executor->getExecutedReturnedCode(2)); + $this->assertNull($executor->getExecutedOutput(2)); - static::assertSame('run2', $executor->getLastCommand()); - static::assertSame(42, $executor->getLastReturnedCode()); - static::assertSame('TEST 2', $executor->getLastOutput()); + $this->assertSame('run2', $executor->getLastCommand()); + $this->assertSame(42, $executor->getLastReturnedCode()); + $this->assertSame('TEST 2', $executor->getLastOutput()); - static::assertSame('TEST', $output); - static::assertSame('TEST 2', $output2); + $this->assertSame('TEST', $output); + $this->assertSame('TEST 2', $output2); } } diff --git a/tests/FoxyTest.php b/tests/FoxyTest.php index 33f6f38..d92cd24 100644 --- a/tests/FoxyTest.php +++ b/tests/FoxyTest.php @@ -1,5 +1,7 @@ composer = $this->getMockBuilder('Composer\Composer')->disableOriginalConstructor()->getMock(); - $this->composerConfig = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock(); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); + $this->composer = $this->createMock(Composer::class); + $this->composerConfig = $this->createMock(Config::class); + $this->io = $this->createMock(IOInterface::class); + $this->package = $this->createMock(RootPackageInterface::class); - $this->composer->expects(static::any()) + $this->composer + ->expects($this->any()) ->method('getPackage') - ->willReturn($this->package) - ; + ->willReturn($this->package); - $this->composer->expects(static::any()) + $this->composer + ->expects($this->any()) ->method('getConfig') - ->willReturn($this->composerConfig) - ; + ->willReturn($this->composerConfig); + + $rm = $this->createMock(RepositoryManager::class); - $rm = $this->getMockBuilder('Composer\Repository\RepositoryManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) + $this->composer + ->expects($this->any()) ->method('getRepositoryManager') - ->willReturn($rm) - ; + ->willReturn($rm); + + $im = $this->createMock(InstallationManager::class); - $im = $this->getMockBuilder('Composer\Installer\InstallationManager')->disableOriginalConstructor()->getMock(); - $this->composer->expects(static::any()) + $this->composer + ->expects($this->any()) ->method('getInstallationManager') ->willReturn($im) ; } - public function testGetSubscribedEvents() + public function testGetSubscribedEvents(): void { - static::assertCount(4, Foxy::getSubscribedEvents()); + $this->assertCount(4, Foxy::getSubscribedEvents()); } - public function testActivate() + public function testActivate(): void { $foxy = new Foxy(); $foxy->activate($this->composer, $this->io); $foxy->init(); - static::assertTrue(true); + + $this->assertTrue(true); } - public function testDeactivate() + public function testDeactivate(): void { $foxy = new Foxy(); $foxy->deactivate($this->composer, $this->io); - static::assertTrue(true); + + $this->assertTrue(true); } - public function testUninstall() + public function testUninstall(): void { $foxy = new Foxy(); $foxy->uninstall($this->composer, $this->io); - static::assertTrue(true); + + $this->assertTrue(true); } - public function testActivateOnInstall() + public function testActivateOnInstall(): void { - $package = $this->getMockBuilder('Composer\Package\Package') - ->disableOriginalConstructor() - ->getMock() - ; - $package->expects(static::once()) - ->method('getName') - ->willReturn('foxy/foxy') - ; + $package = $this->createMock(Package::class); - $operation = $this->getMockBuilder('Composer\DependencyResolver\Operation\InstallOperation') - ->disableOriginalConstructor()->getMock(); - $operation->expects(static::once()) - ->method('getPackage') - ->willReturn($package) - ; + $package->expects($this->once())->method('getName')->willReturn('foxy/foxy'); + + $operation = $this->createMock(InstallOperation::class); + + $operation->expects($this->once())->method('getPackage')->willReturn($package); /** @var MockObject|PackageEvent $event */ - $event = $this->getMockBuilder('Composer\Installer\PackageEvent')->disableOriginalConstructor()->getMock(); - $event->expects(static::once()) - ->method('getOperation') - ->willReturn($operation) - ; + $event = $this->createMock(PackageEvent::class); + + $event->expects($this->once())->method('getOperation')->willReturn($operation); $foxy = new Foxy(); + $foxy->activate($this->composer, $this->io); $foxy->initOnInstall($event); } - public function testActivateWithInvalidManager() + public function testActivateWithInvalidManager(): void { - static::expectException('Foxy\Exception\RuntimeException'); - static::expectExceptionMessage('The asset manager "invalid_manager" doesn\'t exist'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessage('The asset manager "invalid_manager" doesn\'t exist'); - $this->package->expects(static::any()) + $this->package + ->expects($this->any()) ->method('getConfig') - ->willReturn(array( - 'foxy' => array( - 'manager' => 'invalid_manager', - ), - )) - ; + ->willReturn(['foxy' => ['manager' => 'invalid_manager']]); $foxy = new Foxy(); $foxy->activate($this->composer, $this->io); @@ -157,34 +141,24 @@ public function testActivateWithInvalidManager() public static function getSolveAssetsData(): array { - return array( - array('solve_event_install', false), - array('solve_event_update', true), - ); + return [['solve_event_install', false], ['solve_event_update', true]]; } /** * @dataProvider getSolveAssetsData - * - * @param string $eventName - * @param bool $expectedUpdatable */ - public function testSolveAssets($eventName, $expectedUpdatable) + public function testSolveAssets(string $eventName, bool $expectedUpdatable): void { $event = new Event($eventName, $this->composer, $this->io); - /** @var \PHPUnit_Framework_MockObject_MockObject|SolverInterface $solver */ - $solver = $this->getMockBuilder('Foxy\Solver\SolverInterface')->getMock(); - $solver->expects(static::once()) - ->method('setUpdatable') - ->with($expectedUpdatable) - ; - $solver->expects(static::once()) - ->method('solve') - ->with($this->composer, $this->io) - ; + /** @var MockObject|SolverInterface $solver */ + $solver = $this->createMock(SolverInterface::class); + + $solver->expects($this->once())->method('setUpdatable')->with($expectedUpdatable); + $solver->expects($this->once())->method('solve')->with($this->composer, $this->io); $foxy = new Foxy(); + $foxy->setSolver($solver); $foxy->solveAssets($event); } diff --git a/tests/Json/JsonFileTest.php b/tests/Json/JsonFileTest.php index e206580..7211c01 100644 --- a/tests/Json/JsonFileTest.php +++ b/tests/Json/JsonFileTest.php @@ -1,5 +1,7 @@ cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_asset_json_file_test_', true); $this->sfs = new Filesystem(); $this->sfs->mkdir($this->cwd); - chdir($this->cwd); + + \chdir($this->cwd); } protected function tearDown(): void { parent::tearDown(); - chdir($this->oldCwd); + \chdir($this->oldCwd); + $this->sfs->remove($this->cwd); $this->sfs = null; $this->oldCwd = null; $this->cwd = null; } - public function testGetArrayKeysWithoutFile() + public function testGetArrayKeysWithoutFile(): void { $filename = './package.json'; $jsonFile = new JsonFile($filename); - static::assertSame(array(), $jsonFile->getArrayKeys()); + $this->assertSame([], $jsonFile->getArrayKeys()); } - public function testGetArrayKeysWithExistingFile() + public function testGetArrayKeysWithExistingFile(): void { - $expected = array( - 'contributors', - ); + $expected = ['contributors']; $content = <<assertFileExists($filename); $jsonFile = new JsonFile($filename); - static::assertSame($expected, $jsonFile->getArrayKeys()); + $this->assertSame($expected, $jsonFile->getArrayKeys()); } - public function testGetIndentWithoutFile() + public function testGetIndentWithoutFile(): void { $filename = './package.json'; $jsonFile = new JsonFile($filename); - static::assertSame(4, $jsonFile->getIndent()); + $this->assertSame(4, $jsonFile->getIndent()); } - public function testGetIndentWithExistingFile() + public function testGetIndentWithExistingFile(): void { - $content = <<<'JSON' -{ - "name": "test" -} -JSON; + $content = <<assertFileExists($filename); $jsonFile = new JsonFile($filename); - static::assertSame(2, $jsonFile->getIndent()); + $this->assertSame(2, $jsonFile->getIndent()); } - public function testWriteWithoutFile() + public function testWriteWithoutFile(): void { - $expected = <<<'JSON' -{ - "name": "test" -} + $expected = << 'test', - ); + $data = ['name' => 'test']; $jsonFile = new JsonFile($filename); $jsonFile->write($data); - static::assertFileExists($filename); + $this->assertFileExists($filename); $content = file_get_contents($filename); Assert::equalsWithoutLE($expected, $content); } - public function testWriteWithExistingFile() + public function testWriteWithExistingFile(): void { - $expected = <<<'JSON' -{ - "name": "test", - "contributors": [], - "dependencies": {}, - "private": true -} + $expected = <<assertFileExists($filename); $jsonFile = new JsonFile($filename); $data = (array) $jsonFile->read(); $data['private'] = true; $jsonFile->write($data); - static::assertFileExists($filename); + $this->assertFileExists($filename); $content = file_get_contents($filename); - static::assertSame($expected, $content); + $this->assertSame($expected, $content); } } diff --git a/tests/Json/JsonFormatterTest.php b/tests/Json/JsonFormatterTest.php index daaa939..83d63fe 100644 --- a/tests/Json/JsonFormatterTest.php +++ b/tests/Json/JsonFormatterTest.php @@ -1,5 +1,7 @@ 'test', - 'contributors' => array(), - 'dependencies' => array( - '@foo/bar' => '^1.0.0', - ), - 'devDependencies' => array(), - ); + 'contributors' => [], + 'dependencies' => ['@foo/bar' => '^1.0.0'], 'devDependencies' => [] + ]; $content = json_encode($data); - Assert::equalsWithoutLE($expected, JsonFormatter::format($content, array(), 2)); + Assert::equalsWithoutLE($expected, JsonFormatter::format($content, [], 2)); } public function testGetArrayKeys(): void @@ -57,9 +57,9 @@ public function testGetArrayKeys(): void "dependencies": {} } JSON; - $expected = array('contributors'); + $expected = ['contributors']; - static::assertSame($expected, JsonFormatter::getArrayKeys($content)); + $this->assertSame($expected, JsonFormatter::getArrayKeys($content)); } public function testGetIndent(): void @@ -76,9 +76,7 @@ public function testGetIndent(): void public function testUnescapeUnicode(): void { - $data = array( - 'name' => '\u0048\u0065\u006c\u006c\u006f', // Hello en unicode - ); + $data = ['name' => '\u0048\u0065\u006c\u006c\u006f']; $content = json_encode($data); $expected = << 'https:\/\/example.com', // URL con barras diagonales escapadas - ); + $data = ['url' => 'https:\/\/example.com']; $content = json_encode($data); - $expected = <<<'JSON' + $expected = <<oldCwd = getcwd(); $this->cwd = sys_get_temp_dir() . \DIRECTORY_SEPARATOR . uniqid('foxy_solver_test_', true); - $this->config = new Config(array( - 'enabled' => true, - 'composer-asset-dir' => $this->cwd . '/composer-asset-dir', - )); - $this->composer = $this->getMockBuilder('Composer\Composer')->disableOriginalConstructor()->getMock(); - $this->composerConfig = $this->getMockBuilder('Composer\Config')->disableOriginalConstructor()->getMock(); - $this->io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); - $this->fs = $this->getMockBuilder('Composer\Util\Filesystem')->disableOriginalConstructor()->getMock(); - $this->im = $this->getMockBuilder('Composer\Installer\InstallationManager') - ->disableOriginalConstructor() - ->onlyMethods(array('getInstallPath')) - ->getMock(); + $this->config = new Config(['enabled' => true, 'composer-asset-dir' => $this->cwd . '/composer-asset-dir']); + $this->composer = $this->createMock(Composer::class); + $this->composerConfig = $this->createMock(\Composer\Config::class); + $this->io = $this->createMock(IOInterface::class); + $this->fs = $this->createMock(Filesystem::class); + $this->im = $this->createMock(InstallationManager::class); $this->sfs = new \Symfony\Component\Filesystem\Filesystem(); - $this->package = $this->getMockBuilder('Composer\Package\RootPackageInterface')->getMock(); - $this->manager = $this->getMockBuilder('Foxy\Asset\AssetManagerInterface')->getMock(); - $this->composerFallback = $this->getMockBuilder('Foxy\Fallback\FallbackInterface')->getMock(); + $this->package = $this->createMock(RootPackageInterface::class); + $this->manager = $this->createMock(AssetManagerInterface::class); + $this->composerFallback = $this->createMock(FallbackInterface::class); $this->sfs->mkdir($this->cwd); - chdir($this->cwd); - $this->localRepo = $this->getMockBuilder('Composer\Repository\InstalledArrayRepository') - ->onlyMethods(array('getCanonicalPackages')) - ->getMock() - ; + \chdir($this->cwd); - if (class_exists('Composer\Util\HttpDownloader')) { + $this->localRepo = $this->createMock(InstalledArrayRepository::class); + + if (\class_exists(\Composer\Util\HttpDownloader::class)) { $rm = new RepositoryManager($this->io, $this->composerConfig, new HttpDownloader($this->io, $this->composerConfig)); $rm->setLocalRepository($this->localRepo); } else { @@ -146,38 +87,21 @@ protected function setUp(): void $rm->setLocalRepository($this->localRepo); } - $this->composer->expects(static::any()) - ->method('getRepositoryManager') - ->willReturn($rm) - ; - - $this->composer->expects(static::any()) - ->method('getInstallationManager') - ->willReturn($this->im) - ; - - $this->composer->expects(static::any()) - ->method('getPackage') - ->willReturn($this->package) - ; - - $this->composer->expects(static::any()) - ->method('getConfig') - ->willReturn($this->composerConfig) - ; - - $this->composer->expects(static::any()) + $this->composer->expects($this->any())->method('getRepositoryManager')->willReturn($rm); + $this->composer->expects($this->any())->method('getInstallationManager')->willReturn($this->im); + $this->composer->expects($this->any())->method('getPackage')->willReturn($this->package); + $this->composer->expects($this->any())->method('getConfig')->willReturn($this->composerConfig); + $this->composer + ->expects($this->any()) ->method('getEventDispatcher') - ->willReturn(new EventDispatcher($this->composer, $this->io)) - ; + ->willReturn(new EventDispatcher($this->composer, $this->io)); $sfs = $this->sfs; - $this->fs->expects(static::any()) + + $this->fs + ->expects($this->any()) ->method('findShortestPath') - ->willReturnCallback(function ($from, $to) use ($sfs) { - return rtrim($sfs->makePathRelative($to, $from), '/'); - }) - ; + ->willReturnCallback(fn ($from, $to) => rtrim($sfs->makePathRelative($to, $from), '/')); $this->solver = new Solver($this->manager, $this->config, $this->fs, $this->composerFallback); } @@ -204,36 +128,25 @@ protected function tearDown(): void $this->cwd = null; } - public function testSetUpdatable() + public function testSetUpdatable(): void { - $this->manager->expects(static::once()) - ->method('setUpdatable') - ->with(false) - ; - + $this->manager->expects($this->once())->method('setUpdatable')->with(false); $this->solver->setUpdatable(false); } - public function testSolveWithDisableOption() + public function testSolveWithDisableOption(): void { - $config = new Config(array( - 'enabled' => false, - )); + $config = new Config(['enabled' => false]); $solver = new Solver($this->manager, $config, $this->fs); - $this->manager->expects(static::never()) - ->method('run') - ; + $this->manager->expects($this->never())->method('run'); $solver->solve($this->composer, $this->io); } public static function getSolveData(): array { - return array( - array(0), - array(1), - ); + return [[0], [1]]; } /** @@ -241,70 +154,42 @@ public static function getSolveData(): array * * @param int $resRunManager The result value of the run command of asset manager */ - public function testSolve($resRunManager) + public function testSolve(int $resRunManager): void { - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $requirePackage */ - $requirePackage = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $requirePackage->expects(static::any()) - ->method('getPrettyVersion') - ->willReturn('1.0.0') - ; - $requirePackage->expects(static::any()) - ->method('getName') - ->willReturn('foo/bar') - ; - $requirePackage->expects(static::any()) - ->method('getRequires') - ->willReturn(array( - new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0')), - )) - ; - $requirePackage->expects(static::any()) - ->method('getDevRequires') - ->willReturn(array()) - ; - - $this->addInstalledPackages(array( - $requirePackage, - )); - - $requirePackagePath = $this->cwd . '/vendor/foo/bar'; + /** @var PackageInterface|MockObject $requirePackage */ + $requirePackage = $this->createMock(PackageInterface::class); - $this->im->expects(static::once()) - ->method('getInstallPath') - ->willReturn($requirePackagePath) - ; + $requirePackage->expects($this->any())->method('getPrettyVersion')->willReturn('1.0.0'); + $requirePackage->expects($this->any())->method('getName')->willReturn('foo/bar'); + $requirePackage + ->expects($this->any()) + ->method('getRequires') + ->willReturn([new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))]); + $requirePackage->expects($this->any())->method('getDevRequires')->willReturn([]); - $this->manager->expects(static::exactly(2)) - ->method('getPackageName') - ->willReturn('package.json') - ; + $this->addInstalledPackages([$requirePackage]); - $this->manager->expects(static::once()) - ->method('addDependencies') - ; + $requirePackagePath = $this->cwd . '/vendor/foo/bar'; - $this->manager->expects(static::once()) - ->method('run') - ->willReturn($resRunManager) - ; + $this->im->expects($this->once())->method('getInstallPath')->willReturn($requirePackagePath); + $this->manager->expects($this->exactly(2))->method('getPackageName')->willReturn('package.json'); + $this->manager->expects($this->once())->method('addDependencies'); + $this->manager->expects($this->once())->method('run')->willReturn($resRunManager); if (0 === $resRunManager) { - $this->composerFallback->expects(static::never()) - ->method('restore') - ; + $this->composerFallback->expects($this->never())->method('restore'); } else { - $this->composerFallback->expects(static::once()) - ->method('restore') - ; + $this->composerFallback->expects($this->once())->method('restore'); $this->expectException('RuntimeException'); $this->expectExceptionMessage('The asset manager ended with an error'); } $requirePackageFilename = $requirePackagePath . \DIRECTORY_SEPARATOR . $this->manager->getPackageName(); + $this->sfs->mkdir(\dirname($requirePackageFilename)); - file_put_contents($requirePackageFilename, '{}'); + + \file_put_contents($requirePackageFilename, '{}'); $this->solver->solve($this->composer, $this->io); } @@ -314,11 +199,8 @@ public function testSolve($resRunManager) * * @param PackageInterface[] $packages The installed packages */ - protected function addInstalledPackages(array $packages = array()) + private function addInstalledPackages(array $packages = []): void { - $this->localRepo->expects(static::any()) - ->method('getCanonicalPackages') - ->willReturn($packages) - ; + $this->localRepo->expects($this->any())->method('getCanonicalPackages')->willReturn($packages); } } diff --git a/tests/Util/AssetUtilTest.php b/tests/Util/AssetUtilTest.php index 39b501c..b45b775 100644 --- a/tests/Util/AssetUtilTest.php +++ b/tests/Util/AssetUtilTest.php @@ -1,5 +1,7 @@ cwd = null; } - public function testGetName() + public function testGetName(): void { - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $package */ - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::once()) - ->method('getName') - ->willReturn('foo/bar') - ; - - static::assertSame('@composer-asset/foo--bar', AssetUtil::getName($package)); + $package = $this->createMock(PackageInterface::class); + $package->expects($this->once())->method('getName')->willReturn('foo/bar'); + + $this->assertSame('@composer-asset/foo--bar', AssetUtil::getName($package)); } - public function testGetPathWithoutRequiredFoxy() + public function testGetPathWithoutRequiredFoxy(): void { - /** @var InstallationManager|\PHPUnit_Framework_MockObject_MockObject $installationManager */ - $installationManager = $this->getMockBuilder('Composer\Installer\InstallationManager') - ->disableOriginalConstructor() - ->onlyMethods(array('getInstallPath')) - ->getMock() - ; - $installationManager->expects(static::never()) - ->method('getInstallPath') - ; - - /** @var AbstractAssetManager|\PHPUnit_Framework_MockObject_MockObject $assetManager */ - $assetManager = $this->getMockBuilder('Foxy\Asset\AbstractAssetManager') - ->disableOriginalConstructor() - ->getMockForAbstractClass() - ; - - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $package */ - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::once()) - ->method('getRequires') - ->willReturn(array()) - ; - $package->expects(static::once()) - ->method('getDevRequires') - ->willReturn(array()) - ; + + $installationManager = $this->createMock(InstallationManager::class); + + $installationManager->expects($this->never())->method('getInstallPath'); + + $assetManager = $this->createMock(AbstractAssetManager::class); + + /** @var PackageInterface|MockObject $package */ + $package = $this->createMock(PackageInterface::class); + + $package->expects($this->once())->method('getRequires')->willReturn([]); + $package->expects($this->once())->method('getDevRequires')->willReturn([]); $res = AssetUtil::getPath($installationManager, $assetManager, $package); - static::assertNull($res); + $this->assertNull($res); } public static function getRequiresData(): array { - return array( - array(array(new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))), array(), false), - array(array(), array(new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))), false), - array(array(new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))), array(), true), - array(array(), array(new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))), true), - ); + return [ + [ + [new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))], + [], + false, + ], + [ + [], + [new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))], + false, + ], + [ + [new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))], + [], + true, + ], + [ + [], + [new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0'))], + true, + ], + ]; } /** @@ -117,275 +111,234 @@ public static function getRequiresData(): array * * @param Link[] $requires * @param Link[] $devRequires - * @param bool $fileExists */ - public function testGetPathWithRequiredFoxy(array $requires, array $devRequires, $fileExists = false) + public function testGetPathWithRequiredFoxy(array $requires, array $devRequires, bool $fileExists = false): void { - $installationManager = $this->getMockBuilder('Composer\Installer\InstallationManager') - ->disableOriginalConstructor() - ->onlyMethods(['getInstallPath']) - ->getMock(); - ; - $installationManager->expects(static::once())->method('getInstallPath')->willReturn($this->cwd); - $assetManager = $this->getMockBuilder('Foxy\Asset\AbstractAssetManager') + $installationManager = $this->createMock(InstallationManager::class); + + $installationManager->expects($this->once())->method('getInstallPath')->willReturn($this->cwd); + + /** @var AbstractAssetManager|MockObject $assetManager */ + $assetManager = $this + ->getMockBuilder(AbstractAssetManager::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::once())->method('getRequires')->willReturn($requires); + + $package = $this->createMock(PackageInterface::class); + + $package->expects($this->once())->method('getRequires')->willReturn($requires); if (0 === \count($devRequires)) { - $package->expects(static::never()) - ->method('getDevRequires') - ; + $package->expects($this->never())->method('getDevRequires'); } else { - $package->expects(static::once()) - ->method('getDevRequires') - ->willReturn($devRequires) - ; + $package->expects($this->once())->method('getDevRequires')->willReturn($devRequires); } if ($fileExists) { $expectedFilename = $this->cwd . \DIRECTORY_SEPARATOR . $assetManager->getPackageName(); - file_put_contents($expectedFilename, '{}'); - $expectedFilename = str_replace('\\', '/', realpath($expectedFilename)); + + \file_put_contents($expectedFilename, '{}'); + + $expectedFilename = \str_replace('\\', '/', \realpath($expectedFilename)); } else { $expectedFilename = null; } $res = AssetUtil::getPath($installationManager, $assetManager, $package); - static::assertSame($expectedFilename, $res); + $this->assertSame($expectedFilename, $res); } public static function getExtraData(): array { - return array( - array(false, false), - array(true, false), - array(false, true), - array(true, true), - ); + return [[false, false], [true, false], [false, true], [true, true]]; } /** * @dataProvider getExtraData - * - * @param bool $withExtra - * @param bool $fileExists */ - public function testGetPathWithExtraActivation($withExtra, $fileExists = false) + public function testGetPathWithExtraActivation(bool $withExtra, bool $fileExists = false): void { - $installationManager = $this->getMockBuilder('Composer\Installer\InstallationManager') - ->disableOriginalConstructor() - ->onlyMethods(array('getInstallPath')) - ->getMock(); + $installationManager = $this->createMock(InstallationManager::class); if ($withExtra && $fileExists) { - $installationManager->expects(static::once()) - ->method('getInstallPath') - ->willReturn($this->cwd) - ; + $installationManager->expects($this->once())->method('getInstallPath')->willReturn($this->cwd); } - $assetManager = $this->getMockBuilder('Foxy\Asset\AbstractAssetManager') + + /** @var AbstractAssetManager|MockObject $assetManager */ + $assetManager = $this + ->getMockBuilder(AbstractAssetManager::class) ->disableOriginalConstructor() ->getMockForAbstractClass(); - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::any()) - ->method('getRequires') - ->willReturn(array()) - ; + $package = $this->createMock(PackageInterface::class); - $package->expects(static::any()) - ->method('getDevRequires') - ->willReturn(array()) - ; - - $package->expects(static::atLeastOnce()) - ->method('getExtra') - ->willReturn(array( - 'foxy' => $withExtra, - )) - ; + $package->expects($this->any())->method('getRequires')->willReturn([]); + $package->expects($this->any())->method('getDevRequires')->willReturn([]); + $package->expects($this->atLeastOnce())->method('getExtra')->willReturn(['foxy' => $withExtra]); if ($fileExists) { $expectedFilename = $this->cwd . \DIRECTORY_SEPARATOR . $assetManager->getPackageName(); - file_put_contents($expectedFilename, '{}'); - $expectedFilename = $withExtra ? str_replace('\\', '/', realpath($expectedFilename)) : null; + + \file_put_contents($expectedFilename, '{}'); + + $expectedFilename = $withExtra ? \str_replace('\\', '/', \realpath($expectedFilename)) : null; } else { $expectedFilename = null; } $res = AssetUtil::getPath($installationManager, $assetManager, $package); - static::assertSame($expectedFilename, $res); + $this->assertSame($expectedFilename, $res); } - public function testHasNoPluginDependency() + public function testHasNoPluginDependency(): void { - static::assertFalse(AssetUtil::hasPluginDependency(array( - new Link('root/package', 'foo/bar', new Constraint('=', '1.0.0')), - ))); + $this->assertFalse( + AssetUtil::hasPluginDependency([new Link('root/package', 'foo/bar', new Constraint('=', '1.0.0'))]) + ); } - public function testHasPluginDependency() + public function testHasPluginDependency(): void { - static::assertTrue(AssetUtil::hasPluginDependency(array( - new Link('root/package', 'foo/bar', new Constraint('=', '1.0.0')), - new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0')), - new Link('root/package', 'bar/foo', new Constraint('=', '1.0.0')), - ))); + $this->assertTrue( + AssetUtil::hasPluginDependency( + [ + new Link('root/package', 'foo/bar', new Constraint('=', '1.0.0')), + new Link('root/package', 'foxy/foxy', new Constraint('=', '1.0.0')), + new Link('root/package', 'bar/foo', new Constraint('=', '1.0.0')) + ], + ) + ); } public static function getIsProjectActivationData(): array { - return array( - array('full/qualified', true), - array('full-disable/qualified', false), - array('foo/bar', true), - array('baz/foo', false), - array('baz/foo-test', false), - array('bar/test', true), - array('other/package', false), - array('test-string/package', true), - ); + return [ + ['full/qualified', true], + ['full-disable/qualified', false], + ['foo/bar', true], + ['baz/foo', false], + ['baz/foo-test', false], + ['bar/test', true], + ['other/package', false], + ['test-string/package', true], + ]; } /** * @dataProvider getIsProjectActivationData - * - * @param string $packageName - * @param bool $expected */ - public function testIsProjectActivation($packageName, $expected) + public function testIsProjectActivation(string $packageName, bool $expected): void { - $enablePackages = array( + $enablePackages = [ 0 => 'test-string/*', 'foo/*' => true, 'baz/foo' => false, '/^bar\/*/' => true, 'full/qualified' => true, 'full-disable/qualified' => false, - ); + ]; - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $package */ - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::once()) - ->method('getName') - ->willReturn($packageName) - ; + /** @var PackageInterface|MockObject $package */ + $package = $this->createMock(PackageInterface::class); + + $package->expects($this->once())->method('getName')->willReturn($packageName); $res = AssetUtil::isProjectActivation($package, $enablePackages); - static::assertSame($expected, $res); + + $this->assertSame($expected, $res); } public static function getIsProjectActivationWithWildcardData(): array { - return array( - array('full/qualified', true), - array('full-disable/qualified', false), - array('foo/bar', true), - array('baz/foo', false), - array('baz/foo-test', false), - array('bar/test', true), - array('other/package', true), - array('test-string/package', true), - ); + return [ + ['full/qualified', true], + ['full-disable/qualified', false], + ['foo/bar', true], + ['baz/foo', false], + ['baz/foo-test', false], + ['bar/test', true], + ['other/package', true], + ['test-string/package', true], + ]; } /** * @dataProvider getIsProjectActivationWithWildcardData - * - * @param string $packageName - * @param bool $expected */ - public function testIsProjectActivationWithWildcardPattern($packageName, $expected) + public function testIsProjectActivationWithWildcardPattern(string $packageName, bool $expected): void { - $enablePackages = array( + $enablePackages = [ 'baz/foo*' => false, 'full-disable/qualified' => false, '*' => true, - ); + ]; + + /** @var PackageInterface|MockObject $package */ + $package = $this->createMock(PackageInterface::class); - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $package */ - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); - $package->expects(static::once()) - ->method('getName') - ->willReturn($packageName) - ; + $package->expects($this->once())->method('getName')->willReturn($packageName); $res = AssetUtil::isProjectActivation($package, $enablePackages); - static::assertSame($expected, $res); + + $this->assertSame($expected, $res); } public static function getFormatPackageData(): array { - return array( - array('1.0.0', null, '1.0.0'), - array('1.0.1', '1.0.0', '1.0.0'), - array('1.0.0.x-dev', null, '1.0.0'), - array('1.0.0.x', null, '1.0.0'), - array('1.0.0.1', null, '1.0.0'), - array('dev-master', null, '1.0.0', '1-dev'), - array('dev-master', null, '1.0.0', '1.0-dev'), - array('dev-master', null, '1.0.0', '1.0.0-dev'), - array('dev-master', null, '1.0.0', '1.x-dev'), - array('dev-master', null, '1.0.0', '1.0.x-dev'), - array('dev-master', null, '1.0.0', '1.*-dev'), - array('dev-master', null, '1.0.0', '1.0.*-dev'), - ); + return [ + ['1.0.0', null, '1.0.0'], + ['1.0.1', '1.0.0', '1.0.0'], + ['1.0.0.x-dev', null, '1.0.0'], + ['1.0.0.x', null, '1.0.0'], + ['1.0.0.1', null, '1.0.0'], + ['dev-master', null, '1.0.0', '1-dev'], + ['dev-master', null, '1.0.0', '1.0-dev'], + ['dev-master', null, '1.0.0', '1.0.0-dev'], + ['dev-master', null, '1.0.0', '1.x-dev'], + ['dev-master', null, '1.0.0', '1.0.x-dev'], + ['dev-master', null, '1.0.0', '1.*-dev'], + ['dev-master', null, '1.0.0', '1.0.*-dev'], + ]; } /** * @dataProvider getFormatPackageData - * - * @param string $packageVersion - * @param null|string $assetVersion - * @param string $expectedAssetVersion - * @param null|string $branchAlias */ - public function testFormatPackage($packageVersion, $assetVersion, $expectedAssetVersion, $branchAlias = null) - { + public function testFormatPackage( + string $packageVersion, + string|null $assetVersion, + string $expectedAssetVersion, + string $branchAlias = null + ): void { $packageName = '@composer-asset/foo--bar'; - /** @var PackageInterface|\PHPUnit_Framework_MockObject_MockObject $package */ - $package = $this->getMockBuilder('Composer\Package\PackageInterface')->getMock(); + /** @var PackageInterface|MockObject $package */ + $package = $this->createMock(PackageInterface::class); - $assetPackage = array(); + $assetPackage = []; if (null !== $assetVersion) { $assetPackage['version'] = $assetVersion; - $package->expects(static::never()) - ->method('getPrettyVersion') - ; - $package->expects(static::never()) - ->method('getExtra') - ; + $package->expects($this->never())->method('getPrettyVersion'); + $package->expects($this->never())->method('getExtra'); } else { - $extra = array(); + $extra = []; if (null !== $branchAlias) { $extra['branch-alias'][$packageVersion] = $branchAlias; } - $package->expects(static::once()) - ->method('getPrettyVersion') - ->willReturn($packageVersion) - ; - $package->expects(static::once()) - ->method('getExtra') - ->willReturn($extra) - ; + $package->expects($this->once())->method('getPrettyVersion')->willReturn($packageVersion); + $package->expects($this->once())->method('getExtra')->willReturn($extra); } - $expected = array( - 'name' => $packageName, - 'version' => $expectedAssetVersion, - ); + $expected = ['name' => $packageName, 'version' => $expectedAssetVersion]; $res = AssetUtil::formatPackage($package, $packageName, $assetPackage); - static::assertEquals($expected, $res); + $this->assertEquals($expected, $res); } } diff --git a/tests/Util/ComposerUtilTest.php b/tests/Util/ComposerUtilTest.php index d772c2e..6c4f9ff 100644 --- a/tests/Util/ComposerUtilTest.php +++ b/tests/Util/ComposerUtilTest.php @@ -1,5 +1,7 @@ assertTrue(true, 'Composer\'s version is valid'); } else { - $this->expectException('Foxy\Exception\RuntimeException'); - $this->expectExceptionMessageMatches('/Foxy requires the Composer\'s minimum version "([\d\.^|, ]+)", current version is "([\d\.]+)"/'); + $this->expectException(\Foxy\Exception\RuntimeException::class); + $this->expectExceptionMessageMatches( + '/Foxy requires the Composer\'s minimum version "([\d\.^|, ]+)", current version is "([\d\.]+)"/' + ); } ComposerUtil::validateVersion($requiredVersion, $composerVersion); diff --git a/tests/Util/ConsoleUtilTest.php b/tests/Util/ConsoleUtilTest.php index 5397793..85d8829 100644 --- a/tests/Util/ConsoleUtilTest.php +++ b/tests/Util/ConsoleUtilTest.php @@ -1,5 +1,7 @@ assertSame($input, ConsoleUtil::getInput($io)); } - public function testGetInputWithoutValidInput() + public function testGetInputWithoutValidInput(): void { /** @var IOInterface $io */ - $io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock(); + $io = $this->createMock(IOInterface::class); - static::assertInstanceOf('Symfony\Component\Console\Input\ArgvInput', ConsoleUtil::getInput($io)); + $this->assertInstanceOf(ArgvInput::class, ConsoleUtil::getInput($io)); } public static function getPreferredInstallOptionsData(): array { - return array( - array(false, false, 'auto', false), - array(false, true, 'auto', true), - array(true, false, 'source', false), - array(false, true, 'dist', false), - ); + return [ + [false, false, 'auto', false], + [false, true, 'auto', true], + [true, false, 'source', false], + [false, true, 'dist', false], + ]; } /** * @dataProvider getPreferredInstallOptionsData - * - * @param bool $expectedPreferSource - * @param bool $expectedPreferDist - * @param string $preferedInstall - * @param bool $inputPrefer */ - public function testGetPreferredInstallOptions($expectedPreferSource, $expectedPreferDist, $preferedInstall, $inputPrefer) - { - $config = $this->getMockBuilder(Config::class)->disableOriginalConstructor()->onlyMethods(['get'])->getMock(); - $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); + public function testGetPreferredInstallOptions( + bool $expectedPreferSource, + bool $expectedPreferDist, + string $preferedInstall, + bool $inputPrefer + ): void { + $config = $this->createMock(Config::class); + $input = $this->createMock(InputInterface::class); - $config->expects(static::once()) - ->method('get') - ->with('preferred-install') - ->willReturn($preferedInstall) - ; + $config->expects($this->once())->method('get')->with('preferred-install')->willReturn($preferedInstall); if ($inputPrefer) { - $input->expects(static::atLeastOnce()) + $input->expects($this->atLeastOnce()) ->method('getOption') - ->willReturnCallback(static function ($option) { - return !('prefer-source' === $option); - }) + ->willReturnCallback(static fn ($option) => !('prefer-source' === $option)) ; } $res = ConsoleUtil::getPreferredInstallOptions($config, $input); - static::assertEquals(array($expectedPreferSource, $expectedPreferDist), $res); + $this->assertEquals([$expectedPreferSource, $expectedPreferDist], $res); } } diff --git a/tests/Util/PackageUtilTest.php b/tests/Util/PackageUtilTest.php index 4cbf5c1..1c1ad64 100644 --- a/tests/Util/PackageUtilTest.php +++ b/tests/Util/PackageUtilTest.php @@ -1,5 +1,7 @@ array( - array( - 'name' => 'foo/bar', - 'version' => '1.0.0.0', - ), - ), - 'packages-dev' => array( - array( - 'name' => 'bar/foo', - 'version' => '1.0.0.0', - ), - ), - ); + $lockData = [ + 'packages' => [ + ['name' => 'foo/bar', 'version' => '1.0.0.0'] + ], + 'packages-dev' => [ + ['name' => 'bar/foo', 'version' => '1.0.0.0'] + ], + ]; $package = new CompletePackage('foo/bar', '1.0.0.0', '1.0.0.0'); $package->setType('library'); @@ -46,62 +42,60 @@ public function testLoadLockPackages() $packageDev = new CompletePackage('bar/foo', '1.0.0.0', '1.0.0.0'); $packageDev->setType('library'); - $expectedPackages = array( - $package, - ); - $expectedDevPackages = array( - $packageDev, - ); + $expectedPackages = [$package]; + $expectedDevPackages = [$packageDev]; $lockDataLoaded = PackageUtil::loadLockPackages($lockData); - static::assertArrayHasKey('packages', $lockDataLoaded); - static::assertArrayHasKey('packages-dev', $lockDataLoaded); - static::assertEquals($lockDataLoaded['packages'], $expectedPackages); - static::assertEquals($lockDataLoaded['packages-dev'], $expectedDevPackages); + $this->assertArrayHasKey('packages', $lockDataLoaded); + $this->assertArrayHasKey('packages-dev', $lockDataLoaded); + $this->assertEquals($lockDataLoaded['packages'], $expectedPackages); + $this->assertEquals($lockDataLoaded['packages-dev'], $expectedDevPackages); } - public function testLoadLockPackagesWithoutPackages() + public function testLoadLockPackagesWithoutPackages(): void { - static::assertSame(array(), PackageUtil::loadLockPackages(array())); + $this->assertSame([], PackageUtil::loadLockPackages([])); } - public function testConvertLockAlias() + public function testConvertLockAlias(): void { - $lockData = array( - 'aliases' => array( - array( + $lockData = [ + 'aliases' => [ + [ 'alias' => '1.0.0', - 'alias_normalized' => '1.0.0.0', + 'alias_normalized' => + '1.0.0.0', 'version' => 'dev-feature/1.0-test', 'package' => 'foo/bar', - ), - array( + ], + [ 'alias' => '2.2.0', 'alias_normalized' => '2.2.0.0', 'version' => 'dev-feature/2.2-test', 'package' => 'foo/baz', - ), - ), - ); - $expectedAliases = array( - 'foo/bar' => array( - 'dev-feature/1.0-test' => array( + ], + ], + ]; + + $expectedAliases = [ + 'foo/bar' => [ + 'dev-feature/1.0-test' => [ 'alias' => '1.0.0', - 'alias_normalized' => '1.0.0.0', - ), - ), - 'foo/baz' => array( - 'dev-feature/2.2-test' => array( + 'alias_normalized' => '1.0.0.0' + ] + ], + 'foo/baz' => [ + 'dev-feature/2.2-test' => [ 'alias' => '2.2.0', 'alias_normalized' => '2.2.0.0', - ), - ), - ); + ], + ], + ]; $convertedAliases = PackageUtil::convertLockAlias($lockData); - static::assertArrayHasKey('aliases', $convertedAliases); - static::assertEquals($convertedAliases['aliases'], $expectedAliases); + $this->assertArrayHasKey('aliases', $convertedAliases); + $this->assertEquals($convertedAliases['aliases'], $expectedAliases); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index 3a350f5..0000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,16 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require __DIR__ . '/../vendor/autoload.php'; - -if (!class_exists('\PHPUnit_Framework_TestCase') && class_exists('\PHPUnit\Framework\TestCase')) { - class_alias('\PHPUnit\Framework\TestCase', '\PHPUnit_Framework_TestCase'); -}