Skip to content

Commit c851cb0

Browse files
committed
deduplicate testing copying file using data provider
1 parent ae4b8c8 commit c851cb0

1 file changed

Lines changed: 90 additions & 68 deletions

File tree

tests/CopyFilesTaskIntegrationTest.php

Lines changed: 90 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace VasekPurchart\Phing\CopyFiles;
66

7+
use Generator;
78
use PHPUnit\Framework\Assert;
89
use Project;
910
use VasekPurchart\Phing\PhingTester\PhingTester;
@@ -13,85 +14,106 @@ class CopyFilesTaskIntegrationTest extends \PHPUnit\Framework\TestCase
1314

1415
private const TEMP_DIRECTORY_PATH = __DIR__ . '/temp';
1516

16-
public function testCopyFile(): void
17+
/**
18+
* @return mixed[][]|\Generator
19+
*/
20+
public function copyFileDataProvider(): Generator
1721
{
18-
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/foo';
19-
file_put_contents($sourceFilePath, 'FOO');
20-
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/foo-copy';
21-
if (file_exists($targetFilePath)) {
22-
unlink($targetFilePath);
23-
}
24-
25-
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
26-
$target = __FUNCTION__;
27-
$tester->executeTarget($target);
28-
29-
Assert::assertFileEquals($sourceFilePath, $targetFilePath);
30-
$tester->assertLogMessageRegExp('~Copying.+/foo.+->.+/foo-copy~', $target, Project::MSG_INFO);
22+
yield 'copy file' => [
23+
'target' => 'testCopyFile',
24+
'sourceFileName' => '/foo',
25+
'targetFileName' => '/foo-copy',
26+
'existingSourceFileContents' => 'FOO',
27+
'existingTargetFileContents' => null,
28+
'expectedTargetFileContents' => 'FOO',
29+
'logMessageRegExp' => '~Copying.+/foo.+->.+/foo-copy~',
30+
'logMessagePriority' => Project::MSG_INFO,
31+
];
32+
33+
yield 'copy file with absolute path' => [
34+
'target' => 'testCopyFileWithAbsolutePath',
35+
'sourceFileName' => '/foo',
36+
'targetFileName' => '/foo-copy',
37+
'existingSourceFileContents' => 'FOO',
38+
'existingTargetFileContents' => null,
39+
'expectedTargetFileContents' => 'FOO',
40+
'logMessageRegExp' => '~Copying.+/foo.+->.+/foo-copy~',
41+
'logMessagePriority' => Project::MSG_INFO,
42+
];
43+
44+
yield 'target file exists' => [
45+
'target' => 'testTargetFileExists',
46+
'sourceFileName' => '/new',
47+
'targetFileName' => '/existing',
48+
'existingSourceFileContents' => 'NEW',
49+
'existingTargetFileContents' => 'EXISTING',
50+
'expectedTargetFileContents' => 'EXISTING',
51+
'logMessageRegExp' => '~/existing.+already exists.+SKIPPING~',
52+
'logMessagePriority' => Project::MSG_INFO,
53+
];
54+
55+
yield 'target file exists skip' => [
56+
'target' => 'testTargetFileExistsSkip',
57+
'sourceFileName' => '/new',
58+
'targetFileName' => '/existing',
59+
'existingSourceFileContents' => 'NEW',
60+
'existingTargetFileContents' => 'EXISTING',
61+
'expectedTargetFileContents' => 'EXISTING',
62+
'logMessageRegExp' => '~/existing.+already exists.+SKIPPING~',
63+
'logMessagePriority' => Project::MSG_INFO,
64+
];
65+
66+
yield 'target file exists replace' => [
67+
'target' => 'testTargetFileExistsReplace',
68+
'sourceFileName' => '/new',
69+
'targetFileName' => '/existing',
70+
'existingSourceFileContents' => 'NEW',
71+
'existingTargetFileContents' => 'EXISTING',
72+
'expectedTargetFileContents' => 'NEW',
73+
'logMessageRegExp' => '~/existing.+already exists~',
74+
'logMessagePriority' => Project::MSG_VERBOSE,
75+
];
3176
}
3277

