Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VarnishPluginTest.php
Go to the documentation of this file.
1 <?php
7 
16 
20 class VarnishPluginTest extends \PHPUnit\Framework\TestCase
21 {
25  private $plugin;
26 
30  private $objectManagerHelper;
31 
35  private $configMock;
36 
40  private $versionMock;
41 
45  private $stateMock;
46 
50  private $frontControllerMock;
51 
55  private $responseMock;
56 
60  private $resultMock;
61 
62  protected function setUp()
63  {
64  $this->configMock = $this->getMockBuilder(Config::class)
65  ->disableOriginalConstructor()
66  ->getMock();
67  $this->versionMock = $this->getMockBuilder(Version::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $this->stateMock = $this->getMockBuilder(AppState::class)
71  ->disableOriginalConstructor()
72  ->getMock();
73  $this->frontControllerMock = $this->getMockBuilder(FrontControllerInterface::class)
74  ->getMockForAbstractClass();
75  $this->responseMock = $this->getMockBuilder(ResponseHttp::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $this->resultMock = $this->getMockBuilder(ResultInterface::class)
79  ->getMockForAbstractClass();
80 
81  $this->objectManagerHelper = new ObjectManagerHelper($this);
82  $this->plugin = $this->objectManagerHelper->getObject(
83  VarnishPlugin::class,
84  [
85  'config' => $this->configMock,
86  'version' => $this->versionMock,
87  'state' => $this->stateMock
88  ]
89  );
90  }
91 
98  public function testAfterDispatchReturnsCache($state, $countHeader)
99  {
100  $this->configMock->expects(static::once())
101  ->method('isEnabled')
102  ->willReturn(true);
103  $this->configMock->expects(static::once())
104  ->method('getType')
105  ->willReturn(Config::VARNISH);
106  $this->versionMock->expects(static::once())
107  ->method('process');
108  $this->stateMock->expects(static::once())
109  ->method('getMode')
110  ->willReturn($state);
111  $this->responseMock->expects(static::exactly($countHeader))
112  ->method('setHeader')
113  ->with('X-Magento-Debug');
114 
115  $this->assertSame(
116  $this->responseMock,
117  $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock)
118  );
119  }
120 
122  {
123  $this->configMock->expects(static::once())
124  ->method('isEnabled')
125  ->willReturn(true);
126  $this->configMock->expects(static::once())
127  ->method('getType')
128  ->willReturn(Config::VARNISH);
129  $this->versionMock->expects(static::never())
130  ->method('process');
131  $this->stateMock->expects(static::never())
132  ->method('getMode');
133  $this->resultMock->expects(static::never())
134  ->method('setHeader');
135 
136  $this->assertSame(
137  $this->resultMock,
138  $this->plugin->afterDispatch($this->frontControllerMock, $this->resultMock)
139  );
140  }
141 
142  public function testAfterDispatchDisabled()
143  {
144  $this->configMock->expects(static::any())
145  ->method('getType')
146  ->willReturn(null);
147  $this->versionMock->expects(static::never())
148  ->method('process');
149  $this->stateMock->expects(static::any())
150  ->method('getMode')
151  ->willReturn(AppState::MODE_DEVELOPER);
152  $this->responseMock->expects(static::never())
153  ->method('setHeader');
154 
155  $this->assertSame(
156  $this->responseMock,
157  $this->plugin->afterDispatch($this->frontControllerMock, $this->responseMock)
158  );
159  }
160 
164  public function afterDispatchDataProvider()
165  {
166  return [
167  'developer_mode' => [AppState::MODE_DEVELOPER, 1],
168  'production' => [AppState::MODE_PRODUCTION, 0]
169  ];
170  }
171 }