Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
HttpContentProviderTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Psr\Log\LoggerInterface;
13 
17 class HttpContentProviderTest extends \PHPUnit\Framework\TestCase
18 {
22  private $httpContentProvider;
23 
27  private $loggerMock;
28 
32  private $urlBuilderMock;
33 
37  private $httpClientMock;
38 
39  public function setUp()
40  {
41  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
42  ->getMockForAbstractClass();
43  $this->urlBuilderMock = $this->getMockBuilder(UrlBuilder::class)
44  ->disableOriginalConstructor()
45  ->setMethods(['getUrl'])
46  ->getMock();
47  $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
48  ->getMockForAbstractClass();
49 
50  $objectManager = new ObjectManager($this);
51  $this->httpContentProvider = $objectManager->getObject(
52  HttpContentProvider::class,
53  [
54  'httpClient' => $this->httpClientMock,
55  'urlBuilder' => $this->urlBuilderMock,
56  'logger' => $this->loggerMock
57  ]
58  );
59  }
60 
61  public function testGetContentSuccess()
62  {
63  $version = '2.3.0';
64  $edition = 'Community';
65  $locale = 'fr_FR';
66  $url = 'https://content.url.example/'. $version . '/' . $edition . '/' . $locale . '.json';
67  $response = '{"return":"success"}';
68 
69  $this->urlBuilderMock->expects($this->any())
70  ->method('getUrl')
71  ->willReturn($url);
72  $this->httpClientMock->expects($this->once())
73  ->method('get')
74  ->with($url);
75  $this->httpClientMock->expects($this->once())
76  ->method('getBody')
77  ->willReturn($response);
78  $this->httpClientMock->expects($this->once())
79  ->method('getStatus')
80  ->willReturn(200);
81  $this->loggerMock->expects($this->never())
82  ->method('warning');
83 
84  $this->assertEquals($response, $this->httpContentProvider->getContent($version, $edition, $locale));
85  }
86 
87  public function testGetContentFailure()
88  {
89  $version = '2.3.5';
90  $edition = 'Community';
91  $locale = 'fr_FR';
92  $url = 'https://content.url.example/'. $version . '/' . $edition . '/' . $locale . '.json';
93 
94  $this->urlBuilderMock->expects($this->any())
95  ->method('getUrl')
96  ->with($version, $edition, $locale)
97  ->willReturn($url);
98  $this->httpClientMock->expects($this->once())
99  ->method('get')
100  ->with($url)
101  ->will($this->throwException(new \Exception));
102  $this->httpClientMock->expects($this->never())->method('getBody');
103  $this->loggerMock->expects($this->once())
104  ->method('warning');
105 
106  $this->assertFalse($this->httpContentProvider->getContent($version, $edition, $locale));
107  }
108 
110  {
111  $version = '2.3.1';
112  $edition = 'Community';
113  $locale = 'fr_FR';
114  $urlLocale = 'https://content.url.example/'. $version . '/' . $edition . '/' . $locale . '.json';
115  $urlDefaultLocale = 'https://content.url.example/'. $version . '/' . $edition . '/en_US.json';
116  $response = '{"return":"default-locale"}';
117 
118  $this->urlBuilderMock->expects($this->exactly(2))
119  ->method('getUrl')
120  ->withConsecutive(
121  [$version, $edition, $locale],
122  [$version, $edition, 'en_US']
123  )
124  ->willReturnOnConsecutiveCalls($urlLocale, $urlDefaultLocale);
125  $this->httpClientMock->expects($this->exactly(2))
126  ->method('get')
127  ->withConsecutive([$urlLocale], [$urlDefaultLocale]);
128  $this->httpClientMock->expects($this->exactly(2))
129  ->method('getBody')
130  ->willReturnOnConsecutiveCalls('', $response);
131  $this->httpClientMock->expects($this->exactly(2))
132  ->method('getStatus')
133  ->willReturnOnConsecutiveCalls(404, 200);
134  $this->loggerMock->expects($this->never())
135  ->method('warning');
136 
137  $this->assertEquals($response, $this->httpContentProvider->getContent($version, $edition, $locale));
138  }
139 
148  {
149  $urlLocale = 'https://content.url.example/'. $version . '/' . $edition . '/' . $locale . '.json';
150  $urlDefaultLocale = 'https://content.url.example/'. $version . '/' . $edition . '/en_US.json';
151  $urlDefault = 'https://content.url.example/' . $version . '/default.json';
152 
153  $this->urlBuilderMock->expects($this->exactly(3))
154  ->method('getUrl')
155  ->withConsecutive(
156  [$version, $edition, $locale],
157  [$version, $edition, 'en_US'],
158  [$version, '', 'default']
159  )
160  ->willReturnOnConsecutiveCalls($urlLocale, $urlDefaultLocale, $urlDefault);
161  $this->httpClientMock->expects($this->exactly(3))
162  ->method('get')
163  ->withConsecutive([$urlLocale], [$urlDefaultLocale], [$urlDefault]);
164  $this->httpClientMock->expects($this->exactly(3))
165  ->method('getBody')
166  ->willReturnOnConsecutiveCalls('', '', $response);
167  $this->httpClientMock->expects($this->exactly(3))
168  ->method('getStatus')
169  ->willReturnOnConsecutiveCalls(404, 404, 200);
170  $this->loggerMock->expects($this->never())
171  ->method('warning');
172 
173  $this->assertEquals($response, $this->httpContentProvider->getContent($version, $edition, $locale));
174  }
175 
180  {
181  return [
182  'default-fr_FR' => [
183  '2.3.0',
184  'Community',
185  'fr_FR',
186  '{"return":"default-fr_FR"}'
187  ],
188  'default-en_US' => [
189  '2.3.0',
190  'Community',
191  'en_US',
192  '{"return":"default-en_US"}'
193  ],
194  'empty-fr_FR' => [
195  '2.3.0',
196  'Community',
197  'fr_FR',
198  '{"return":"empty-fr_FR"}'
199  ],
200  'empty-en_US' => [
201  '2.3.0',
202  'Community',
203  'en_US',
204  '{"return":"empty-en_US"}'
205  ]
206  ];
207  }
208 }
$response
Definition: 404.php:11
$objectManager
Definition: bootstrap.php:17
foreach(array_keys($composerData['require']) as $requiredPackage) if(empty($edition)) if(!empty($opts['edition'])) $edition