1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Tests\Generator\Resolvers; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Configuration\ConfigurationInterface; |
6
|
|
|
use ApiGen\Generator\Resolvers\RelativePathResolver; |
7
|
|
|
use ApiGen\Utils\FileSystem; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
final class RelativePathResolverTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
public function testGetRelativePath(): void |
13
|
|
|
{ |
14
|
|
|
$configurationMock = $this->createMock(ConfigurationInterface::class); |
15
|
|
|
$configurationMock->method('getOption') |
16
|
|
|
->willReturn([TEMP_DIR]); |
17
|
|
|
|
18
|
|
|
$relativePathResolver = new RelativePathResolver($configurationMock, new FileSystem); |
19
|
|
|
|
20
|
|
|
$this->assertSame('some-file.txt', $relativePathResolver->getRelativePath(TEMP_DIR . '/some-file.txt')); |
21
|
|
|
$this->assertSame( |
22
|
|
|
'some/dir/some-file.txt', |
23
|
|
|
$relativePathResolver->getRelativePath(TEMP_DIR . '/some/dir/some-file.txt') |
24
|
|
|
); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
public function testGetRelativePathWithWindowsPath(): void |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$configurationMock = $this->createMock(ConfigurationInterface::class); |
30
|
|
|
$configurationMock->method('getOption') |
31
|
|
|
->willReturn(['C:\some\dir']); |
32
|
|
|
|
33
|
|
|
$relativePathResolver = new RelativePathResolver($configurationMock, new FileSystem); |
34
|
|
|
|
35
|
|
|
$this->assertSame('file.txt', $relativePathResolver->getRelativePath('C:\some\dir\file.txt')); |
36
|
|
|
$this->assertSame('more-dir/file.txt', $relativePathResolver->getRelativePath('C:\some\dir\more-dir\file.txt')); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @expectedException \InvalidArgumentException |
41
|
|
|
*/ |
42
|
|
|
public function testGetRelativePathInvalid(): void |
43
|
|
|
{ |
44
|
|
|
$configurationMock = $this->createMock(ConfigurationInterface::class); |
45
|
|
|
$configurationMock->method('getOption') |
46
|
|
|
->willReturn([TEMP_DIR]); |
47
|
|
|
|
48
|
|
|
$relativePathResolver = new RelativePathResolver($configurationMock, new FileSystem); |
49
|
|
|
|
50
|
|
|
$relativePathResolver->getRelativePath('/var/dir/some-strange-file.txt'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Issue #408 |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testGetRelativePathWithSourceEndingSlash(): void |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$configurationMock = $this->createMock(ConfigurationInterface::class); |
59
|
|
|
$configurationMock->method('getOption') |
60
|
|
|
->with('source') |
61
|
|
|
->willReturn(['ProjectBeta']); |
62
|
|
|
|
63
|
|
|
$relativePathResolver = new RelativePathResolver($configurationMock, new FileSystem); |
64
|
|
|
|
65
|
|
|
$fileName = 'ProjectBeta/entities/Category.php'; |
66
|
|
|
$this->assertSame('entities/Category.php', $relativePathResolver->getRelativePath($fileName)); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.