From 45c2dbade582c708e5f3293b85bf162a03c2c1e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 28 Dec 2021 13:06:49 +0100 Subject: [PATCH 1/3] Update test suite to support PHPUnit 5 --- composer.json | 2 +- tests/Command/BuildTest.php | 12 +-- tests/Command/InstallTest.php | 51 ++++------- tests/Command/SearchTest.php | 155 +++++++++++++--------------------- tests/Phar/TargetPharTest.php | 15 +--- 5 files changed, 86 insertions(+), 149 deletions(-) diff --git a/composer.json b/composer.json index 80bf802..df46f62 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/process": "^5.0 || ^4.0 || ^3.0 || ^2.5" }, "require-dev": { - "phpunit/phpunit": "^4.8" + "phpunit/phpunit": "^5.7 || ^4.8" }, "autoload": { "psr-0": {"Clue": "src/"} diff --git a/tests/Command/BuildTest.php b/tests/Command/BuildTest.php index fa2056e..552f59e 100644 --- a/tests/Command/BuildTest.php +++ b/tests/Command/BuildTest.php @@ -17,18 +17,18 @@ public function testCtorCreatesPackager() public function testExecuteBuildWillBuildPharer() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', null); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->never())->method('setTarget'); $pharer->expects($this->once())->method('build'); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); @@ -38,18 +38,18 @@ public function testExecuteBuildWillBuildPharer() public function testExecuteBuildWillBuildPharerWithExplicitTarget() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', 'targetDir'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('setTarget')->with('targetDir'); $pharer->expects($this->once())->method('build'); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); diff --git a/tests/Command/InstallTest.php b/tests/Command/InstallTest.php index 7cc2560..7b70a81 100644 --- a/tests/Command/InstallTest.php +++ b/tests/Command/InstallTest.php @@ -18,18 +18,18 @@ public function testCtorCreatesPackager() public function testExecuteInstallWillInstallPackager() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', null); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $package = $this->getMockBuilder('Clue\PharComposer\Package\Package')->disableOriginalConstructor()->getMock(); $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('getPackageRoot')->willReturn($package); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); $packager->expects($this->once())->method('getSystemBin')->with($package, null)->willReturn('targetPath'); @@ -41,18 +41,18 @@ public function testExecuteInstallWillInstallPackager() public function testExecuteInstallWillInstallPackagerWithExplicitTarget() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', 'targetDir'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $package = $this->getMockBuilder('Clue\PharComposer\Package\Package')->disableOriginalConstructor()->getMock(); $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('getPackageRoot')->willReturn($package); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); $packager->expects($this->once())->method('getSystemBin')->with($package, 'targetDir')->willReturn('targetPath'); @@ -62,32 +62,16 @@ public function testExecuteInstallWillInstallPackagerWithExplicitTarget() $command->run($input, $output); } - /** - * @doesNotPerformAssertions - */ - public function testNotBlockedByLegacyInstallation() - { - // Symfony 5+ added parameter type declarations, so we can use this to check which version is installed - $ref = new ReflectionMethod('Symfony\Component\Console\Command\Command', 'setName'); - $params = $ref->getParameters(); - if (PHP_VERSION_ID >= 70000 && isset($params[0]) && $params[0]->hasType()) { - $this->markTestSkipped('Unable to run this test (mocked QuestionHelper) with legacy PHPUnit against Symfony v5+'); - } - } - - /** - * @depends testNotBlockedByLegacyInstallation - */ public function testExecuteInstallWillInstallPackagerWhenTargetPathAlreadyExistsAndDialogQuestionYieldsYes() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', null); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willReturn(true); $helpers = new HelperSet(array( @@ -98,7 +82,7 @@ public function testExecuteInstallWillInstallPackagerWhenTargetPathAlreadyExists $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('getPackageRoot')->willReturn($package); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); $packager->expects($this->once())->method('getSystemBin')->with($package, null)->willReturn(__FILE__); @@ -109,20 +93,17 @@ public function testExecuteInstallWillInstallPackagerWhenTargetPathAlreadyExists $command->run($input, $output); } - /** - * @depends testNotBlockedByLegacyInstallation - */ public function testExecuteInstallWillNotInstallPackagerWhenTargetPathAlreadyExistsAndDialogQuestionShouldNotOverwrite() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->exactly(2))->method('getArgument')->withConsecutive( array('project'), array('target') )->willReturnOnConsecutiveCalls('dir', null); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $output->expects($this->once())->method('writeln')->with('Aborting'); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willReturn(false); $helpers = new HelperSet(array( @@ -133,7 +114,7 @@ public function testExecuteInstallWillNotInstallPackagerWhenTargetPathAlreadyExi $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('getPackageRoot')->willReturn($package); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('setOutput')->with($output); $packager->expects($this->once())->method('getPharer')->with('dir')->willReturn($pharer); $packager->expects($this->once())->method('getSystemBin')->with($package, null)->willReturn(__FILE__); @@ -146,8 +127,8 @@ public function testExecuteInstallWillNotInstallPackagerWhenTargetPathAlreadyExi public function testExecuteInstallWillReportErrorOnWindows() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $output->expects($this->once())->method('writeln')->with($this->stringContains('platform')); $command = new Install(null, true); diff --git a/tests/Command/SearchTest.php b/tests/Command/SearchTest.php index 0084738..90fcd55 100644 --- a/tests/Command/SearchTest.php +++ b/tests/Command/SearchTest.php @@ -22,40 +22,26 @@ public function testCtorCreatesPackagerAndPackagist() $this->assertInstanceOf('Packagist\Api\Client', $packagist); } - /** - * @doesNotPerformAssertions - */ - public function testNotBlockedByLegacyInstallation() - { - // Symfony 5+ added parameter type declarations, so we can use this to check which version is installed - $ref = new ReflectionMethod('Symfony\Component\Console\Command\Command', 'setName'); - $params = $ref->getParameters(); - if (PHP_VERSION_ID >= 70000 && isset($params[0]) && $params[0]->hasType()) { - $this->markTestSkipped('Unable to run this test (mocked QuestionHelper) with legacy PHPUnit against Symfony v5+'); - } - } - /** * @expectedException RuntimeException * @expectedExceptionMessage stop1 - * @depends testNotBlockedByLegacyInstallation */ public function testExecuteWithoutProjectWillAskForProjectAndRunSearch() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn(null); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willReturn('foo'); $helpers = new HelperSet(array( 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willThrowException(new RuntimeException('stop1')); $command = new Search($packager, $packagist, false); @@ -66,24 +52,23 @@ public function testExecuteWithoutProjectWillAskForProjectAndRunSearch() /** * @expectedException RuntimeException * @expectedExceptionMessage stop1 - * @depends testNotBlockedByLegacyInstallation */ public function testExecuteWithProjectWillRunSearchWithoutAskingForProject() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->never())->method('ask'); $helpers = new HelperSet(array( 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willThrowException(new RuntimeException('stop1')); $command = new Search($packager, $packagist, false); @@ -94,28 +79,27 @@ public function testExecuteWithProjectWillRunSearchWithoutAskingForProject() /** * @expectedException RuntimeException * @expectedExceptionMessage stop1 - * @depends testNotBlockedByLegacyInstallation */ public function testExecuteWithProjectAndSearchReturnsNoMatchesWillReportAndAskForOtherProject() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $output->expects($this->exactly(2))->method('writeln')->withConsecutive( array('Searching for foo...'), array('No matching packages found') ); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willThrowException(new RuntimeException('stop1')); $helpers = new HelperSet(array( 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array()); $command = new Search($packager, $packagist, false); @@ -126,26 +110,25 @@ public function testExecuteWithProjectAndSearchReturnsNoMatchesWillReportAndAskF /** * @expectedException RuntimeException * @expectedExceptionMessage stop1 - * @depends testNotBlockedByLegacyInstallation */ public function testExecuteWithProjectAndSearchReturnsOneMatchWillAskForProject() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willThrowException(new RuntimeException('stop1')); $helpers = new HelperSet(array( 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $command = new Search($packager, $packagist, false); @@ -156,19 +139,18 @@ public function testExecuteWithProjectAndSearchReturnsOneMatchWillAskForProject( /** * @expectedException RuntimeException * @expectedExceptionMessage stop1 - * @depends testNotBlockedByLegacyInstallation */ public function testExecuteWithProjectSelectedWillSearchVersions() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); $output->expects($this->exactly(2))->method('writeln')->withConsecutive( array('Searching for foo...'), array('Selected foo/bar, listing versions...') ); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->once())->method('ask')->willReturn( 'foo/bar (⤓)' ); @@ -177,12 +159,12 @@ public function testExecuteWithProjectSelectedWillSearchVersions() 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); $result->expects($this->exactly(2))->method('getName')->willReturn('foo/bar'); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $packagist->expects($this->once())->method('get')->with('foo/bar')->willThrowException(new RuntimeException('stop1')); @@ -191,20 +173,13 @@ public function testExecuteWithProjectSelectedWillSearchVersions() $command->run($input, $output); } - /** - * @depends testNotBlockedByLegacyInstallation - */ public function testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActionYieldsQuit() { - if (PHP_VERSION_ID >= 70100 && PHP_VERSION_ID < 70200) { - $this->markTestSkipped('Test broken with PHP 7.1 on legacy PHPUnit'); - } - - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->exactly(3))->method('ask')->willReturnOnConsecutiveCalls( 'foo/bar (⤓)', 'dev-master (no executable bin)', @@ -215,18 +190,18 @@ public function testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActi 'question' => $questionHelper )); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); $result->expects($this->exactly(2))->method('getName')->willReturn('foo/bar'); - $version = $this->getMock('Packagist\Api\Result\Package\Version'); + $version = $this->getMockBuilder('Packagist\Api\Result\Package\Version')->getMock(); $version->expects($this->exactly(2))->method('getVersion')->willReturn('dev-master'); - $package = $this->getMock('Packagist\Api\Result\Package'); + $package = $this->getMockBuilder('Packagist\Api\Result\Package')->getMock(); $package->expects($this->once())->method('getVersions')->willReturn(array($version)); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $packagist->expects($this->once())->method('get')->with('foo/bar')->willReturn($package); @@ -235,17 +210,13 @@ public function testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActi $command->run($input, $output); } - /** - * @depends testNotBlockedByLegacyInstallation - * @depends testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActionYieldsQuit - */ public function testExecuteWithProjectAndVersionSelectedWillBuildWhenAskedForActionYieldsBuild() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->exactly(3))->method('ask')->willReturnOnConsecutiveCalls( 'foo/bar (⤓)', 'dev-master (no executable bin)', @@ -259,19 +230,19 @@ public function testExecuteWithProjectAndVersionSelectedWillBuildWhenAskedForAct $pharer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $pharer->expects($this->once())->method('build'); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('getPharer')->with('foo/bar', 'dev-master')->willReturn($pharer); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); $result->expects($this->exactly(2))->method('getName')->willReturn('foo/bar'); - $version = $this->getMock('Packagist\Api\Result\Package\Version'); + $version = $this->getMockBuilder('Packagist\Api\Result\Package\Version')->getMock(); $version->expects($this->exactly(2))->method('getVersion')->willReturn('dev-master'); - $package = $this->getMock('Packagist\Api\Result\Package'); + $package = $this->getMockBuilder('Packagist\Api\Result\Package')->getMock(); $package->expects($this->once())->method('getVersions')->willReturn(array($version)); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $packagist->expects($this->once())->method('get')->with('foo/bar')->willReturn($package); @@ -280,17 +251,13 @@ public function testExecuteWithProjectAndVersionSelectedWillBuildWhenAskedForAct $command->run($input, $output); } - /** - * @depends testNotBlockedByLegacyInstallation - * @depends testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActionYieldsQuit - */ public function testExecuteWithProjectAndVersionSelectedWillInstallWhenAskedForActionYieldsInstall() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->exactly(3))->method('ask')->willReturnOnConsecutiveCalls( 'foo/bar (⤓)', 'dev-master (no executable bin)', @@ -306,21 +273,21 @@ public function testExecuteWithProjectAndVersionSelectedWillInstallWhenAskedForA $pharer->expects($this->once())->method('getPackageRoot')->willReturn($package); $pharer->expects($this->never())->method('build'); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); $packager->expects($this->once())->method('getPharer')->with('foo/bar', 'dev-master')->willReturn($pharer); $packager->expects($this->once())->method('getSystemBin')->with($package)->willReturn('targetPath'); $packager->expects($this->once())->method('install')->with($pharer, 'targetPath'); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); $result->expects($this->exactly(2))->method('getName')->willReturn('foo/bar'); - $version = $this->getMock('Packagist\Api\Result\Package\Version'); + $version = $this->getMockBuilder('Packagist\Api\Result\Package\Version')->getMock(); $version->expects($this->exactly(2))->method('getVersion')->willReturn('dev-master'); - $package = $this->getMock('Packagist\Api\Result\Package'); + $package = $this->getMockBuilder('Packagist\Api\Result\Package')->getMock(); $package->expects($this->once())->method('getVersions')->willReturn(array($version)); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $packagist->expects($this->once())->method('get')->with('foo/bar')->willReturn($package); @@ -329,17 +296,13 @@ public function testExecuteWithProjectAndVersionSelectedWillInstallWhenAskedForA $command->run($input, $output); } - /** - * @depends testNotBlockedByLegacyInstallation - * @depends testExecuteWithProjectAndVersionSelectedWillQuitWhenAskedForActionYieldsQuit - */ public function testExecuteWithProjectAndVersionSelectedOnWindowsWillNotOfferInstallWhenAskedForAction() { - $input = $this->getMock('Symfony\Component\Console\Input\InputInterface'); + $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); $input->expects($this->once())->method('getArgument')->with('project')->willReturn('foo'); - $output = $this->getMock('Symfony\Component\Console\Output\OutputInterface'); + $output = $this->getMockBuilder('Symfony\Component\Console\Output\OutputInterface')->getMock(); - $questionHelper = $this->getMock('Symfony\Component\Console\Helper\QuestionHelper'); + $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->exactly(3))->method('ask')->withConsecutive( $this->anything(), $this->anything(), @@ -362,18 +325,18 @@ public function testExecuteWithProjectAndVersionSelectedOnWindowsWillNotOfferIns $package = $this->getMockBuilder('Clue\PharComposer\Package\Package')->disableOriginalConstructor()->getMock(); - $packager = $this->getMock('Clue\PharComposer\Phar\Packager'); + $packager = $this->getMockBuilder('Clue\PharComposer\Phar\Packager')->getMock(); - $result = $this->getMock('Packagist\Api\Result\Result'); + $result = $this->getMockBuilder('Packagist\Api\Result\Result')->getMock(); $result->expects($this->exactly(2))->method('getName')->willReturn('foo/bar'); - $version = $this->getMock('Packagist\Api\Result\Package\Version'); + $version = $this->getMockBuilder('Packagist\Api\Result\Package\Version')->getMock(); $version->expects($this->exactly(2))->method('getVersion')->willReturn('dev-master'); - $package = $this->getMock('Packagist\Api\Result\Package'); + $package = $this->getMockBuilder('Packagist\Api\Result\Package')->getMock(); $package->expects($this->once())->method('getVersions')->willReturn(array($version)); - $packagist = $this->getMock('Packagist\Api\Client'); + $packagist = $this->getMockBuilder('Packagist\Api\Client')->getMock(); $packagist->expects($this->once())->method('search')->with('foo')->willReturn(array($result)); $packagist->expects($this->once())->method('get')->with('foo/bar')->willReturn($package); diff --git a/tests/Phar/TargetPharTest.php b/tests/Phar/TargetPharTest.php index a5df41a..dcc159d 100644 --- a/tests/Phar/TargetPharTest.php +++ b/tests/Phar/TargetPharTest.php @@ -26,18 +26,11 @@ public function setUp() $this->markTestSkipped('Unable to mock \Phar on PHP 5.4/5.5'); } - $this->mockPhar = $this->createMock('\Phar'); - $this->mockPharComposer = $this->createMock('Clue\PharComposer\Phar\PharComposer'); + $this->mockPhar = $this->getMockBuilder('\Phar')->disableOriginalConstructor()->getMock(); + $this->mockPharComposer = $this->getMockBuilder('Clue\PharComposer\Phar\PharComposer')->disableOriginalConstructor()->getMock(); $this->targetPhar = new TargetPhar($this->mockPhar, $this->mockPharComposer); } - private function createMock($class) - { - return $this->getMockBuilder($class) - ->disableOriginalConstructor() - ->getMock(); - } - /** * @test */ @@ -59,7 +52,7 @@ public function addFileCalculatesLocalPartForBox() public function buildFromIteratorProvidesBasePathForBox() { $mockPackage = new Package(array(), 'path/to/package'); - $mockTraversable = $this->getMock('\Iterator'); + $mockTraversable = $this->getMockBuilder('\Iterator')->getMock(); $this->mockPharComposer->expects($this->once()) ->method('getPackageRoot') ->willReturn($mockPackage); @@ -83,7 +76,7 @@ public function addPackageAddsResourcesFromCalculatedBundle() $this->mockPhar->expects($this->once()) ->method('addFile') ->with($this->equalTo('path/to/package/file.php'), $this->equalTo('file.php')); - $mockFinder = $this->createMock('Symfony\Component\Finder\Finder'); + $mockFinder = $this->getMockBuilder('Symfony\Component\Finder\Finder')->disableOriginalConstructor()->getMock(); $bundle->addDir($mockFinder); $mockPackage = new Package(array(), 'path/to/package'); $this->mockPharComposer->expects($this->once()) From 87413463774ef1839698c1492a9e826c2a300f27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Tue, 28 Dec 2021 17:28:01 +0100 Subject: [PATCH 2/3] Update test suite to support PHPUnit 7 --- composer.json | 2 +- tests/Command/SearchTest.php | 4 ++-- tests/bootstrap.php | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index df46f62..79d4c99 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/process": "^5.0 || ^4.0 || ^3.0 || ^2.5" }, "require-dev": { - "phpunit/phpunit": "^5.7 || ^4.8" + "phpunit/phpunit": "^7.5 || ^5.7 || ^4.8.36" }, "autoload": { "psr-0": {"Clue": "src/"} diff --git a/tests/Command/SearchTest.php b/tests/Command/SearchTest.php index 90fcd55..d473001 100644 --- a/tests/Command/SearchTest.php +++ b/tests/Command/SearchTest.php @@ -304,8 +304,8 @@ public function testExecuteWithProjectAndVersionSelectedOnWindowsWillNotOfferIns $questionHelper = $this->getMockBuilder('Symfony\Component\Console\Helper\QuestionHelper')->getMock(); $questionHelper->expects($this->exactly(3))->method('ask')->withConsecutive( - $this->anything(), - $this->anything(), + array($this->anything()), + array($this->anything()), array( $input, $output, diff --git a/tests/bootstrap.php b/tests/bootstrap.php index c41ce49..7f05fbe 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,8 +1,10 @@ Date: Tue, 28 Dec 2021 13:37:57 +0100 Subject: [PATCH 3/3] Update test suite to support PHPUnit 9 --- .github/workflows/ci.yml | 3 +++ composer.json | 2 +- phpunit.xml.dist | 19 ++++++++++++------- phpunit.xml.legacy | 18 ++++++++++++++++++ tests/Command/SearchTest.php | 30 ++++++++++-------------------- tests/LoggerTest.php | 4 +++- tests/Phar/PackagerTest.php | 18 +++++------------- tests/Phar/PharComposerTest.php | 22 ++++++---------------- tests/Phar/TargetPharTest.php | 4 +++- tests/bootstrap.php | 17 ++++++++++++++++- 10 files changed, 77 insertions(+), 60 deletions(-) create mode 100644 phpunit.xml.legacy diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8204f57..8336a0d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,9 @@ jobs: php-version: ${{ matrix.php }} - run: composer install - run: vendor/bin/phpunit --coverage-text + if: ${{ matrix.php >= 7.3 }} + - run: vendor/bin/phpunit --coverage-text -c phpunit.xml.legacy + if: ${{ matrix.php < 7.3 }} - run: composer build PHPUnit-hhvm: diff --git a/composer.json b/composer.json index 79d4c99..6b5d477 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "symfony/process": "^5.0 || ^4.0 || ^3.0 || ^2.5" }, "require-dev": { - "phpunit/phpunit": "^7.5 || ^5.7 || ^4.8.36" + "phpunit/phpunit": "^9.5 || ^5.7 || ^4.8.36" }, "autoload": { "psr-0": {"Clue": "src/"} diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 688f812..3bea625 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,14 +1,19 @@ - + + ./tests/ - - - ./src - - - \ No newline at end of file + + + ./src/ + + + diff --git a/phpunit.xml.legacy b/phpunit.xml.legacy new file mode 100644 index 0000000..50d3927 --- /dev/null +++ b/phpunit.xml.legacy @@ -0,0 +1,18 @@ + + + + + + + ./tests/ + + + + + ./src/ + + + diff --git a/tests/Command/SearchTest.php b/tests/Command/SearchTest.php index d473001..22543b5 100644 --- a/tests/Command/SearchTest.php +++ b/tests/Command/SearchTest.php @@ -22,10 +22,6 @@ public function testCtorCreatesPackagerAndPackagist() $this->assertInstanceOf('Packagist\Api\Client', $packagist); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage stop1 - */ public function testExecuteWithoutProjectWillAskForProjectAndRunSearch() { $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); @@ -46,13 +42,11 @@ public function testExecuteWithoutProjectWillAskForProjectAndRunSearch() $command = new Search($packager, $packagist, false); $command->setHelperSet($helpers); + + $this->setExpectedException('RuntimeException', 'stop1'); $command->run($input, $output); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage stop1 - */ public function testExecuteWithProjectWillRunSearchWithoutAskingForProject() { $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); @@ -73,13 +67,11 @@ public function testExecuteWithProjectWillRunSearchWithoutAskingForProject() $command = new Search($packager, $packagist, false); $command->setHelperSet($helpers); + + $this->setExpectedException('RuntimeException', 'stop1'); $command->run($input, $output); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage stop1 - */ public function testExecuteWithProjectAndSearchReturnsNoMatchesWillReportAndAskForOtherProject() { $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); @@ -104,13 +96,11 @@ public function testExecuteWithProjectAndSearchReturnsNoMatchesWillReportAndAskF $command = new Search($packager, $packagist, false); $command->setHelperSet($helpers); + + $this->setExpectedException('RuntimeException', 'stop1'); $command->run($input, $output); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage stop1 - */ public function testExecuteWithProjectAndSearchReturnsOneMatchWillAskForProject() { $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); @@ -133,13 +123,11 @@ public function testExecuteWithProjectAndSearchReturnsOneMatchWillAskForProject( $command = new Search($packager, $packagist, false); $command->setHelperSet($helpers); + + $this->setExpectedException('RuntimeException', 'stop1'); $command->run($input, $output); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage stop1 - */ public function testExecuteWithProjectSelectedWillSearchVersions() { $input = $this->getMockBuilder('Symfony\Component\Console\Input\InputInterface')->getMock(); @@ -170,6 +158,8 @@ public function testExecuteWithProjectSelectedWillSearchVersions() $command = new Search($packager, $packagist, false); $command->setHelperSet($helpers); + + $this->setExpectedException('RuntimeException', 'stop1'); $command->run($input, $output); } diff --git a/tests/LoggerTest.php b/tests/LoggerTest.php index 76e0b23..512e940 100644 --- a/tests/LoggerTest.php +++ b/tests/LoggerTest.php @@ -13,8 +13,10 @@ class LoggerTest extends TestCase /** * set up test environment + * + * @before */ - public function setUp() + public function setUpLogger() { $this->logger = new Logger(); } diff --git a/tests/Phar/PackagerTest.php b/tests/Phar/PackagerTest.php index 54e484c..8e2b4dc 100644 --- a/tests/Phar/PackagerTest.php +++ b/tests/Phar/PackagerTest.php @@ -7,7 +7,8 @@ class PackagerTest extends TestCase { private $packager; - public function setUp() + /** @before */ + public function setUpPackager() { $this->packager = new Packager(); } @@ -48,30 +49,21 @@ public function provideExecCommands() ); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage not installed - */ public function testEmptyNotInstalled() { + $this->setExpectedException('RuntimeException', 'not installed'); $this->packager->getPharer(__DIR__ . '/../fixtures/01-empty'); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage not a readable file - */ public function testNoComposer() { + $this->setExpectedException('InvalidArgumentException', 'not a readable file'); $this->packager->getPharer(__DIR__ . '/../fixtures/02-no-composer'); } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage not a readable file - */ public function testNoComposerMissing() { + $this->setExpectedException('InvalidArgumentException', 'not a readable file'); $this->packager->getPharer(__DIR__ . '/../fixtures/02-no-composer/composer.json'); } diff --git a/tests/Phar/PharComposerTest.php b/tests/Phar/PharComposerTest.php index 4e97544..2045f10 100644 --- a/tests/Phar/PharComposerTest.php +++ b/tests/Phar/PharComposerTest.php @@ -19,12 +19,9 @@ public function testConstructor() return $pharcomposer; } - /** - * @expectedException InvalidArgumentException - * @expectedExceptionMessage Unable to parse given path - */ public function testConstructorThrowsWhenPathIsNotFile() { + $this->setExpectedException('InvalidArgumentException', 'Unable to parse given path'); new PharComposer(__DIR__); } @@ -43,14 +40,11 @@ public function testSetters(PharComposer $pharcomposer) return $pharcomposer; } - /** - * @expectedException UnexpectedValueException - * @expectedExceptionMessage Bin file "bin/invalid" does not exist - */ public function testGetMainThrowsWhenBinDoesNotExist() { $pharer = new PharComposer(__DIR__ . '/../fixtures/05-invalid-bin/composer.json'); + $this->setExpectedException('UnexpectedValueException', 'Bin file "bin/invalid" does not exist'); $pharer->getMain(); } @@ -62,22 +56,16 @@ public function testSetTargetWillAppendPackageShortNameWhenTargetIsDirectory() $this->assertEquals(__DIR__ . '/phar-composer.phar', $pharer->getTarget()); } - /** - * @expectedException RuntimeException - * @expectedExceptionMessage not properly installed - */ public function testBuildThrowsWhen() { $pharer = new PharComposer(__DIR__ . '/../fixtures/01-empty/composer.json'); $pharer->setOutput(false); $pharer->setTarget('/dev/null'); + + $this->setExpectedException('RuntimeException', 'not properly installed'); $pharer->build(); } - /** - * @expectedException RuntimeException - * @expectedException Unable to write phar: - */ public function testBuildThrowsWhenTargetCanNotBeWritten() { if (!Phar::canWrite() || !file_exists('/dev/null')) { @@ -87,6 +75,8 @@ public function testBuildThrowsWhenTargetCanNotBeWritten() $pharer = new PharComposer(__DIR__ . '/../fixtures/03-project-with-phars/composer.json'); $pharer->setOutput(false); $pharer->setTarget('/dev/null'); + + $this->setExpectedException('RuntimeException', 'Unable to write phar:'); $pharer->build(); } diff --git a/tests/Phar/TargetPharTest.php b/tests/Phar/TargetPharTest.php index dcc159d..69fce43 100644 --- a/tests/Phar/TargetPharTest.php +++ b/tests/Phar/TargetPharTest.php @@ -19,8 +19,10 @@ class TargetPharTest extends TestCase /** * set up test environment + * + * @before */ - public function setUp() + public function setUpPhar() { if (PHP_VERSION_ID >= 50400 && PHP_VERSION_ID <= 50600) { $this->markTestSkipped('Unable to mock \Phar on PHP 5.4/5.5'); diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 7f05fbe..41a0c98 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -6,5 +6,20 @@ class TestCase extends BaseTestCase { - + public function setExpectedException($exception, $exceptionMessage = '', $exceptionCode = null) + { + if (method_exists($this, 'expectException')) { + // PHPUnit 5+ + $this->expectException($exception); + if ($exceptionMessage !== '') { + $this->expectExceptionMessage($exceptionMessage); + } + if ($exceptionCode !== null) { + $this->expectExceptionCode($exceptionCode); + } + } else { + // legacy PHPUnit 4 + parent::setExpectedException($exception, $exceptionMessage, $exceptionCode); + } + } }