Skip to content

Commit

Permalink
Get rid of all the ->at() mock invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Dec 9, 2021
1 parent b73a393 commit 9b99079
Show file tree
Hide file tree
Showing 56 changed files with 742 additions and 911 deletions.
8 changes: 2 additions & 6 deletions composer.json
Expand Up @@ -21,9 +21,6 @@
"homepage": "https://seld.be"
}
],
"_comment": [
"symfony/polyfill-php73 sould be removed once https://github.com/symfony/symfony/pull/44523 is released and the symfony/console requirement bumped to match that release"
],
"require": {
"php": "^7.2.5 || ^8.0",
"composer/ca-bundle": "^1.0",
Expand All @@ -35,13 +32,12 @@
"psr/log": "^1.0 || ^2.0",
"seld/jsonlint": "^1.4",
"seld/phar-utils": "^1.0",
"symfony/console": "^5.4 || ^6.0",
"symfony/console": "^5.4.1 || ^6.0",
"symfony/filesystem": "^5.4 || ^6.0",
"symfony/finder": "^5.4 || ^6.0",
"symfony/process": "^5.4 || ^6.0",
"react/promise": "^2.8",
"composer/pcre": "^1.0",
"symfony/polyfill-php73": "^1.9"
"composer/pcre": "^1.0"
},
"require-dev": {
"symfony/phpunit-bridge": "^6.0",
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion phpstan/locate-phpunit-autoloader.php
@@ -1,7 +1,7 @@
<?php

$bestDirFound = null;
$dirs = glob(__DIR__.'/../vendor/bin/.phpunit/phpunit-*');
$dirs = glob(__DIR__.'/../vendor/bin/.phpunit/phpunit-*', GLOB_ONLYDIR);
natsort($dirs);

foreach (array_reverse($dirs) as $dir) {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Expand Up @@ -16,7 +16,6 @@
>
<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=0"/>
<env name="SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT" value="1"/>
<env name="COMPOSER_TEST_SUITE" value="1"/>
</php>
<testsuites>
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/Util/Http/Response.php
Expand Up @@ -25,15 +25,15 @@ class Response
private $request;
/** @var int */
private $code;
/** @var string[] */
/** @var list<string> */
private $headers;
/** @var ?string */
private $body;

/**
* @param Request $request
* @param int $code
* @param string[] $headers
* @param list<string> $headers
* @param ?string $body
*/
public function __construct(array $request, $code, array $headers, $body)
Expand Down
2 changes: 1 addition & 1 deletion src/Composer/Util/Svn.php
Expand Up @@ -165,7 +165,7 @@ private function executeWithAuthRetry($svnCommand, $cwd, $url, $path, $verbose)
}

$errorOutput = $this->process->getErrorOutput();
$fullOutput = implode("\n", array($output, $errorOutput));
$fullOutput = trim(implode("\n", array($output, $errorOutput)));

// the error is not auth-related
if (false === stripos($fullOutput, 'Could not authenticate to server:')
Expand Down
3 changes: 2 additions & 1 deletion tests/Composer/Test/AllFunctionalTest.php
Expand Up @@ -38,8 +38,9 @@ public function setUp(): void
chdir(__DIR__.'/Fixtures/functional');
}

public function tearDown(): void
protected function tearDown(): void
{
parent::tearDown();
if ($this->oldcwd) {
chdir($this->oldcwd);
}
Expand Down
80 changes: 37 additions & 43 deletions tests/Composer/Test/ApplicationTest.php
Expand Up @@ -14,11 +14,12 @@

use Composer\Console\Application;
use Composer\XdebugHandler\XdebugHandler;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\OutputInterface;

class ApplicationTest extends TestCase
{
public function tearDown(): void
protected function tearDown(): void
{
parent::tearDown();

Expand All @@ -34,22 +35,24 @@ public function testDevWarning()

putenv('COMPOSER_NO_INTERACTION=1');

$index = 0;
$inputMock->expects($this->at($index++))
$inputMock->expects($this->any())
->method('hasParameterOption')
->with($this->equalTo('--no-plugins'))
->will($this->returnValue(true));

$inputMock->expects($this->at($index++))
->willReturnCallback(function ($opt): bool {
switch ($opt) {
case '--no-plugins':
return true;
case '--no-cache':
return false;
}

return false;
});

$inputMock->expects($this->once())
->method('setInteractive')
->with($this->equalTo(false));

$inputMock->expects($this->at($index++))
->method('hasParameterOption')
->with($this->equalTo('--no-cache'))
->will($this->returnValue(false));

$inputMock->expects($this->at($index++))
$inputMock->expects($this->once())
->method('getParameterOption')
->with($this->equalTo(array('--working-dir', '-d')))
->will($this->returnValue(false));
Expand All @@ -58,33 +61,22 @@ public function testDevWarning()
->method('getFirstArgument')
->will($this->returnValue('show'));

$index = 0;
$outputMock->expects($this->at($index++))
->method("write");
$output = new BufferedOutput();
$expectedOutput = '';

if (XdebugHandler::isXdebugActive()) {
$outputMock->expects($this->at($index++))
->method("getVerbosity")
->willReturn(OutputInterface::VERBOSITY_NORMAL);

$outputMock->expects($this->at($index++))
->method("write")
->with($this->equalTo('<warning>Composer is operating slower than normal because you have Xdebug enabled. See https://getcomposer.org/xdebug</warning>'));
$expectedOutput .= '<warning>Composer is operating slower than normal because you have Xdebug enabled. See https://getcomposer.org/xdebug</warning>'.PHP_EOL;
}

$outputMock->expects($this->at($index++))
->method("getVerbosity")
->willReturn(OutputInterface::VERBOSITY_NORMAL);

$outputMock->expects($this->at($index++))
->method("write")
->with($this->equalTo(sprintf('<warning>Warning: This development build of Composer is over 60 days old. It is recommended to update it by running "%s self-update" to get the latest version.</warning>', $_SERVER['PHP_SELF'])));
$expectedOutput .= sprintf('<warning>Warning: This development build of Composer is over 60 days old. It is recommended to update it by running "%s self-update" to get the latest version.</warning>', $_SERVER['PHP_SELF']).PHP_EOL;

if (!defined('COMPOSER_DEV_WARNING_TIME')) {
define('COMPOSER_DEV_WARNING_TIME', time() - 1);
}

$application->doRun($inputMock, $outputMock);
$application->doRun($inputMock, $output);

$this->assertStringContainsString($expectedOutput, $output->fetch());
}

/**
Expand All @@ -102,22 +94,24 @@ public function ensureNoDevWarning($command)

putenv('COMPOSER_NO_INTERACTION=1');

$index = 0;
$inputMock->expects($this->at($index++))
$inputMock->expects($this->any())
->method('hasParameterOption')
->with($this->equalTo('--no-plugins'))
->will($this->returnValue(true));

$inputMock->expects($this->at($index++))
->willReturnCallback(function ($opt): bool {
switch ($opt) {
case '--no-plugins':
return true;
case '--no-cache':
return false;
}

return false;
});

$inputMock->expects($this->once())
->method('setInteractive')
->with($this->equalTo(false));

$inputMock->expects($this->at($index++))
->method('hasParameterOption')
->with($this->equalTo('--no-cache'))
->will($this->returnValue(false));

$inputMock->expects($this->at($index++))
$inputMock->expects($this->once())
->method('getParameterOption')
->with($this->equalTo(array('--working-dir', '-d')))
->will($this->returnValue(false));
Expand Down
29 changes: 12 additions & 17 deletions tests/Composer/Test/Autoload/AutoloadGeneratorTest.php
Expand Up @@ -150,6 +150,7 @@ protected function setUp(): void

protected function tearDown(): void
{
parent::tearDown();
chdir($this->origDir);

if (is_dir($this->workingDir)) {
Expand Down Expand Up @@ -957,17 +958,13 @@ public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles(
$notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0');
$notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0');

$this->repository->expects($this->at(1))
$this->repository->expects($this->exactly(3))
->method('getCanonicalPackages')
->will($this->returnValue($autoloadPackages));

$this->repository->expects($this->at(3))
->method('getCanonicalPackages')
->will($this->returnValue($notAutoloadPackages));

$this->repository->expects($this->at(5))
->method('getCanonicalPackages')
->will($this->returnValue($notAutoloadPackages));
->willReturnOnConsecutiveCalls(
$autoloadPackages,
$notAutoloadPackages,
$notAutoloadPackages
);

$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
Expand Down Expand Up @@ -1283,14 +1280,12 @@ public function testIncludePathFileWithoutPathsIsSkipped()
public function testPreAndPostEventsAreDispatchedDuringAutoloadDump()
{
$this->eventDispatcher
->expects($this->at(0))
->method('dispatchScript')
->with(ScriptEvents::PRE_AUTOLOAD_DUMP, false);

$this->eventDispatcher
->expects($this->at(1))
->expects($this->exactly(2))
->method('dispatchScript')
->with(ScriptEvents::POST_AUTOLOAD_DUMP, false);
->withConsecutive(
[ScriptEvents::PRE_AUTOLOAD_DUMP, false],
[ScriptEvents::POST_AUTOLOAD_DUMP, false]
);

$package = new RootPackage('root/a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Prefix' => 'foo/bar/non/existing/')));
Expand Down
1 change: 1 addition & 0 deletions tests/Composer/Test/CacheTest.php
Expand Up @@ -55,6 +55,7 @@ public function setUp(): void

protected function tearDown(): void
{
parent::tearDown();
if (is_dir($this->root)) {
$fs = new Filesystem;
$fs->removeDirectory($this->root);
Expand Down
1 change: 1 addition & 0 deletions tests/Composer/Test/Config/JsonConfigSourceTest.php
Expand Up @@ -42,6 +42,7 @@ protected function setUp(): void

protected function tearDown(): void
{
parent::tearDown();
if (is_dir($this->workingDir)) {
$this->fs->removeDirectory($this->workingDir);
}
Expand Down
23 changes: 10 additions & 13 deletions tests/Composer/Test/Downloader/DownloadManagerTest.php
Expand Up @@ -254,13 +254,12 @@ public function testFullPackageDownloadFailover()
->will($this->returnValue('prettyPackage'));

$package
->expects($this->at(3))
->method('setInstallationSource')
->with('dist');
$package
->expects($this->at(5))
->expects($this->exactly(2))
->method('setInstallationSource')
->with('source');
->withConsecutive(
['dist'],
['source']
);

$downloaderFail = $this->createDownloaderMock();
$downloaderFail
Expand All @@ -280,15 +279,13 @@ public function testFullPackageDownloadFailover()
->setMethods(array('getDownloaderForPackage'))
->getMock();
$manager
->expects($this->at(0))
->method('getDownloaderForPackage')
->with($package)
->will($this->returnValue($downloaderFail));
$manager
->expects($this->at(1))
->expects($this->exactly(2))
->method('getDownloaderForPackage')
->with($package)
->will($this->returnValue($downloaderSuccess));
->willReturnOnConsecutiveCalls(
$downloaderFail,
$downloaderSuccess
);

$manager->download($package, 'target_dir');
}
Expand Down

0 comments on commit 9b99079

Please sign in to comment.