Skip to content

Commit

Permalink
fix: The at() matcher has been deprecated. It will be removed in PHPU…
Browse files Browse the repository at this point in the history
…nit 10. Please refactor your test to not rely on the order in which methods are invoked.
  • Loading branch information
Peter Gribanov committed Nov 18, 2024
1 parent f64e6c8 commit 750f2ec
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 86 deletions.
43 changes: 21 additions & 22 deletions tests/Command/DownloadDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,14 @@ public function testInvalidURLArgument(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('URL of downloaded GeoIP2 database should be a string, got ["https:\/\/example.com\/GeoIP2.tar.gz"] instead.');

$url = ['https://example.com/GeoIP2.tar.gz'];
$target = '/tmp/GeoIP2.mmdb';

$this->input
->expects($this->at(4))
->expects($this->exactly(2))
->method('getArgument')
->with('url')
->willReturn(['https://example.com/GeoIP2.tar.gz']);
->withConsecutive(['url'], ['target'])
->willReturnOnConsecutiveCalls($url, $target);

$this->command->run($this->input, $this->output);
}
Expand All @@ -92,11 +95,14 @@ public function testNoTargetArgument(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Target download path should be a string, got null instead.');

$url = 'https://example.com/GeoIP2.tar.gz';
$target = null;

$this->input
->expects($this->at(4))
->expects($this->exactly(2))
->method('getArgument')
->with('url')
->willReturn('https://example.com/GeoIP2.tar.gz');
->withConsecutive(['url'], ['target'])
->willReturnOnConsecutiveCalls($url, $target);

$this->command->run($this->input, $this->output);
}
Expand All @@ -106,16 +112,14 @@ public function testInvalidTargetArgument(): void
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Target download path should be a string, got ["\/tmp\/GeoIP2.mmdb"] instead.');

$url = 'https://example.com/GeoIP2.tar.gz';
$target = ['/tmp/GeoIP2.mmdb'];

$this->input
->expects($this->at(4))
->method('getArgument')
->with('url')
->willReturn('https://example.com/GeoIP2.tar.gz');
$this->input
->expects($this->at(5))
->expects($this->exactly(2))
->method('getArgument')
->with('target')
->willReturn(['/tmp/GeoIP2.mmdb']);
->withConsecutive(['url'], ['target'])
->willReturnOnConsecutiveCalls($url, $target);

$this->command->run($this->input, $this->output);
}
Expand All @@ -126,15 +130,10 @@ public function testDownload(): void
$target = '/tmp/GeoIP2.mmdb';

$this->input
->expects($this->at(4))
->method('getArgument')
->with('url')
->willReturn($url);
$this->input
->expects($this->at(5))
->expects($this->exactly(2))
->method('getArgument')
->with('target')
->willReturn($target);
->withConsecutive(['url'], ['target'])
->willReturnOnConsecutiveCalls($url, $target);

