1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2017 Gerrit Addiks. |
4
|
|
|
* This package (including this file) was released under the terms of the GPL-3.0. |
5
|
|
|
* You should have received a copy of the GNU General Public License along with this program. |
6
|
|
|
* If not, see <http://www.gnu.org/licenses/> or send me a mail so i can send you a copy. |
7
|
|
|
* @license GPL-3.0 |
8
|
|
|
* @author Gerrit Addiks <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Addiks\SymfonyGenerics\Tests\Integration\Arguments; |
12
|
|
|
|
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Addiks\SymfonyGenerics\Services\ArgumentCompiler; |
15
|
|
|
use Psr\Container\ContainerInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\HttpFoundation\Request; |
19
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
20
|
|
|
use Doctrine\ORM\EntityManager; |
21
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
22
|
|
|
use Addiks\SymfonyGenerics\Tests\Integration\Arguments\ServiceSample; |
23
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
24
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
25
|
|
|
use Twig_Environment; |
26
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
27
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
28
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
29
|
|
|
use Psr\Log\LoggerInterface; |
30
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
31
|
|
|
use Twig\Environment; |
32
|
|
|
|
33
|
|
|
require_once(__DIR__ . '/ServiceSample.php'); |
34
|
|
|
|
35
|
|
|
final class ArgumentCompilerTest extends TestCase |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
private ArgumentCompiler $compiler; |
|
|
|
|
39
|
|
|
|
40
|
|
|
private ContainerInterface $container; |
41
|
|
|
|
42
|
|
|
private XmlFileLoader $loader; |
43
|
|
|
|
44
|
|
|
private EntityManager $entityManager; |
45
|
|
|
|
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
$this->container = new ContainerBuilder(); |
49
|
|
|
|
50
|
|
|
$this->loader = new XmlFileLoader( |
51
|
|
|
$this->container, |
52
|
|
|
new FileLocator(__DIR__ . '/../../..') |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
$this->loader->load('services.xml'); |
56
|
|
|
|
57
|
|
|
$this->entityManager = $this->createMock(EntityManager::class); |
58
|
|
|
|
59
|
|
|
$this->container->set('doctrine.orm.entity_manager', $this->entityManager); |
60
|
|
|
$this->container->set('request_stack', new RequestStack()); |
61
|
|
|
$this->container->set('twig', $this->createMock(Environment::class)); |
62
|
|
|
$this->container->set('security.authorization_checker', $this->createMock(AuthorizationCheckerInterface::class)); |
63
|
|
|
$this->container->set('router', $this->createMock(UrlGeneratorInterface::class)); |
64
|
|
|
$this->container->set('session', $this->createMock(Session::class)); |
65
|
|
|
$this->container->set('logger', $this->createMock(LoggerInterface::class)); |
66
|
|
|
$this->container->set('event_dispatcher', $this->createMock(EventDispatcherInterface::class)); |
67
|
|
|
|
68
|
|
|
$this->compiler = $this->container->get('symfony_generics.argument_compiler'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
* @dataProvider dataProviderForShouldBuildArguments |
74
|
|
|
*/ |
75
|
|
|
public function shouldBuildArguments($expectedResult, $argumentsConfiguration) |
76
|
|
|
{ |
77
|
|
|
/** @var Request $request */ |
78
|
|
|
$request = $this->createMock(Request::class); |
79
|
|
|
$request->method("getContent")->willReturn("request-content"); |
80
|
|
|
$request->method("get")->will($this->returnValueMap([ |
81
|
|
|
['foo', null, 'foo-result'], |
82
|
|
|
])); |
83
|
|
|
|
84
|
|
|
/** @var UploadedFile $file */ |
85
|
|
|
$file = $this->createMock(UploadedFile::class); |
86
|
|
|
$file->method('getPathname')->willReturn('data://,some-file-content'); |
87
|
|
|
$file->method('getClientOriginalName')->willReturn('some-original-file-name'); |
88
|
|
|
$file->method('getFilename')->willReturn('some-file-name'); |
89
|
|
|
$file->method('getMimeType')->willReturn('some/mime-type'); |
90
|
|
|
|
91
|
|
|
$request->files = $this->createMock(FileBag::class); |
92
|
|
|
$request->files->expects($this->any())->method('get')->with($this->equalTo('foo'))->willReturn($file); |
93
|
|
|
|
94
|
|
|
$someService = new ServiceSample(); |
95
|
|
|
|
96
|
|
|
$this->entityManager->method('find')->will($this->returnValueMap([ |
97
|
|
|
['Foo\\Bar\\Baz', 'foo-result', null, null, $someService], |
98
|
|
|
['Foo\\Bar\\Baz', '123', null, null, $someService], |
99
|
|
|
])); |
100
|
|
|
|
101
|
|
|
$this->container->get('request_stack')->push($request); |
102
|
|
|
|
103
|
|
|
$this->container->set('some.service', $someService); |
104
|
|
|
$this->container->set('some_service', $someService); |
105
|
|
|
|
106
|
|
|
/** @var array $additionalData */ |
107
|
|
|
$additionalData = [ |
108
|
|
|
'some_additional_argument' => 'addArgRes', |
109
|
|
|
'some_argument_service' => $someService |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
/** @var mixed $actualResult */ |
113
|
|
|
$actualResult = $this->compiler->buildArguments([$argumentsConfiguration], $additionalData); |
114
|
|
|
|
115
|
|
|
$this->assertEquals($expectedResult, $actualResult[0]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function dataProviderForShouldBuildArguments() |
119
|
|
|
{ |
120
|
|
|
return array( |
121
|
|
|
[null, ""], |
122
|
|
|
[true, "true"], |
123
|
|
|
[false, "false"], |
124
|
|
|
[null, "null"], |
125
|
|
|
["literal", "literal"], |
126
|
|
|
["literal", "'literal'"], |
127
|
|
|
["literal", '"literal"'], |
128
|
|
|
["request-content", '$'], |
129
|
|
|
["foo-result", '$foo'], |
130
|
|
|
["some-file-content", '$files.foo'], |
131
|
|
|
[$this->createMock(UploadedFile::class), '$files.foo.object'], |
132
|
|
|
["some-original-file-name", '$files.foo.originalname'], |
133
|
|
|
["some-file-name", '$files.foo.filename'], |
134
|
|
|
["some-file-content", '$files.foo.content'], |
135
|
|
|
["some/mime-type", '$files.foo.mimetype'], |
136
|
|
|
[new ServiceSample(), 'Foo\\Bar\\Baz#123'], |
137
|
|
|
[new ServiceSample(), 'Foo\\Bar\\Baz#$foo'], |
138
|
|
|
["#qwe#foo#", 'Foo\\Bar\\Baz#$foo::bar'], |
139
|
|
|
["#baz#foo#", 'Foo\\Bar\\Baz#$foo::bar(baz)'], |
140
|
|
|
["#qwe#foo#", '@some.service::bar'], |
141
|
|
|
["#qwe#foo#", ' @some.service::bar '], |
142
|
|
|
["#qwe#foo#", "\[email protected]::bar\n"], |
143
|
|
|
["#asd#baz#", '@some_service::bar(\'asd\', baz)'], |
144
|
|
|
["addArgRes", '{some_additional_argument}'], |
145
|
|
|
["#qwe#foo#", '{some_argument_service}::bar'], |
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
} |
150
|
|
|
|