1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skaut\Skautis\Test\Unit; |
4
|
|
|
|
5
|
|
|
use Mockery\MockInterface; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Skaut\Skautis\User; |
8
|
|
|
use Skaut\Skautis\Wsdl\Decorator\Cache\CacheDecorator; |
9
|
|
|
use Skaut\Skautis\Wsdl\WebServiceInterface; |
10
|
|
|
use Symfony\Component\Cache\Adapter\ArrayAdapter; |
11
|
|
|
use Symfony\Component\Cache\Psr16Cache; |
12
|
|
|
|
13
|
|
|
class CacheDecoratorTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
protected function tearDown(): void |
17
|
|
|
{ |
18
|
|
|
\Mockery::close(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testDecoratorRequireRequest() |
22
|
|
|
{ |
23
|
|
|
$value = ['id' => 'response']; |
24
|
|
|
$args = ['asd', 'uv', User::ID_LOGIN => 'a']; |
25
|
|
|
|
26
|
|
|
/** @var WebServiceInterface|MockInterface $webService */ |
27
|
|
|
$webService = \Mockery::mock(WebServiceInterface::class); |
28
|
|
|
$webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value); |
|
|
|
|
29
|
|
|
|
30
|
|
|
$cache = new Psr16Cache(new ArrayAdapter()); |
31
|
|
|
|
32
|
|
|
//Prazdna cache, musi poslat request |
33
|
|
|
$decoratedServiceA = new CacheDecorator($webService, $cache, 30); |
|
|
|
|
34
|
|
|
$response = $decoratedServiceA->call('funkceA', $args); |
35
|
|
|
$this->assertEquals($value, $response); |
36
|
|
|
|
37
|
|
|
|
38
|
|
|
//Jina instance WS |
39
|
|
|
/** @var WebServiceInterface|MockInterface $decoratedServiceB */ |
40
|
|
|
$webServiceB = \Mockery::mock(WebServiceInterface::class); |
41
|
|
|
|
42
|
|
|
//Cache naplnena z prechoziho requestu |
43
|
|
|
$decoratedServiceB = new CacheDecorator($webServiceB, $cache, 30); |
|
|
|
|
44
|
|
|
$response = $decoratedServiceB->call('funkceA', $args); |
45
|
|
|
|
46
|
|
|
//Vraci data z cache |
47
|
|
|
$this->assertEquals($value, $response); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testDecorator() |
51
|
|
|
{ |
52
|
|
|
$value = ['id' => 'response']; |
53
|
|
|
$args = ['asd', 'uv']; |
54
|
|
|
|
55
|
|
|
/** @var WebServiceInterface|MockInterface $webService */ |
56
|
|
|
$webService = \Mockery::mock(WebServiceInterface::class); |
57
|
|
|
$webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value); |
|
|
|
|
58
|
|
|
$webService->shouldReceive('call')->with('funkceB', $args)->once()->andReturn($value); |
59
|
|
|
|
60
|
|
|
$cache = new Psr16Cache(new ArrayAdapter()); |
61
|
|
|
$decoratedService = new CacheDecorator($webService, $cache, 30); |
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
// Stejne volani pouze 1x WebService |
67
|
|
|
$response = $decoratedService->call('funkceA', $args); |
68
|
|
|
$this->assertEquals($value, $response); |
69
|
|
|
|
70
|
|
|
// __call() stejny jako call() |
71
|
|
|
$response = $decoratedService->funkceA($args[0], $args[1]); |
72
|
|
|
$this->assertEquals($value, $response); |
73
|
|
|
|
74
|
|
|
// Stejne parametry jina funkce |
75
|
|
|
$response = $decoratedService->call('funkceB', $args); |
76
|
|
|
$this->assertEquals($value, $response); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
// Zmena obsahu parametru |
80
|
|
|
$args[0] = 'qwe'; |
81
|
|
|
$webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value); |
82
|
|
|
|
83
|
|
|
$response = $decoratedService->call('funkceA', $args); |
84
|
|
|
$this->assertEquals($value, $response); |
85
|
|
|
|
86
|
|
|
|
87
|
|
|
// Odebrani parametru |
88
|
|
|
unset($args['1']); |
89
|
|
|
$webService->shouldReceive('call')->with('funkceA', $args)->once()->andReturn($value); |
90
|
|
|
|
91
|
|
|
$response = $decoratedService->call('funkceA', $args); |
92
|
|
|
$this->assertEquals($value, $response); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: