Skip to content

Commit

Permalink
MAGETWO-43452: Ogre sprint 38 contribution to mainline
Browse files Browse the repository at this point in the history
Merge remote-tracking branch 'mainline/develop' into MAGETWO-40848-components-from-vendor

Conflicts:
	composer.lock
	lib/internal/Magento/Framework/App/Test/Unit/Resource/Config/_files/invalidResourcesXmlArray.php
	lib/internal/Magento/Framework/App/Test/Unit/Resource/Config/_files/valid_resources.xml
  • Loading branch information
Ivan Gavryshko committed Oct 7, 2015
2 parents 55c7214 + 168d27c commit 9449564
Show file tree
Hide file tree
Showing 43 changed files with 1,151 additions and 385 deletions.
7 changes: 7 additions & 0 deletions .htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,10 @@
## http://developer.yahoo.com/performance/rules.html#etags

#FileETag none

############################################
## Add custom headers
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
Header set X-XSS-Protection "1; mode=block"
</IfModule>
63 changes: 19 additions & 44 deletions app/code/Magento/CacheInvalidate/Model/PurgeCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,20 @@
*/
namespace Magento\CacheInvalidate\Model;

use Symfony\Component\Config\Definition\Exception\Exception;
use Zend\Uri\Uri;
use Zend\Http\Client\Adapter\Socket;
use Magento\Framework\Cache\InvalidateLogger;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\Config\ConfigOptionsListConstants;
use Magento\Framework\App\RequestInterface;

class PurgeCache
{
const HEADER_X_MAGENTO_TAGS_PATTERN = 'X-Magento-Tags-Pattern';

/**
* @var UriFactory
* @var \Magento\PageCache\Model\Cache\Server
*/
protected $uriFactory;
protected $cacheServer;

/**
* @var SocketFactory
* @var \Magento\CacheInvalidate\Model\SocketFactory
*/
protected $socketAdapterFactory;

Expand All @@ -30,75 +27,53 @@ class PurgeCache
*/
private $logger;

/**
* @var DeploymentConfig
*/
private $config;

/**
* @var RequestInterface
*/
private $request;

const DEFAULT_PORT = 80;

/**
* Constructor
*
* @param UriFactory $uriFactory
* @param SocketFactory $socketAdapterFactory
* @param \Magento\PageCache\Model\Cache\Server $cacheServer
* @param \Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory
* @param InvalidateLogger $logger
* @param Reader $configReader
* @param RequestInterface $request
*/
public function __construct(
UriFactory $uriFactory,
SocketFactory $socketAdapterFactory,
InvalidateLogger $logger,
DeploymentConfig $config,
RequestInterface $request
\Magento\PageCache\Model\Cache\Server $cacheServer,
\Magento\CacheInvalidate\Model\SocketFactory $socketAdapterFactory,
InvalidateLogger $logger
) {
$this->uriFactory = $uriFactory;
$this->cacheServer = $cacheServer;
$this->socketAdapterFactory = $socketAdapterFactory;
$this->logger = $logger;
$this->config = $config;
$this->request = $request;
}

/**
* Send curl purge request
* to invalidate cache by tags pattern
*
* @param string $tagsPattern
* @return void
* @return bool Return true if successful; otherwise return false
*/
public function sendPurgeRequest($tagsPattern)
{
$uri = $this->uriFactory->create();
$socketAdapter = $this->socketAdapterFactory->create();
$servers = $this->config->get(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS)
?: [['host' => $this->request->getHttpHost()]];
$headers = ['X-Magento-Tags-Pattern' => $tagsPattern];
$servers = $this->cacheServer->getUris();
$headers = [self::HEADER_X_MAGENTO_TAGS_PATTERN => $tagsPattern];
$socketAdapter->setOptions(['timeout' => 10]);
foreach ($servers as $server) {
$port = isset($server['port']) ? $server['port'] : self::DEFAULT_PORT;
$uri->setScheme('http')
->setHost($server['host'])
->setPort($port);
try {
$socketAdapter->connect($server['host'], $port);
$socketAdapter->connect($server->getHost(), $server->getPort());
$socketAdapter->write(
'PURGE',
$uri,
$server,
'1.1',
$headers
);
$socketAdapter->close();
} catch (Exception $e) {
} catch (\Exception $e) {
$this->logger->critical($e->getMessage(), compact('server', 'tagsPattern'));
return false;
}
}

$this->logger->execute(compact('servers', 'tagsPattern'));
return true;
}
}
18 changes: 0 additions & 18 deletions app/code/Magento/CacheInvalidate/Model/UriFactory.php

