Skip to content

Commit bfe9bbe

Browse files
committed
extract multiple cases from test methods to data providers
1 parent ae4b8c8 commit bfe9bbe

1 file changed

Lines changed: 71 additions & 74 deletions

File tree

tests/CopyFilesTaskIntegrationTest.php

Lines changed: 71 additions & 74 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,24 +14,28 @@ 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);
22+
yield 'copy file' => [
23+
'target' => 'testCopyFile',
24+
];
2825

29-
Assert::assertFileEquals($sourceFilePath, $targetFilePath);
30-
$tester->assertLogMessageRegExp('~Copying.+/foo.+->.+/foo-copy~', $target, Project::MSG_INFO);
26+
yield 'copy file with absolute path' => [
27+
'target' => 'testCopyFileWithAbsolutePath',
28+
];
3129
}
3230

33-
public function testCopyFileWithAbsolutePath(): void
31+
/**
32+
* @dataProvider copyFileDataProvider
33+
*
34+
* @param string $target
35+
*/
36+
public function testCopyFile(
37+
string $target
38+
): void
3439
{
3540
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/foo';
3641
file_put_contents($sourceFilePath, 'FOO');
@@ -40,38 +45,41 @@ public function testCopyFileWithAbsolutePath(): void
4045
}
4146

4247
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
43-
$target = __FUNCTION__;
4448
$tester->executeTarget($target);
4549

4650
Assert::assertFileEquals($sourceFilePath, $targetFilePath);
4751
$tester->assertLogMessageRegExp('~Copying.+/foo.+->.+/foo-copy~', $target, Project::MSG_INFO);
4852
}
4953

50-
public function testTargetFileExists(): void
54+
/**
55+
* @return mixed[][]|\Generator
56+
*/
57+
public function targetFileExistsDataProvider(): Generator
5158
{
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);
59+
yield 'target file exists' => [
60+
'target' => 'testTargetFileExists',
61+
];
6062

61-
Assert::assertFileExists($targetFilePath);
62-
Assert::assertSame('EXISTING', file_get_contents($targetFilePath));
63-
$tester->assertLogMessageRegExp('~/existing.+already exists.+SKIPPING~', $target, Project::MSG_INFO);
63+
yield 'target file exists skip' => [
64+
'target' => 'testTargetFileExistsSkip',
65+
];
6466
}
6567

66-
public function testTargetFileExistsSkip(): void
68+
/**
69+
* @dataProvider targetFileExistsDataProvider
70+
*
71+
* @param string $target
72+
*/
73+
public function testTargetFileExists(
74+
string $target
75+
): void
6776
{
6877
$sourceFilePath = self::TEMP_DIRECTORY_PATH . '/new';
6978
file_put_contents($sourceFilePath, 'NEW');
7079
$targetFilePath = self::TEMP_DIRECTORY_PATH . '/existing';
7180
file_put_contents($targetFilePath, 'EXISTING');
7281

7382
$tester = new PhingTester(__DIR__ . '/copy-files-task-integration-test.xml', self::TEMP_DIRECTORY_PATH);
74-
$target = __FUNCTION__;
7583
$tester->executeTarget($target);
7684

7785
Assert::assertFileExists($targetFilePath);
@@ -264,63 +272,52 @@ public function testCopyFileToNonExistingDirectory(): void
264272
$tester->assertLogMessageRegExp('~/foo.+cannot be copied.+/foo-copy~', $target, Project::MSG_ERR);
265273
}
266274

267-
public function testMissingCopyFileElement(): void
268-
{
269-
$buildFilePath = __DIR__ . '/copy-files-task-integration-test.xml';
270-
$tester = new PhingTester($buildFilePath);
271-
$target = __FUNCTION__;
272-
273-
try {
274-
$tester->executeTarget($target);
275-
Assert::fail('Exception expected');
276-
} catch (\BuildException $e) {
277-
Assert::assertStringStartsWith($buildFilePath, $e->getLocation()->toString());
278-
Assert::assertRegExp('~one.+<file>.+expected~', $e->getMessage());
279-
}
280-
}
281-
282-
public function testMissingSource(): void
275+
/**
276+
* @return mixed[][]|\Generator
277+
*/
278+
public function throwBuildExceptionDataProvider(): Generator
283279
{
284-
$buildFilePath = __DIR__ . '/copy-files-task-integration-test.xml';
285-
$tester = new PhingTester($buildFilePath);
286-
$target = __FUNCTION__;
287-
288-
try {
289-
$tester->executeTarget($target);
290-
Assert::fail('Exception expected');
291-
} catch (\BuildException $e) {
292-
Assert::assertStringStartsWith($buildFilePath, $e->getLocation()->toString());
293-
Assert::assertRegExp('~<file>.+`source`~', $e->getMessage());
294-
}
280+
yield 'missing copy file element' => [
281+
'target' => 'testMissingCopyFileElement',
282+
'expectedMessagePatternRegExp' => '~one.+<file>.+expected~',
283+
];
284+
285+
yield 'missing source' => [
286+
'target' => 'testMissingSource',
287+
'expectedMessagePatternRegExp' => '~<file>.+`source`~',
288+
];
289+
290+
yield 'missing target' => [
291+
'target' => 'testMissingTarget',
292+
'expectedMessagePatternRegExp' => '~<file>.+`target`~',
293+
];
294+
295+
yield 'invalid file exists mode' => [
296+
'target' => 'testInvalidFileExistsMode',
297+
'expectedMessagePatternRegExp' => '~invalid.+mode~i',
298+
];
295299
}
296300

297-
public function testMissingTarget(): void
301+
/**
302+
* @dataProvider throwBuildExceptionDataProvider
303+
*
304+
* @param string $target
305+
* @param string $expectedMessagePatternRegExp
306+
*/
307+
public function testThrowBuildException(
308+
string $target,
309+
string $expectedMessagePatternRegExp
310+
): void
298311
{
299312
$buildFilePath = __DIR__ . '/copy-files-task-integration-test.xml';
300313
$tester = new PhingTester($buildFilePath);
301-
$target = __FUNCTION__;
302-
303-
try {
304-
$tester->executeTarget($target);
305-
Assert::fail('Exception expected');
306-
} catch (\BuildException $e) {
307-
Assert::assertStringStartsWith($buildFilePath, $e->getLocation()->toString());
308-
Assert::assertRegExp('~<file>.+`target`~', $e->getMessage());
309-
}
310-
}
311-
312-
public function testInvalidFileExistsMode(): void
313-
{
314-
$buildFilePath = __DIR__ . '/copy-files-task-integration-test.xml';
315-
$tester = new PhingTester($buildFilePath);
316-
$target = __FUNCTION__;
317314

318315
try {
319316
$tester->executeTarget($target);
320317
Assert::fail('Exception expected');
321318
} catch (\BuildException $e) {
322319
Assert::assertStringStartsWith($buildFilePath, $e->getLocation()->toString());
323-
Assert::assertRegExp('~invalid.+mode~i', $e->getMessage());
320+
Assert::assertRegExp($expectedMessagePatternRegExp, $e->getMessage());
324321
}
325322
}
326323

0 commit comments

Comments
 (0)