-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInterfaceFileTest.php
More file actions
111 lines (88 loc) · 3.72 KB
/
Copy pathInterfaceFileTest.php
File metadata and controls
111 lines (88 loc) · 3.72 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
declare(strict_types=1);
namespace Component;
use Dot\Maker\Component\Declaration;
use Dot\Maker\Component\InterfaceFile;
use PHPUnit\Framework\TestCase;
class InterfaceFileTest extends TestCase
{
private string $namespace = 'App\\Module\\Directory';
private string $interfaceName = 'ClassName';
public function testWillInstantiate(): void
{
$interfaceFile = new InterfaceFile($this->namespace, $this->interfaceName);
$this->assertContainsOnlyInstancesOf(InterfaceFile::class, [$interfaceFile]);
$this->assertSame($this->namespace, $interfaceFile->namespace);
$this->assertSame($this->interfaceName, $interfaceFile->interfaceName);
}
public function testAccessorsWillGetAndSetProperties(): void
{
$interfaceFile = new InterfaceFile($this->namespace, $this->interfaceName);
$this->assertCount(0, $interfaceFile->getDeclarations());
$interfaceFile->addDeclaration((new Declaration('getResource'))->setReturnType('Resource'));
$this->assertCount(1, $interfaceFile->getDeclarations());
$this->assertCount(0, $interfaceFile->getInterfaces());
$interfaceFile->addInterface('InterfaceName');
$this->assertCount(1, $interfaceFile->getInterfaces());
$this->assertCount(0, $interfaceFile->getClassUses());
$interfaceFile->useClass('App\\ModuleA\\ClassName');
$this->assertCount(1, $interfaceFile->getClassUses());
$interfaceFile->useClass('App\\ModuleB\\ClassName', 'OtherClassName');
$this->assertCount(2, $interfaceFile->getClassUses());
$this->assertSame([
'use App\\ModuleA\\ClassName;' => 'use App\\ModuleA\\ClassName;',
'use App\\ModuleB\\ClassName as OtherClassName;' => 'use App\\ModuleB\\ClassName as OtherClassName;',
], $interfaceFile->getClassUses());
$this->assertCount(0, $interfaceFile->getFunctionUses());
$interfaceFile->useFunction('some_function');
$this->assertCount(1, $interfaceFile->getFunctionUses());
$this->assertSame([
'use function some_function;' => 'use function some_function;',
], $interfaceFile->getFunctionUses());
$this->assertCount(0, $interfaceFile->getConstantUses());
$interfaceFile->useConstant('SOME_CONSTANT');
$this->assertCount(1, $interfaceFile->getConstantUses());
$this->assertSame([
'use const SOME_CONSTANT;' => 'use const SOME_CONSTANT;',
], $interfaceFile->getConstantUses());
$this->assertTrue($interfaceFile->isStrictTypes());
$interfaceFile->setStrictTypes(false);
$this->assertFalse($interfaceFile->isStrictTypes());
$this->assertSame('', $interfaceFile->getComment());
$interfaceFile->setComment('Comment');
$this->assertSame('Comment', $interfaceFile->getComment());
}
public function testWillRenderInterface(): void
{
$actual = (new InterfaceFile($this->namespace, $this->interfaceName))
->addInterface('OtherInterface')
->useClass('App\\Module\\OtherInterface')
->useFunction('sprintf')
->useConstant('PHP_EOL')
->setComment(<<<COMM
/**
* Comment
*/
COMM)
->addDeclaration((new Declaration('getResource'))->setReturnType('Resource'));
$this->assertSame($this->dataProviderRenderedClass(), (string) $actual);
}
private function dataProviderRenderedClass(): string
{
return <<<BODY
<?php
declare(strict_types=1);
namespace App\Module\Directory;
use App\Module\OtherInterface;
use function sprintf;
use const PHP_EOL;
/**
* Comment
*/
interface ClassName extends OtherInterface
{
public function getResource(): Resource;
}
BODY;
}
}