This file was deleted.

187 changes: 79 additions & 108 deletions app/code/Magento/CacheInvalidate/Test/Unit/Model/PurgeCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,144 +5,115 @@
*/
namespace Magento\CacheInvalidate\Test\Unit\Model;

use \Zend\Uri\UriFactory;

class PurgeCacheTest extends \PHPUnit_Framework_TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\CacheInvalidate\Model\PurgeCache */
/** @var \Magento\CacheInvalidate\Model\PurgeCache */
protected $model;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Uri\Uri */
protected $uriMock;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Zend\Http\Client\Adapter\Socket */
protected $socketAdapterMock;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Cache\InvalidateLogger */
protected $loggerMock;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\DeploymentConfig\Reader */
protected $configReaderMock;
/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\PageCache\Model\Cache\Server */
protected $cacheServer;

/** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\App\RequestInterface */
protected $requestMock;

/**
* Set up all mocks and data for test
*/
public function setUp()
{
$this->uriFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\UriFactory', [], [], '', false);
$this->uriMock = $this->getMock('\Zend\Uri\Uri', [], [], '', false);
$this->socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false);
$socketFactoryMock = $this->getMock('Magento\CacheInvalidate\Model\SocketFactory', [], [], '', false);
$this->socketAdapterMock = $this->getMock('\Zend\Http\Client\Adapter\Socket', [], [], '', false);
$this->configMock = $this->getMock('Magento\Framework\App\DeploymentConfig', [], [], '', false);
$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
$this->requestMock = $this->getMock('Magento\Framework\App\Request\Http', [], [], '', false);
$this->socketAdapterMock->expects($this->once())
->method('setOptions')
->with(['timeout' => 10]);
$this->uriFactoryMock->expects($this->once())
->method('create')
->willReturn($this->uriMock);
$this->socketFactoryMock->expects($this->once())
$socketFactoryMock->expects($this->once())
->method('create')
->willReturn($this->socketAdapterMock);
$this->model = new \Magento\CacheInvalidate\Model\PurgeCache(
$this->uriFactoryMock,
$this->socketFactoryMock,
$this->loggerMock,
$this->configMock,
$this->requestMock

$this->loggerMock = $this->getMock('Magento\Framework\Cache\InvalidateLogger', [], [], '', false);
$this->cacheServer = $this->getMock('Magento\PageCache\Model\Cache\Server', [], [], '', false);

$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
$this->model = $objectManager->getObject(
'Magento\CacheInvalidate\Model\PurgeCache',
[
'cacheServer' => $this->cacheServer,
'socketAdapterFactory' => $socketFactoryMock,
'logger' => $this->loggerMock,
]
);
}

public function testSendPurgeRequestEmptyConfig()
/**
* @param string[] $hosts
* @dataProvider sendPurgeRequestDataProvider
*/
public function testSendPurgeRequest($hosts)
{
$this->socketAdapterMock->expects($this->once())
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->once())
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn('');
$this->requestMock->expects($this->any())
->method('getHttpHost')
->willReturn('127.0.0.1');
$this->uriMock->expects($this->once())
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setHost')
->with('127.0.0.1')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setPort')
->with(\Magento\CacheInvalidate\Model\PurgeCache::DEFAULT_PORT);
$this->model->sendPurgeRequest('tags');
}
$uris = [];
foreach ($hosts as $host) {
$port = isset($host['port']) ? $host['port'] : \Magento\PageCache\Model\Cache\Server::DEFAULT_PORT;
$uris[] = UriFactory::factory('')->setHost($host['host'])
->setPort($port)
->setScheme('http');
}
$this->cacheServer->expects($this->once())
->method('getUris')
->willReturn($uris);

