Repository navigation
Expand file tree
/
Copy pathSourceCodeTest.php
More file actions
89 lines (70 loc) · 2.69 KB
/
Copy pathSourceCodeTest.php
File metadata and controls
89 lines (70 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
namespace Phpactor\CodeTransform\Tests\Unit\Domain;
use PHPUnit\Framework\TestCase;
use Phpactor\CodeTransform\Domain\SourceCode;
use RuntimeException;
class SourceCodeTest extends TestCase
{
const PATH = '/bar';
const SOURCE = 'asd';
const OTHER_SOURCE = 'other source';
const OTHER_PATH = '/other/path.php';
public function testPath(): void
{
$source = SourceCode::fromStringAndPath(self::SOURCE, self::PATH);
$this->assertEquals(self::PATH, $source->path());
}
public function testFromUnknownReturnsSourceCodeIfPassedSourceCode(): void
{
$source1 = SourceCode::fromStringAndPath(self::SOURCE, self::PATH);
$source2 = SourceCode::fromUnknown($source1);
$this->assertSame($source1, $source2);
}
public function testFromUnknownReturnsSourceCodeIfPassedString(): void
{
$source1 = 'hello';
$source2 = SourceCode::fromUnknown($source1);
$this->assertEquals(SourceCode::fromString($source1), $source2);
}
public function testFromUnknownThrowsExceptionIfTypeIsInvalid(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Do not know');
$source2 = SourceCode::fromUnknown(1234);
}
public function testWithSource(): void
{
$source1 = SourceCode::fromStringAndPath(self::SOURCE, self::PATH);
$source2 = $source1->withSource(self::OTHER_SOURCE);
$this->assertEquals(self::OTHER_SOURCE, $source2->__toString());
$this->assertNotSame($source1, $source2);
}
public function testWithPath(): void
{
$source1 = SourceCode::fromStringAndPath(self::SOURCE, self::PATH);
$source2 = $source1->withPath(self::OTHER_PATH);
$this->assertEquals(self::OTHER_PATH, $source2->path());
$this->assertNotSame($source1, $source2);
}
public function testNonAbsolutePath(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Path "path" must be absolute');
SourceCode::fromStringAndPath('asdf', 'path');
}
public function testCanonicalizePath(): void
{
$sourceCode = SourceCode::fromStringAndPath('asd', '/path/to/here/../');
$this->assertEquals('/path/to', $sourceCode->path());
}
public function testExtractSelection(): void
{
$sourceCode = SourceCode::fromString('12345678');
$this->assertEquals('34', $sourceCode->extractSelection(2, 4));
}
public function testReplaceSelection(): void
{
$sourceCode = SourceCode::fromString('12345678');
$this->assertEquals('12HE5678', (string) $sourceCode->replaceSelection('HE', 2, 4));
}
}