1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016 |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
namespace Aimeos\Admin\JQAdm\Common\Decorator; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
class CacheTest extends \PHPUnit_Framework_TestCase |
13
|
|
|
{ |
14
|
|
|
private $context; |
15
|
|
|
private $object; |
16
|
|
|
private $mock; |
17
|
|
|
private $cache; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
protected function setUp() |
21
|
|
|
{ |
22
|
|
|
$this->cache = $this->getMockBuilder( 'Aimeos\MW\Cache\None' ) |
23
|
|
|
->setMethods( array( 'deleteByTags' ) ) |
24
|
|
|
->disableOriginalConstructor() |
25
|
|
|
->getMock(); |
26
|
|
|
|
27
|
|
|
$this->mock = $this->getMockBuilder( 'Aimeos\Admin\JQAdm\Product\Standard' ) |
28
|
|
|
->setMethods( array( 'delete', 'save' ) ) |
29
|
|
|
->disableOriginalConstructor() |
30
|
|
|
->getMock(); |
31
|
|
|
|
32
|
|
|
$templatePaths = \TestHelperJqadm::getTemplatePaths(); |
33
|
|
|
$this->context = \TestHelperJqadm::getContext(); |
34
|
|
|
$this->context->setCache( $this->cache ); |
35
|
|
|
|
36
|
|
|
$this->object = new \Aimeos\Admin\JQAdm\Common\Decorator\Cache( $this->mock, $this->context, $templatePaths ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
protected function tearDown() |
41
|
|
|
{ |
42
|
|
|
unset( $this->object, $this->mock, $this->context, $this->cache ); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
public function testDelete() |
47
|
|
|
{ |
48
|
|
|
$view = \TestHelperJqadm::getView(); |
49
|
|
|
$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, array( 'id' => 1 ) ); |
50
|
|
|
$view->addHelper( 'param', $helper ); |
51
|
|
|
|
52
|
|
|
$tags = array( 'product', 'product-1' ); |
53
|
|
|
$this->cache->expects( $this->once() )->method( 'deleteByTags' )->with( $this->equalTo( $tags ) ); |
54
|
|
|
$this->mock->expects( $this->once() )->method( 'delete' )->will( $this->returnValue( 'test' ) ); |
55
|
|
|
|
56
|
|
|
$this->object->setView( $view ); |
57
|
|
|
$result = $this->object->delete(); |
58
|
|
|
|
59
|
|
|
$this->assertEquals( 'test', $result ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
public function testSave() |
64
|
|
|
{ |
65
|
|
|
$item = \Aimeos\MShop\Factory::createManager( $this->context, 'product' )->findItem( 'CNC' ); |
|
|
|
|
66
|
|
|
|
67
|
|
|
$tags = array( 'product', 'product-' . $item->getId() ); |
68
|
|
|
$view = \TestHelperJqadm::getView(); |
69
|
|
|
$view->item = $item; |
70
|
|
|
|
71
|
|
|
$this->cache->expects( $this->once() )->method( 'deleteByTags' )->with( $this->equalTo( $tags ) ); |
72
|
|
|
$this->mock->expects( $this->once() )->method( 'save' )->will( $this->returnValue( 'test' ) ); |
73
|
|
|
|
74
|
|
|
$this->object->setView( $view ); |
75
|
|
|
$result = $this->object->save(); |
76
|
|
|
|
77
|
|
|
$this->assertEquals( 'test', $result ); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: