|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace AbterPhp\Files\Form\Factory; |
|
6
|
|
|
|
|
7
|
|
|
use AbterPhp\Files\Domain\Entities\File as Entity; |
|
8
|
|
|
use AbterPhp\Files\Domain\Entities\FileCategory; |
|
|
|
|
|
|
9
|
|
|
use AbterPhp\Files\Orm\FileCategoryRepo; |
|
10
|
|
|
use AbterPhp\Framework\I18n\ITranslator; |
|
11
|
|
|
use Opulence\Http\Requests\RequestMethods; |
|
12
|
|
|
use Opulence\Sessions\ISession; |
|
13
|
|
|
use Opulence\Sessions\Session; |
|
14
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
15
|
|
|
use PHPUnit\Framework\TestCase; |
|
16
|
|
|
|
|
17
|
|
|
class FileTest extends TestCase |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var ISession|MockObject */ |
|
20
|
|
|
protected $sessionMock; |
|
21
|
|
|
|
|
22
|
|
|
/** @var ITranslator|MockObject */ |
|
23
|
|
|
protected $translatorMock; |
|
24
|
|
|
|
|
25
|
|
|
/** @var FileCategoryRepo|MockObject */ |
|
26
|
|
|
protected $fileCategoryRepoMock; |
|
27
|
|
|
|
|
28
|
|
|
/** @var File */ |
|
29
|
|
|
protected $sut; |
|
30
|
|
|
|
|
31
|
|
|
public function setUp(): void |
|
32
|
|
|
{ |
|
33
|
|
|
parent::setUp(); |
|
34
|
|
|
|
|
35
|
|
|
$this->sessionMock = $this->getMockBuilder(Session::class) |
|
36
|
|
|
->onlyMethods(['get']) |
|
37
|
|
|
->getMock(); |
|
38
|
|
|
$this->sessionMock->expects($this->any())->method('get')->willReturnArgument(0); |
|
39
|
|
|
|
|
40
|
|
|
$this->translatorMock = $this->getMockBuilder(ITranslator::class) |
|
41
|
|
|
->onlyMethods(['translate', 'canTranslate']) |
|
42
|
|
|
->getMock(); |
|
43
|
|
|
$this->translatorMock->expects($this->any())->method('translate')->willReturnArgument(0); |
|
44
|
|
|
|
|
45
|
|
|
$this->fileCategoryRepoMock = $this->getMockBuilder(FileCategoryRepo::class) |
|
46
|
|
|
->disableOriginalConstructor() |
|
47
|
|
|
->onlyMethods(['getAll']) |
|
48
|
|
|
->getMock(); |
|
49
|
|
|
|
|
50
|
|
|
$this->sut = new File($this->sessionMock, $this->translatorMock, $this->fileCategoryRepoMock); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function testCreate() |
|
54
|
|
|
{ |
|
55
|
|
|
$action = 'foo'; |
|
56
|
|
|
$method = RequestMethods::POST; |
|
57
|
|
|
$showUrl = 'bar'; |
|
58
|
|
|
$entityId = '59b927de-7fea-4866-97fb-2036d4fdbe2e'; |
|
59
|
|
|
$identifier = 'blah'; |
|
60
|
|
|
$allFileCategories = [ |
|
61
|
|
|
new FileCategory('a3e90fa1-3003-465d-82e0-570baa0aa53f', 'fc-22', 'FC 22', true, []), |
|
62
|
|
|
new FileCategory('bd40cbaf-29a9-4371-aa5d-e97966792c92', 'fc-73', 'FC 73', true, []), |
|
63
|
|
|
new FileCategory('544f7fac-38e1-4103-932f-452ea52733ed', 'fc-112', 'FC 112', false, []), |
|
64
|
|
|
new FileCategory('63c18995-d24a-41bb-9016-ebca473019e9', 'fc-432', 'FC 432', true, []), |
|
65
|
|
|
]; |
|
66
|
|
|
$fileCategory = $allFileCategories[1]; |
|
67
|
|
|
|
|
68
|
|
|
$this->fileCategoryRepoMock |
|
69
|
|
|
->expects($this->any()) |
|
70
|
|
|
->method('getAll') |
|
71
|
|
|
->willReturn($allFileCategories); |
|
72
|
|
|
|
|
73
|
|
|
$entityMock = $this->createMockEntity(); |
|
74
|
|
|
|
|
75
|
|
|
$entityMock->expects($this->any())->method('getId')->willReturn($entityId); |
|
76
|
|
|
$entityMock->expects($this->any())->method('getDescription')->willReturn($identifier); |
|
77
|
|
|
$entityMock->expects($this->any())->method('getCategory')->willReturn($fileCategory); |
|
78
|
|
|
|
|
79
|
|
|
$form = (string)$this->sut->create($action, $method, $showUrl, $entityMock); |
|
80
|
|
|
|
|
81
|
|
|
$this->assertStringContainsString($action, $form); |
|
82
|
|
|
$this->assertStringContainsString($showUrl, $form); |
|
83
|
|
|
$this->assertStringContainsString('CSRF', $form); |
|
84
|
|
|
$this->assertStringContainsString('POST', $form); |
|
85
|
|
|
$this->assertStringContainsString('file', $form); |
|
86
|
|
|
$this->assertStringContainsString('description', $form); |
|
87
|
|
|
$this->assertStringContainsString('file_category_id', $form); |
|
88
|
|
|
$this->assertStringContainsString('selected', $form); |
|
89
|
|
|
$this->assertStringContainsString('button', $form); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return MockObject|Entity |
|
94
|
|
|
*/ |
|
95
|
|
|
protected function createMockEntity() |
|
96
|
|
|
{ |
|
97
|
|
|
$entityMock = $this->getMockBuilder(Entity::class) |
|
98
|
|
|
->disableOriginalConstructor() |
|
99
|
|
|
->onlyMethods( |
|
100
|
|
|
[ |
|
101
|
|
|
'getId', |
|
102
|
|
|
'getDescription', |
|
103
|
|
|
'getCategory', |
|
104
|
|
|
] |
|
105
|
|
|
) |
|
106
|
|
|
->getMock(); |
|
107
|
|
|
|
|
108
|
|
|
return $entityMock; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: