1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* @category EdmondsCommerce |
4
|
|
|
* @package EdmondsCommerce_ |
5
|
|
|
* @author Ross Mitchell <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Field; |
9
|
|
|
|
10
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait; |
11
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\FindAndReplaceHelper; |
12
|
|
|
use EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\NamespaceHelper; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class IdTraitTest |
17
|
|
|
* |
18
|
|
|
* @small |
19
|
|
|
* @testdox EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait |
20
|
|
|
* @coversDefaultClass \EdmondsCommerce\DoctrineStaticMeta\CodeGeneration\Generator\Field\IdTrait |
21
|
|
|
* @package EdmondsCommerce\DoctrineStaticMeta\Tests\Small\CodeGeneration\Field |
22
|
|
|
*/ |
23
|
|
|
class IdTraitTest extends TestCase |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
private $fakeFindAndReplace; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @test |
30
|
|
|
* @covers ::getUseStatement |
31
|
|
|
* @covers ::updateEntity |
32
|
|
|
*/ |
33
|
|
|
public function itWillDefaultToUsingTheUuidTrait(): void |
34
|
|
|
{ |
35
|
|
|
$class = $this->getClass(); |
36
|
|
|
$class->updateEntity('/not/a/real/file'); |
37
|
|
|
$expectedTrait = 'use DSM\Fields\Traits\PrimaryKey\UuidFieldTrait;'; |
38
|
|
|
$actualTrait = $this->fakeFindAndReplace->getTraitThatWasReplaced(); |
39
|
|
|
$this->assertSame($expectedTrait, $actualTrait); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $type |
44
|
|
|
* @param string $expectedTrait |
45
|
|
|
* |
46
|
|
|
* @test |
47
|
|
|
* @covers ::setIdTrait |
48
|
|
|
* @covers ::getUseStatement |
49
|
|
|
* @dataProvider getDifferentIdTraits |
50
|
|
|
*/ |
51
|
|
|
public function itCanUpdateTheTraitsToDifferentOnes(int $type, string $expectedTrait): void |
52
|
|
|
{ |
53
|
|
|
$class = $this->getClass(); |
54
|
|
|
$class->setIdTrait($type); |
55
|
|
|
$class->updateEntity('/not/a/real/file'); |
56
|
|
|
$actualTrait = $this->fakeFindAndReplace->getTraitThatWasReplaced(); |
57
|
|
|
$this->assertSame($expectedTrait, $actualTrait); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getDifferentIdTraits(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
|
|
[IdTrait::INTEGER_ID_FIELD_TRAIT, 'use DSM\Fields\Traits\PrimaryKey\IntegerIdFieldTrait;'], |
64
|
|
|
[IdTrait::NON_BINARY_UUID_TRAIT, 'use DSM\Fields\Traits\PrimaryKey\NonBinaryUuidFieldTrait;'], |
65
|
|
|
[IdTrait::UUID_FIELD_TRAIT, 'use DSM\Fields\Traits\PrimaryKey\UuidFieldTrait;'], |
66
|
|
|
]; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers ::updateEntity |
71
|
|
|
* @test |
72
|
|
|
*/ |
73
|
|
|
public function itWillNotCallTheFindAndReplaceWhenItDoesNotNeedTo(): void |
74
|
|
|
{ |
75
|
|
|
$class = $this->getClass(); |
76
|
|
|
$class->setIdTrait(IdTrait::ID_FIELD_TRAIT); |
77
|
|
|
$class->updateEntity('/not/a/real/file'); |
78
|
|
|
$this->assertNull($this->fakeFindAndReplace->getTraitThatWasReplaced()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @covers ::setIdTrait |
83
|
|
|
* @test |
84
|
|
|
*/ |
85
|
|
|
public function itWillThrowAnExceptionIfGivenAnInvalidType(): void |
86
|
|
|
{ |
87
|
|
|
$class = $this->getClass(); |
88
|
|
|
$this->expectException(\LogicException::class); |
89
|
|
|
$class->setIdTrait(-1); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getClass(): IdTrait |
93
|
|
|
{ |
94
|
|
|
$findReplace = $this->getFakeFindAndReplace(); |
95
|
|
|
|
96
|
|
|
return new IdTrait($findReplace); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
101
|
|
|
*/ |
102
|
|
|
private function getFakeFindAndReplace() |
103
|
|
|
{ |
104
|
|
|
if ($this->fakeFindAndReplace === null) { |
105
|
|
|
$this->fakeFindAndReplace = new class(new NamespaceHelper()) extends FindAndReplaceHelper |
106
|
|
|
{ |
107
|
|
|
|
108
|
|
|
private $traitThatWasReplaced; |
109
|
|
|
|
110
|
|
|
public function findReplace( |
111
|
|
|
string $find, |
112
|
|
|
string $replace, |
113
|
|
|
string $filePath |
114
|
|
|
): FindAndReplaceHelper { |
115
|
|
|
$this->traitThatWasReplaced = $replace; |
116
|
|
|
|
117
|
|
|
return $this; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getTraitThatWasReplaced(): ?string |
121
|
|
|
{ |
122
|
|
|
return $this->traitThatWasReplaced; |
123
|
|
|
} |
124
|
|
|
}; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->fakeFindAndReplace; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|