Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PackagesAuthTest.php
Go to the documentation of this file.
1 <?php
8 
9 use \Magento\Setup\Model\PackagesAuth;
10 
14 class PackagesAuthTest extends \PHPUnit\Framework\TestCase
15 {
19  private $curl;
20 
24  private $filesystem;
25 
29  private $packagesAuth;
30 
32  private $serializerMock;
33 
34  public function setUp()
35  {
36  $zendServiceLocator = $this->createMock(\Zend\ServiceManager\ServiceLocatorInterface::class);
37  $zendServiceLocator
38  ->expects($this->any())
39  ->method('get')
40  ->with('config')
41  ->willReturn([
42  'marketplace' => [
43  'check_credentials_url' => 'some_url'
44  ]
45  ]);
46  $this->curl = $this->createMock(\Magento\Framework\HTTP\Client\Curl::class, [], [], '', false);
47  $this->filesystem = $this->createMock(\Magento\Framework\Filesystem::class, [], [], '', false);
48  $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
49  ->getMock();
50  $this->serializerMock->expects($this->any())
51  ->method('serialize')
52  ->willReturnCallback(
53  function ($serializedData) {
54  return json_encode($serializedData);
55  }
56  );
57  $this->packagesAuth = new PackagesAuth(
58  $zendServiceLocator,
59  $this->curl,
60  $this->filesystem,
61  $this->serializerMock
62  );
63  }
64 
66  {
67  $this->curl->expects($this->once())->method('setCredentials')->with('username', 'password');
68  $this->curl->expects($this->once())->method('getStatus');
69  $expectedValue = '{"success":false,"message":"Bad credentials"}';
70  $returnValue = $this->packagesAuth->checkCredentials('username', 'password');
71  $this->assertSame($expectedValue, $returnValue);
72  }
73 
74  public function testCheckCredentials()
75  {
76  $this->curl->expects($this->once())->method('setCredentials')->with('username', 'password');
77  $this->curl->expects($this->once())->method('getStatus')->willReturn(200);
78  $this->curl->expects($this->once())->method('getBody')->willReturn("{'someJson'}");
79  $directory = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
80  $this->filesystem->expects($this->once())->method('getDirectoryWrite')->will($this->returnValue($directory));
81  $directory->expects($this->once())
82  ->method('writeFile')
83  ->with(PackagesAuth::PATH_TO_PACKAGES_FILE, "{'someJson'}");
84  $expectedValue = '{"success":true}';
85  $returnValue = $this->packagesAuth->checkCredentials('username', 'password');
86  $this->assertSame($expectedValue, $returnValue);
87  }
88 
90  {
91  $this->curl->expects($this->once())->method('setCredentials')->with('username', 'password');
92  $this->curl->expects($this->once())
93  ->method('getStatus')
94  ->will($this->throwException(new \Exception("Test error")));
95  $this->curl->expects($this->never())->method('getBody')->willReturn("{'someJson}");
96 
97  $expectedValue = '{"success":false,"message":"Test error"}';
98  $returnValue = $this->packagesAuth->checkCredentials('username', 'password');
99  $this->assertSame($expectedValue, $returnValue);
100  }
101 
102  public function testRemoveCredentials()
103  {
104  $directoryWrite = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
105  $directoryRead = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\ReadInterface::class);
106  $this->filesystem->expects($this->once())->method('getDirectoryRead')->will($this->returnValue($directoryRead));
107  $this->filesystem->expects($this->once())
108  ->method('getDirectoryWrite')
109  ->will($this->returnValue($directoryWrite));
110  $directoryWrite->expects($this->once())->method('isExist')->willReturn(true);
111  $directoryWrite->expects($this->once())->method('isReadable')->willReturn(true);
112  $directoryWrite->expects($this->once())->method('delete')->willReturn(true);
113  $directoryRead->expects($this->once())->method('isExist')->willReturn(true);
114  $directoryRead->expects($this->once())->method('isReadable')->willReturn(true);
115  $directoryRead->expects($this->once())
116  ->method('ReadFile')
117  ->willReturn('{"http-basic":{"some_url":{"username":"somename","password":"somepassword"}}}');
118 
119  $this->assertTrue($this->packagesAuth->removeCredentials());
120  }
121 
122  public function testSaveAuthJson()
123  {
124  $directoryWrite = $this->getMockForAbstractClass(\Magento\Framework\Filesystem\Directory\WriteInterface::class);
125  $this->filesystem->expects($this->once())
126  ->method('getDirectoryWrite')
127  ->will($this->returnValue($directoryWrite));
128  $directoryWrite->expects($this->once())->method('writeFile')->willReturn(true);
129 
130  $this->assertTrue($this->packagesAuth->saveAuthJson("testusername", "testpassword"));
131  }
132 }