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