public function testSendPurgeRequestOneServer()
{
$this->socketAdapterMock->expects($this->once())
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->once())
$i = 1;
foreach ($uris as $uri) {
$this->socketAdapterMock->expects($this->at($i++))
->method('connect')
->with($uri->getHost(), $uri->getPort());
$this->socketAdapterMock->expects($this->at($i++))
->method('write')
->with('PURGE', $uri, '1.1', ['X-Magento-Tags-Pattern' => 'tags']);
$i++;
}
$this->socketAdapterMock->expects($this->exactly(count($uris)))
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn([['host' => '127.0.0.2', 'port' => 1234]]);
$this->uriMock->expects($this->once())
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setHost')
->with('127.0.0.2')
->willReturnSelf();
$this->uriMock->expects($this->once())
->method('setPort')
->with(1234);
$this->model->sendPurgeRequest('tags');

$this->loggerMock->expects($this->once())
->method('execute');

$this->assertTrue($this->model->sendPurgeRequest('tags'));
}

public function testSendPurgeRequestMultipleServers()
public function sendPurgeRequestDataProvider()
{
$this->socketAdapterMock->expects($this->exactly(2))
->method('write')
->with('PURGE', $this->uriMock, '1.1', $this->equalTo(['X-Magento-Tags-Pattern' => 'tags']));
$this->socketAdapterMock->expects($this->exactly(2))
->method('close');
$this->configMock->expects($this->once())
->method('get')
->willReturn(
return [
[
[['host' => '127.0.0.1', 'port' => 8080],]
],
[
[
['host' => '127.0.0.1', 'port' => 8080],
['host' => '127.0.0.2', 'port' => 1234]
['host' => '127.0.0.2', 'port' => 1234],
['host' => 'host']
]
);
$this->uriMock->expects($this->at(0))
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->at(1))
->method('setHost')
->with('127.0.0.1')
->willReturnSelf();
$this->uriMock->expects($this->at(2))
->method('setPort')
->with(8080);
$this->uriMock->expects($this->at(3))
->method('setScheme')
->with('http')
->willReturnSelf();
$this->uriMock->expects($this->at(4))
->method('setHost')
->with('127.0.0.2')
->willReturnSelf();
$this->uriMock->expects($this->at(5))
->method('setPort')
->with(1234);
$this->model->sendPurgeRequest('tags');
]
];
}

public function testSendPurgeRequestWithException()
{
$uris[] = UriFactory::factory('')->setHost('127.0.0.1')
->setPort(8080)
->setScheme('http');

$this->cacheServer->expects($this->once())
->method('getUris')
->willReturn($uris);
$this->socketAdapterMock->method('connect')
->willThrowException(new \Zend\Http\Client\Adapter\Exception\RuntimeException());
$this->loggerMock->expects($this->never())
->method('execute');
$this->loggerMock->expects($this->once())
->method('critical');

$this->assertFalse($this->model->sendPurgeRequest('tags'));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function getTimeInSecondsSinceCreation($consumerId)
$select = $connection->select()
->from($this->getMainTable())
->reset(\Magento\Framework\DB\Select::COLUMNS)
->columns('CURRENT_TIMESTAMP() - created_at')
->columns(new \Zend_Db_Expr('CURRENT_TIMESTAMP() - created_at'))
->where('entity_id = ?', $consumerId);

return $connection->fetchOne($select);
Expand Down
Loading

0 comments on commit 9449564

Please sign in to comment.