33-
public function testCopyFileWithAbsolutePath(): void
78+
/**
79+
* @dataProvider copyFileDataProvider
80+
*
81+
* @param string $target
82+
* @param string $sourceFileName
83+
* @param string $targetFileName
84+
* @param string $existingSourceFileContents
85+
* @param string|null $existingTargetFileContents
86+
* @param string $expectedTargetFileContents
87+
* @param string $logMessageRegExp
88+
* @param int $logMessagePriority
89+
*/
90+
public function testCopyFile(
91+
string $target,
92+
string $sourceFileName,
93+
string $targetFileName,
94+
string $existingSourceFileContents,
95+
?string $existingTargetFileContents,
96+
string $expectedTargetFileContents,
97+
string $logMessageRegExp,
98+
int $logMessagePriority
99+
): void
34100
{
35-
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/foo';
36-
file_put_contents($sourceFilePath, 'FOO');
37-
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/foo-copy';
38-
if (file_exists($targetFilePath)) {
101+
$sourceFilePath = self::TEMP_DIRECTORY_PATH . $sourceFileName;
102+
file_put_contents($sourceFilePath, $existingSourceFileContents);
103+
104+
$targetFilePath = self::TEMP_DIRECTORY_PATH . $targetFileName;
105+
if ($existingTargetFileContents !== null) {
106+
file_put_contents($targetFilePath, $existingTargetFileContents);
107+
} elseif (file_exists($targetFilePath)) {
39108
unlink($targetFilePath);
40109
}
41110

42111
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
43-
$target = __FUNCTION__;
44-
$tester->executeTarget($target);
45-
46-
Assert::assertFileEquals($sourceFilePath, $targetFilePath);
47-
$tester->assertLogMessageRegExp('~Copying.+/foo.+->.+/foo-copy~', $target, Project::MSG_INFO);
48-
}
49-
50-
public function testTargetFileExists(): void
51-
{
52-
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/new';
53-
file_put_contents($sourceFilePath, 'NEW');
54-
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/existing';
55-
file_put_contents($targetFilePath, 'EXISTING');
56-
57-
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
58-
$target = __FUNCTION__;
59-
$tester->executeTarget($target);
60-
61-
Assert::assertFileExists($targetFilePath);
62-
Assert::assertSame('EXISTING', file_get_contents($targetFilePath));
63-
$tester->assertLogMessageRegExp('~/existing.+already exists.+SKIPPING~', $target, Project::MSG_INFO);
64-
}
65-
66-
public function testTargetFileExistsSkip(): void
67-
{
68-
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/new';
69-
file_put_contents($sourceFilePath, 'NEW');
70-
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/existing';
71-
file_put_contents($targetFilePath, 'EXISTING');
72-
73-
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
74-
$target = __FUNCTION__;
75112
$tester->executeTarget($target);
76113

77114
Assert::assertFileExists($targetFilePath);
78-
Assert::assertSame('EXISTING', file_get_contents($targetFilePath));
79-
$tester->assertLogMessageRegExp('~/existing.+already exists.+SKIPPING~', $target, Project::MSG_INFO);
80-
}
81-
82-
public function testTargetFileExistsReplace(): void
83-
{
84-
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/new';
85-
file_put_contents($sourceFilePath, 'NEW');
86-
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/existing';
87-
file_put_contents($targetFilePath, 'EXISTING');
88-
89-
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
90-
$target = __FUNCTION__;
91-
$tester->executeTarget($target);
92-
93-
Assert::assertFileEquals($sourceFilePath, $targetFilePath);
94-
$tester->assertLogMessageRegExp('~/existing.+already exists~', $target, Project::MSG_VERBOSE);
115+
Assert::assertSame($expectedTargetFileContents, file_get_contents($targetFilePath));
116+
$tester->assertLogMessageRegExp($logMessageRegExp, $target, $logMessagePriority);
95117
}
96118

97119
public function testTargetFileExistsFail(): void

0 commit comments

Comments
 (0)