$this->downloader
->expects($this->once())
Expand Down
29 changes: 14 additions & 15 deletions tests/Command/UpdateDatabaseCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testInvalidDatabasesArgument(): void
$this->expectExceptionMessage('Updated databases should be a array, got "" instead.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn('');
Expand All @@ -89,7 +89,7 @@ public function testUndefinedDatabase(): void
$this->expectExceptionMessage('Undefined "default" database.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -104,7 +104,7 @@ public function testUndefinedDatabase2(): void
$this->expectExceptionMessage('Undefined "foo" database.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['foo']);
Expand All @@ -121,7 +121,7 @@ public function testNoDatabaseURL(): void
$this->expectExceptionMessage('Invalid "default" database config.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -138,7 +138,7 @@ public function testNoDatabasePath(): void
$this->expectExceptionMessage('Invalid "default" database config.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -157,7 +157,7 @@ public function testInvalidDatabaseURL(): void
$this->expectExceptionMessage('Invalid "default" database config.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -176,7 +176,7 @@ public function testInvalidDatabasePath(): void
$this->expectExceptionMessage('Invalid "default" database config.');

$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -193,7 +193,7 @@ public function testInvalidDatabasePath(): void
public function testDownloadOneDatabases(): void
{
$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['default']);
Expand All @@ -215,7 +215,7 @@ public function testDownloadOneDatabases(): void
public function testDownloadSeveralDatabases(): void
{
$this->input
->expects($this->at(4))
->expects($this->once())
->method('getArgument')
->with('databases')
->willReturn(['second', 'first']);
Expand All @@ -232,13 +232,12 @@ public function testDownloadSeveralDatabases(): void
];

$this->downloader
->expects($this->at(0))
->method('download')
->with($databases['second']['url'], $databases['second']['path']);
$this->downloader
->expects($this->at(1))
->expects($this->exactly(2))
->method('download')
->with($databases['first']['url'], $databases['first']['path']);
->withConsecutive(
[$databases['second']['url'], $databases['second']['path']],
[$databases['first']['url'], $databases['first']['path']]
);

$command = new UpdateDatabaseCommand($this->downloader, $databases);
$command->run($this->input, $this->output);
Expand Down
77 changes: 28 additions & 49 deletions tests/Downloader/MaxMindDownloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ public function testNotFoundDatabase(): void
->expects($this->atLeastOnce())
->method('debug');

$fs_call = 0;
$this->fs
->expects($this->at($fs_call++))
->expects($this->once())
->method('remove')
->willReturnCallback(function ($files) use ($tmp_zip_regexp, $tmp_unzip_regexp, $tmp_untar_regexp) {
$this->assertIsArray($files);
Expand All @@ -87,7 +86,7 @@ public function testNotFoundDatabase(): void
$this->assertMatchesRegularExpression($tmp_untar_regexp, $files[2]);
});
$this->fs
->expects($this->at($fs_call++))
->expects($this->once())
->method('copy')
->willReturnCallback(function ($origin_file, $target_file, $overwrite_newer_files) use (
$url,
Expand All @@ -102,7 +101,7 @@ public function testNotFoundDatabase(): void
file_put_contents($target_file, base64_decode(self::TAR_GZ_BAD));
});
$this->fs
->expects($this->at($fs_call))
->expects($this->once())
->method('mkdir')
->with(dirname($target), 0755);

Expand All @@ -124,9 +123,8 @@ public function testDownload(): void
->expects($this->atLeastOnce())
->method('debug');

$fs_call = 0;
$this->fs
->expects($this->at($fs_call++))
->expects($this->exactly(2))
->method('remove')
->willReturnCallback(function ($files) use ($tmp_zip_regexp, $tmp_unzip_regexp, $tmp_untar_regexp) {
$this->assertIsArray($files);
Expand All @@ -142,63 +140,44 @@ public function testDownload(): void
$this->assertMatchesRegularExpression($tmp_untar_regexp, $files[2]);
});
$this->fs
->expects($this->at($fs_call++))
->expects($this->exactly(2))
->method('copy')
->willReturnCallback(function ($origin_file, $target_file, $overwrite_newer_files) use (
$url,
$tmp_zip_regexp
$tmp_zip_regexp,
$target,
$path_quote
) {
$this->assertSame($url, $origin_file);
$this->assertIsString($origin_file);
$this->assertIsString($target_file);
$this->assertTrue($overwrite_newer_files);
$this->assertMatchesRegularExpression($tmp_zip_regexp, $target_file);

// make test GeoLite2 db
file_put_contents($target_file, base64_decode(self::TAR_GZ));
if ($target === $target_file) {
$this->assertSame($target, $target_file);
$regexp = sprintf(
'#^%s/[\da-f]+\.\d+_GeoLite2/GeoLite2-City_20200114/GeoLite2.mmdb$#',
$path_quote
);
$this->assertMatchesRegularExpression($regexp, $origin_file);
$this->assertFileExists($origin_file);
$this->assertSame('TestGeoLite2', file_get_contents($origin_file));
} else {

$this->assertSame($url, $origin_file);
$this->assertMatchesRegularExpression($tmp_zip_regexp, $target_file);

// make test GeoLite2 db
file_put_contents($target_file, base64_decode(self::TAR_GZ));
}
});
$this->fs
->expects($this->at($fs_call++))
->expects($this->once())
->method('mkdir')
->with(dirname($target), 0755);
$this->fs
->expects($this->at($fs_call++))
->method('copy')
->willReturnCallback(function (
$origin_file,
$target_file,
$overwrite_newer_files
) use ($target, $path_quote) {
$this->assertIsString($origin_file);
$this->assertSame($target, $target_file);
$this->assertTrue($overwrite_newer_files);
$regexp = sprintf(
'#^%s/[\da-f]+\.\d+_GeoLite2/GeoLite2-City_20200114/GeoLite2.mmdb$#',
$path_quote
);
$this->assertMatchesRegularExpression($regexp, $origin_file);
$this->assertFileExists($origin_file);
$this->assertSame('TestGeoLite2', file_get_contents($origin_file));
});
$this->fs
->expects($this->at($fs_call++))
->expects($this->once())
->method('chmod')
->with($target, 0755);
$this->fs
->expects($this->at($fs_call))
->method('remove')
->willReturnCallback(function ($files) use ($tmp_zip_regexp, $tmp_unzip_regexp, $tmp_untar_regexp) {
$this->assertIsArray($files);
$this->assertCount(3, $files);
$this->assertArrayHasKey(0, $files);
$this->assertArrayHasKey(1, $files);
$this->assertArrayHasKey(2, $files);
$this->assertIsString($files[0]);
$this->assertIsString($files[1]);
$this->assertIsString($files[2]);
$this->assertMatchesRegularExpression($tmp_zip_regexp, $files[0]);
$this->assertMatchesRegularExpression($tmp_unzip_regexp, $files[1]);
$this->assertMatchesRegularExpression($tmp_untar_regexp, $files[2]);
});

$this->downloader->download($url, $target);
}
Expand Down

0 comments on commit 750f2ec

Please sign in to comment.