Skip to content

Commit

Permalink
Merge pull request #1365 from magento-engcom/develop-prs
Browse files Browse the repository at this point in the history
[EngCom] Public Pull Requests
 - MAGETWO-71060 Remove zend json from package info #10340
 - MAGETWO-71059 Remove Zend_Json from setup #10339
  • Loading branch information
ishakhsuvarov authored Jul 27, 2017
2 parents a7f2db6 + 0b410fb commit 27db72c
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
26 changes: 18 additions & 8 deletions lib/internal/Magento/Framework/Module/PackageInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,32 @@ class PackageInfo
protected $nonExistingDependencies = [];

/**
* Constructor
*
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @param Dir\Reader $reader
* @param ComponentRegistrar $componentRegistrar
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(Dir\Reader $reader, ComponentRegistrar $componentRegistrar)
{
public function __construct(
Dir\Reader $reader,
ComponentRegistrar $componentRegistrar,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->reader = $reader;
$this->componentRegistrar = $componentRegistrar;
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
* Load the packages information
*
* @return void
* @throws \Zend_Json_Exception
* @throws \InvalidArgumentException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
private function load()
Expand All @@ -85,9 +95,9 @@ private function load()
$key = $moduleDir . '/composer.json';
if (isset($jsonData[$key]) && $jsonData[$key]) {
try {
$packageData = \Zend_Json::decode($jsonData[$key]);
} catch (\Zend_Json_Exception $e) {
throw new \Zend_Json_Exception(
$packageData = $this->serializer->unserialize($jsonData[$key]);
} catch (\InvalidArgumentException $e) {
throw new \InvalidArgumentException(
sprintf(
"%s composer.json error: %s",
$moduleName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,21 @@ protected function setUp()
->method('getComposerJsonFiles')
->will($this->returnValue($fileIteratorMock));

$this->packageInfo = new PackageInfo($this->reader, $this->componentRegistrar);
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->getMock();

$this->serializerMock->expects($this->any())
->method('unserialize')
->willReturnCallback(
function ($serializedData) {
return json_decode($serializedData, true);
}
);
$this->packageInfo = new PackageInfo(
$this->reader,
$this->componentRegistrar,
$this->serializerMock
);
}

public function testGetModuleName()
Expand Down
20 changes: 16 additions & 4 deletions setup/src/Magento/Setup/Model/PackagesAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,29 @@ class PackagesAuth
*/
private $filesystem;

/**
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @param \Zend\ServiceManager\ServiceLocatorInterface $serviceLocator
* @param \Magento\Framework\HTTP\Client\Curl $curl
* @param \Magento\Framework\Filesystem $filesystem
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @throws \RuntimeException
*/
public function __construct(
\Zend\ServiceManager\ServiceLocatorInterface $serviceLocator,
\Magento\Framework\HTTP\Client\Curl $curl,
\Magento\Framework\Filesystem $filesystem
\Magento\Framework\Filesystem $filesystem,
\Magento\Framework\Serialize\Serializer\Json $serializer = null
) {
$this->serviceLocator = $serviceLocator;
$this->curlClient = $curl;
$this->filesystem = $filesystem;
$this->serializer = $serializer?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
}

/**
Expand All @@ -85,9 +95,11 @@ public function getCredentialBaseUrl()
* @param string $token
* @param string $secretKey
* @return string
* @throws \InvalidArgumentException
*/
public function checkCredentials($token, $secretKey)
{
$response = ['success' => true];
$serviceUrl = $this->getPackagesJsonUrl();
$this->curlClient->setCredentials($token, $secretKey);
try {
Expand All @@ -96,13 +108,13 @@ public function checkCredentials($token, $secretKey)
$packagesInfo = $this->curlClient->getBody();
$directory = $this->filesystem->getDirectoryWrite(DirectoryList::COMPOSER_HOME);
$directory->writeFile(self::PATH_TO_PACKAGES_FILE, $packagesInfo);
return \Zend_Json::encode(['success' => true]);
} else {
return \Zend_Json::encode(['success' => false, 'message' => 'Bad credentials']);
$response = ['success' => false, 'message' => 'Bad credentials'];
}
} catch (\Exception $e) {
return \Zend_Json::encode(['success' => false, 'message' => $e->getMessage()]);
$response = ['success' => false, 'message' => $e->getMessage()];
}
return $this->serializer->serialize($response);
}

/**
Expand Down
19 changes: 18 additions & 1 deletion setup/src/Magento/Setup/Test/Unit/Model/PackagesAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class PackagesAuthTest extends \PHPUnit_Framework_TestCase
*/
private $packagesAuth;

/** @var \Magento\Framework\Serialize\Serializer\Json|\PHPUnit_Framework_MockObject_MockObject */
private $serializerMock;

public function setUp()
{
$zendServiceLocator = $this->getMock(\Zend\ServiceManager\ServiceLocatorInterface::class, [], [], '', false);
Expand All @@ -42,7 +45,21 @@ public function setUp()
]);
$this->curl = $this->getMock(\Magento\Framework\HTTP\Client\Curl::class, [], [], '', false);
$this->filesystem = $this->getMock(\Magento\Framework\Filesystem::class, [], [], '', false);
$this->packagesAuth = new PackagesAuth($zendServiceLocator, $this->curl, $this->filesystem);
$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()
Expand Down

0 comments on commit 27db72c

Please sign in to comment.