Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PartnersTest.php
Go to the documentation of this file.
1 <?php
8 
9 class PartnersTest extends \PHPUnit\Framework\TestCase
10 {
14  private $partnersModelMock;
15 
16  private $returnPackages = '
17  {
18  "partners": {
19  "1": {
20  "url_page": "http://test.com",
21  "url_partner_page": "http://test.com",
22  "img": "http://test.com/img",
23  "title": "Test page",
24  "description": "Test page description"
25  },
26  "2": {
27  "url_page": "http://test.com",
28  "url_partner_page": "http://test.com",
29  "img": "http://test.com/img",
30  "title": "Test page",
31  "description": "Test page description"
32  }
33  }
34  }';
35 
36  protected function setUp()
37  {
38  $this->partnersModelMock = $this->getPartnersModelMock(
39  [
40  'getApiUrl',
41  'getCurlClient',
42  'getCache',
43  'getReferer'
44  ]
45  );
46  }
47 
51  protected $apiUrl = 'www.testpackages';
52 
56  public function testGetPartners()
57  {
58  $this->partnersModelMock->expects($this->once())
59  ->method('getApiUrl')
60  ->will($this->returnValue($this->apiUrl));
61 
62  $curlMock = $this->getCurlMock(['post', 'getBody', 'setOptions']);
63  $curlMock->expects($this->once())
64  ->method('post');
65  $curlMock->expects($this->once())
66  ->method('setOptions');
67  $curlMock->expects($this->once())
68  ->method('getBody')
69  ->will($this->returnValue($this->returnPackages));
70  $this->partnersModelMock->expects($this->exactly(3))
71  ->method('getCurlClient')
72  ->will($this->returnValue($curlMock));
73 
74  $cacheMock = $this->getCacheMock(['savePartnersToCache']);
75  $cacheMock->expects($this->once())
76  ->method('savePartnersToCache');
77  $this->partnersModelMock->expects($this->once())
78  ->method('getCache')
79  ->will($this->returnValue($cacheMock));
80  $this->partnersModelMock->expects($this->once())
81  ->method('getReferer');
82 
83  $this->partnersModelMock->getPartners();
84  }
85 
89  public function testGetPartnersException()
90  {
91  $this->partnersModelMock->expects($this->once())
92  ->method('getApiUrl')
93  ->will($this->returnValue($this->apiUrl));
94 
95  $curlMock = $this->getCurlMock(['post', 'getBody', 'setOptions']);
96  $curlMock->expects($this->once())
97  ->method('post');
98  $curlMock->expects($this->once())
99  ->method('getBody')
100  ->will($this->throwException(new \Exception));
101  $this->partnersModelMock->expects($this->exactly(3))
102  ->method('getCurlClient')
103  ->will($this->returnValue($curlMock));
104 
105  $cacheMock = $this->getCacheMock(['savePartnersToCache', 'loadPartnersFromCache']);
106  $cacheMock->expects($this->never())
107  ->method('savePartnersToCache');
108  $cacheMock->expects($this->once())
109  ->method('loadPartnersFromCache');
110  $this->partnersModelMock->expects($this->once())
111  ->method('getCache')
112  ->will($this->returnValue($cacheMock));
113  $this->partnersModelMock->expects($this->once())
114  ->method('getReferer');
115 
116  $this->partnersModelMock->getPartners();
117  }
118 
124  public function getPartnersBlockMock($methods = null)
125  {
126  return $this->createPartialMock(\Magento\Marketplace\Block\Partners::class, $methods);
127  }
128 
135  {
136  return $this->createPartialMock(\Magento\Marketplace\Model\Partners::class, $methods, []);
137  }
138 
144  public function getCurlMock($methods)
145  {
146  return $this->createPartialMock(\Magento\Framework\HTTP\Client\Curl::class, $methods, []);
147  }
148 
154  public function getCacheMock($methods)
155  {
156  return $this->createPartialMock(\Magento\Marketplace\Helper\Cache::class, $methods, []);
157  }
158 }
$methods
Definition: billing.phtml:71