Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove zend json from the test suite #10320

Merged

Conversation

dmanners
Copy link
Contributor

@dmanners dmanners commented Jul 25, 2017

Description

Since Zend_Json is at EOL we should replace the usage of \Magento\Framework\Serialize\Serializer\Json when required. In this PR I have removed the usage of Zend_Json from the test suite.

Fixed Issues (if relevant)

  1. Upgrade ZF components. Zend_Json #9236: Upgrade ZF components. Zend_Json

Manual testing scenarios

  1. Running the test suite causes no failures

Contribution checklist

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds on Travis CI are green)

dmanners added 3 commits July 25, 2017 07:33
 - replace with direct calls to json_decode and json_encode as these are test cases
 - Mark AbstractController to ignore CouplingBetweenObjects
 - Set the same json_decode default that Zend_Json uses
@@ -268,14 +269,21 @@ protected function getCookieMessages($messageType = null)
{
/** @var $cookieManager CookieManagerInterface */
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);

/** @var $jsonSerializer \Magento\Framework\Serialize\Serializer\Json */
$jsonSerializer = $this->_objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I was not sure if I should use the class \Magento\Framework\Serialize\Serializer\Json here or just json_* but I thought since this was using the cookie interface it should also use the serializer. Happy to make a change if it is thought best to do something else.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we suppress exception anyway here I would simplify implementation and got rid of try/catch at all. But as it is just testing code it does not really matter.

@ishakhsuvarov ishakhsuvarov self-assigned this Jul 25, 2017
@ishakhsuvarov ishakhsuvarov added this to the July 2017 milestone Jul 25, 2017
@@ -15,6 +15,7 @@

/**
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding SuppressWarnings, specially for Coupling, is generally considered a bad practice.
Did you add this as a result of tests execution? As it looks like that you have added same amount of dependencies as removed.
Usually in such cases refactoring has to be done to decouple objects

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ishakhsuvarov I added this as the static test limit got hit because of adding in https://github.com/magento/magento2/pull/10320/files#diff-22fb93f06b955235735842cbdd6c2ab3R274 which I am still not sure about since this is inside the test framework itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But at the same time you have decoupled this class from \Zend_Json and \Zend_Json_Exception. So to me it looks like that same coupling value should be in place after your changes.
Am I missing something?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure myself. https://travis-ci.org/dmanners/magento2/jobs/257261073 that was the failure. I wonder if PHPMD runs from a diff of classes changed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is executed from a diff, looks like it was already broken before.
I will look into the possible ways to reduce coupling for this class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, let me know if I can help out here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added some changes, which reduced coupling, in a way, which seems reasonable to me. Going to proceed with the merge now. Please let me know if it's ok from your perspective.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah those changes seem good to me still not 100% sure why that change fixes the static check, but I can live with not knowing that currently 👍

 - Attempt to reduce coupling between objects
     - Fixed unit test for the integration tests framework
$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->disableOriginalConstructor()
->getMock();
$this->serializerMock->expects($this->any())->method('unserialize')->willReturnCallback(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I love this approach for mocking methods

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to think that using real dependency in this case would be a better idea, however we usually stick to mocking all the stuff in the unit tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah when it comes to mocking or not mocking I am not so sure what is "best" but the callback here is a nice idea specifically for this mock.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such "mock" is practically equivalent to a real class 😉 I prefer tiny tests with small fixtures having mocks in form of returning plain values. Copy-pasting such callbacks to dozens of tests somewhat increases maintenance efforts.

@@ -268,14 +269,21 @@ protected function getCookieMessages($messageType = null)
{
/** @var $cookieManager CookieManagerInterface */
$cookieManager = $this->_objectManager->get(CookieManagerInterface::class);

/** @var $jsonSerializer \Magento\Framework\Serialize\Serializer\Json */
$jsonSerializer = $this->_objectManager->get(\Magento\Framework\Serialize\Serializer\Json::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we suppress exception anyway here I would simplify implementation and got rid of try/catch at all. But as it is just testing code it does not really matter.

$this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
->disableOriginalConstructor()
->getMock();
$this->serializerMock->expects($this->any())->method('unserialize')->willReturnCallback(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such "mock" is practically equivalent to a real class 😉 I prefer tiny tests with small fixtures having mocks in form of returning plain values. Copy-pasting such callbacks to dozens of tests somewhat increases maintenance efforts.

@magento-team magento-team merged commit 8cd3d3a into magento:develop Jul 25, 2017
@dmanners dmanners deleted the remove-zend-json-from-test-suite branch July 26, 2017 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants