Skip to content

Commit b94eaed

Browse files
author
He, Joan(johe)
committed
Merge pull request #610 from magento-extensibility/MAGETWO-42977-use-factories-to-inject-dependencies
MAGETWO-42977: Use factories to inject dependencies on Zend classes in \Magento\CacheInvalidate\Model\PurgeCache
2 parents 2dc1854 + ec0cf53 commit b94eaed

File tree

7 files changed

+97
-23
lines changed

7 files changed

+97
-23
lines changed

app/code/Magento/CacheInvalidate/Model/PurgeCache.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@
1616
class PurgeCache
1717
{
1818
/**
19-
* @var Uri
19+
* @var UriFactory
2020
*/
21-
protected $uri;
21+
protected $uriFactory;
2222

2323
/**
24-
* @var Socket
24+
* @var SocketFactory
2525
*/
26-
protected $socketAdapter;
26+
protected $socketAdapterFactory;
2727

2828
/**
2929
* @var InvalidateLogger
@@ -45,21 +45,21 @@ class PurgeCache
4545
/**
4646
* Constructor
4747
*
48-
* @param Uri $uri
49-
* @param Socket $socketAdapter
48+
* @param UriFactory $uriFactory
49+
* @param SocketFactory $socketAdapterFactory
5050
* @param InvalidateLogger $logger
5151
* @param Reader $configReader
5252
* @param RequestInterface $request
5353
*/
5454
public function __construct(
55-
Uri $uri,
56-
Socket $socketAdapter,
55+
UriFactory $uriFactory,
56+
SocketFactory $socketAdapterFactory,
5757
InvalidateLogger $logger,
5858
DeploymentConfig $config,
5959
RequestInterface $request
6060
) {
61-
$this->uri = $uri;
62-
$this->socketAdapter = $socketAdapter;
61+
$this->uriFactory = $uriFactory;
62+
$this->socketAdapterFactory = $socketAdapterFactory;
6363
$this->logger = $logger;
6464
$this->config = $config;
6565
$this->request = $request;
@@ -74,24 +74,26 @@ public function __construct(
7474
*/
7575
public function sendPurgeRequest($tagsPattern)
7676
{
77+
$uri = $this->uriFactory->create();
78+
$socketAdapter = $this->socketAdapterFactory->create();
7779
$servers = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
7880
?: [['host' => $this->request->getHttpHost()]];
7981
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
80-
$this->socketAdapter->setOptions(['timeout' => 10]);
82+
$socketAdapter->setOptions(['timeout' => 10]);
8183
foreach ($servers as $server) {
8284
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
83-
$this->uri->setScheme('http')
85+
$uri->setScheme('http')
8486
->setHost($server['host'])
8587
->setPort($port);
8688
try {
87-
$this->socketAdapter->connect($server['host'], $port);
88-
$this->socketAdapter->write(
89+
$socketAdapter->connect($server['host'], $port);
90+
$socketAdapter->write(
8991
'PURGE',
90-
$this->uri,
92+
$uri,
9193
'1.1',
9294
$headers
9395
);
94-
$this->socketAdapter->close();
96+
$socketAdapter->close();
9597
} catch (Exception $e) {
9698
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
9799
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Model;
7+
8+
class SocketFactory
9+
{
10+
11+
/**
12+
* @return \Zend\Http\Client\Adapter\Socket
13+
*/
14+
public function create()
15+
{
16+
return new \Zend\Http\Client\Adapter\Socket();
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Model;
7+
8+
class UriFactory
9+
{
10+
11+
/**
12+
* @return \Zend\Uri\Uri
13+
*/
14+
public function create()
15+
{
16+
return new \Zend\Uri\Uri();
17+
}
18+
}

app/code/Magento/CacheInvalidate/Test/Unit/Model/PurgeCacheTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
*/
66
namespace Magento\CacheInvalidate\Test\Unit\Model;
77

8-
use \Magento\Framework\Config\ConfigOptionsListConstants;
9-
108
class PurgeCacheTest extends \PHPUnit_Framework_TestCase
119
{
1210
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
@@ -32,17 +30,25 @@ class PurgeCacheTest extends \PHPUnit_Framework_TestCase
3230
*/
3331
public function setUp()
3432
{
33+
$this->uriFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\UriFactory', [], [], '', false);
3534
$this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false);
35+
$this->socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false);
3636
$this->socketAdapterMock = $this->getMock('\Zend\Http\Client\Adapter\Socket', [], [], '', false);
3737
$this->configMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
3838
$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
3939
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
4040
$this->socketAdapterMock->expects($this->once())
4141
->method('setOptions')
4242
->with(['timeout' => 10]);
43+
$this->uriFactoryMock->expects($this->once())
44+
->method('create')
45+
->willReturn($this->uriMock);
46+
$this->socketFactoryMock->expects($this->once())
47+
->method('create')
48+
->willReturn($this->socketAdapterMock);
4349
$this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
44-
$this->uriMock,
45-
$this->socketAdapterMock,
50+
$this->uriFactoryMock,
51+
$this->socketFactoryMock,
4652
$this->loggerMock,
4753
$this->configMock,
4854
$this->requestMock
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Test\Unit\Model;
7+
8+
class SocketFactoryTest extends \PHPUnit_Framework_TestCase
9+
{
10+
11+
public function testCreate()
12+
{
13+
$factory = new \Magento\CacheInvalidate\Model\SocketFactory();
14+
$this->assertInstanceOf('\Zend\Http\Client\Adapter\Socket', $factory->create());
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CacheInvalidate\Test\Unit\Model;
7+
8+
class UriFactoryTest extends \PHPUnit_Framework_TestCase
9+
{
10+
11+
public function testCreate()
12+
{
13+
$factory = new \Magento\CacheInvalidate\Model\UriFactory();
14+
$this->assertInstanceOf('\Zend\Uri\Uri', $factory->create());
15+
}
16+
}

lib/internal/Magento/Framework/Cache/Test/Unit/InvalidateLoggerTest.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
* See COPYING.txt for license details.
55
*/
66

7-
// @codingStandardsIgnoreFile
8-
97
/**
108
* \Magento\Framework\Cache\InvalidateLogger test case
119
*/
@@ -68,7 +66,7 @@ public function testExecute()
6866

6967
public function testMakeParams()
7068
{
71-
$expected = ['method' => $this->method, 'url' => $this->url, 'invalidateInfo' => $this->params];;
69+
$expected = ['method' => $this->method, 'url' => $this->url, 'invalidateInfo' => $this->params];
7270
$method = new \ReflectionMethod($this->invalidateLogger, 'makeParams');
7371
$method->setAccessible(true);
7472
$this->assertEquals(

0 commit comments

Comments
 (0)