|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace IntegerNet\AsyncVarnish\Test\Integration; |
| 5 | + |
| 6 | +use Magento\CacheInvalidate\Model\PurgeCache; |
| 7 | +use Magento\Framework\App\DeploymentConfig; |
| 8 | +use Magento\TestFramework\Helper\Bootstrap; |
| 9 | +use Magento\TestFramework\ObjectManager; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\Process\PhpExecutableFinder; |
| 12 | +use Symfony\Component\Process\Process; |
| 13 | + |
| 14 | +/** |
| 15 | + * @magentoAppIsolation enabled |
| 16 | + */ |
| 17 | +class PurgeCacheTest extends TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * Full path to varnish mock server |
| 21 | + */ |
| 22 | + private const MOCK_SERVER_DIR = __DIR__ . '/VarnishMock'; |
| 23 | + /** |
| 24 | + * Request log, as written by VarnishMock/server.php |
| 25 | + */ |
| 26 | + private const REQUEST_LOG_FILE = self::MOCK_SERVER_DIR . '/.requests.log'; |
| 27 | + /** |
| 28 | + * Mock server port as used in VarnishMock/server.php |
| 29 | + */ |
| 30 | + private const MOCK_SERVER_PORT = '8082'; |
| 31 | + /** |
| 32 | + * @var Process |
| 33 | + */ |
| 34 | + private $mockServerProcess; |
| 35 | + /** |
| 36 | + * @var PurgeCache |
| 37 | + */ |
| 38 | + private $purgeCache; |
| 39 | + |
| 40 | + protected function setUp() |
| 41 | + { |
| 42 | + $this->startMockServer(); |
| 43 | + $this->createRequestLog(); |
| 44 | + |
| 45 | + $this->configureVarnishHost(); |
| 46 | + $this->purgeCache = Bootstrap::getObjectManager()->get(PurgeCache::class); |
| 47 | + } |
| 48 | + |
| 49 | + private function configureVarnishHost() |
| 50 | + { |
| 51 | + /** @var ObjectManager $objectManager */ |
| 52 | + $objectManager = Bootstrap::getObjectManager(); |
| 53 | + $deploymentConfig = new DeploymentConfig( |
| 54 | + $objectManager->get(DeploymentConfig\Reader::class), |
| 55 | + ['http_cache_hosts' => [['host' => '127.0.0.1', 'port' => self::MOCK_SERVER_PORT]]] |
| 56 | + ); |
| 57 | + $objectManager->addSharedInstance( |
| 58 | + $deploymentConfig, |
| 59 | + DeploymentConfig::class |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + protected function tearDown() |
| 64 | + { |
| 65 | + $this->stopMockServer(); |
| 66 | + } |
| 67 | + |
| 68 | + public function testWebserver() |
| 69 | + { |
| 70 | + $this->assertEquals("OK\n", \file_get_contents('http://127.0.0.1:' . self::MOCK_SERVER_PORT . '/')); |
| 71 | + } |
| 72 | + |
| 73 | + public function testPurgeRequestIsSentToVarnish() |
| 74 | + { |
| 75 | + $tagsPattern = 'XXX|YYY|ZZZZ'; |
| 76 | + $result = $this->purgeCache->sendPurgeRequest($tagsPattern); |
| 77 | + $this->assertTrue($result); |
| 78 | + $this->assertEquals( |
| 79 | + [ |
| 80 | + [ |
| 81 | + 'method' => 'PURGE', |
| 82 | + 'headers' => ['Host' => ['127.0.0.1'], 'X-Magento-Tags-Pattern' => [$tagsPattern]], |
| 83 | + ], |
| 84 | + ], |
| 85 | + $this->getRequestsFromLog() |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | + private function startMockServer(): void |
| 90 | + { |
| 91 | + $objectManager = Bootstrap::getObjectManager(); |
| 92 | + /** @var PhpExecutableFinder $phpExecutableFinder */ |
| 93 | + $phpExecutableFinder = $objectManager->get(PhpExecutableFinder::class); |
| 94 | + $mockServerCmd = $phpExecutableFinder->find() . ' ' . self::MOCK_SERVER_DIR . '/server.php'; |
| 95 | + //the following needs Symfony Process >= 4.2.0 |
| 96 | +// $this->mockServerProcess = Process::fromShellCommandline($mockServerCmd); |
| 97 | + //so we use the old way to instantiate Process from string: |
| 98 | + $this->mockServerProcess = new Process($mockServerCmd); |
| 99 | + $this->mockServerProcess->start(); |
| 100 | + //the following needs Symfony Process >= 4.2.0 |
| 101 | +// $this->mockServerProcess->waitUntil( |
| 102 | +// function($output) { |
| 103 | +// return $output === 'Started'; |
| 104 | +// } |
| 105 | +// ); |
| 106 | + // so we wait a second instead: |
| 107 | + sleep(1); |
| 108 | + } |
| 109 | + |
| 110 | + private function stopMockServer(): void |
| 111 | + { |
| 112 | + // issue: this only kills the parent shell script, not the PHP process (Symfony Process 4.1) |
| 113 | +// $this->mockServerProcess->stop(); |
| 114 | + // so we implemented a kill switch in the server: |
| 115 | + $ch = \curl_init('http://127.0.0.1:8082/?kill=1'); |
| 116 | + \curl_exec($ch); |
| 117 | + } |
| 118 | + |
| 119 | + private function createRequestLog(): void |
| 120 | + { |
| 121 | + \file_put_contents(self::REQUEST_LOG_FILE, ''); |
| 122 | + \chmod(self::REQUEST_LOG_FILE, 0666); |
| 123 | + } |
| 124 | + |
| 125 | + private function getRequestsFromLog(): array |
| 126 | + { |
| 127 | + $requests = \array_map( |
| 128 | + function (string $line): array { |
| 129 | + return \json_decode($line, true); |
| 130 | + }, |
| 131 | + \file(self::REQUEST_LOG_FILE) |
| 132 | + ); |
| 133 | + return $requests; |
| 134 | + } |
| 135 | +} |
0 commit comments