123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516 |
- <?php
- /**
- * Test for \Magento\Integration\Model\IntegrationService
- *
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Integration\Test\Unit\Model;
- use Magento\Integration\Model\Integration;
- class IntegrationServiceTest extends \PHPUnit\Framework\TestCase
- {
- const VALUE_INTEGRATION_ID = 1;
- const VALUE_INTEGRATION_NAME = 'Integration Name';
- const VALUE_INTEGRATION_ANOTHER_NAME = 'Another Integration Name';
- const VALUE_INTEGRATION_EMAIL = 'test@magento.com';
- const VALUE_INTEGRATION_SETUP_BACKEND = 0;
- const VALUE_INTEGRATION_ENDPOINT = 'http://magento.ll/endpoint';
- const VALUE_INTEGRATION_CONSUMER_ID = 1;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- private $_integrationFactory;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- private $_integrationMock;
- /** @var \PHPUnit_Framework_MockObject_MockObject */
- private $_emptyIntegrationMock;
- /** @var \Magento\Integration\Model\IntegrationService */
- private $_service;
- /** @var array */
- private $_integrationData;
- protected function setUp()
- {
- $this->_integrationFactory = $this->getMockBuilder(\Magento\Integration\Model\IntegrationFactory::class)
- ->disableOriginalConstructor()
- ->setMethods(['create'])
- ->getMock();
- $this->_integrationMock = $this->getMockBuilder(
- \Magento\Integration\Model\Integration::class
- )->disableOriginalConstructor()->setMethods(
- [
- 'getData',
- 'getId',
- 'getName',
- 'getEmail',
- 'getEndpoint',
- 'load',
- 'loadByName',
- 'save',
- 'delete',
- '__wakeup',
- ]
- )->getMock();
- $this->_integrationData = [
- Integration::ID => self::VALUE_INTEGRATION_ID,
- Integration::NAME => self::VALUE_INTEGRATION_NAME,
- Integration::EMAIL => self::VALUE_INTEGRATION_EMAIL,
- Integration::EMAIL => self::VALUE_INTEGRATION_ENDPOINT,
- Integration::SETUP_TYPE => self::VALUE_INTEGRATION_SETUP_BACKEND,
- ];
- $this->_integrationFactory->expects(
- $this->any()
- )->method(
- 'create'
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $oauthConsumerHelper = $this->getMockBuilder(
- \Magento\Integration\Api\OauthServiceInterface::class
- )->disableOriginalConstructor()->getMock();
- $oauthConsumer = $this->getMockBuilder(
- \Magento\Integration\Model\Oauth\Consumer::class
- )->disableOriginalConstructor()->getMock();
- $oauthConsumerHelper->expects(
- $this->any()
- )->method(
- 'createConsumer'
- )->will(
- $this->returnValue($oauthConsumer)
- );
- $oauthConsumerHelper->expects($this->any())->method('loadConsumer')->will($this->returnValue($oauthConsumer));
- $this->_service = new \Magento\Integration\Model\IntegrationService(
- $this->_integrationFactory,
- $oauthConsumerHelper
- );
- $this->_emptyIntegrationMock = $this->getMockBuilder(
- \Magento\Integration\Model\Integration::class
- )->disableOriginalConstructor()->setMethods(
- [
- 'getData',
- 'getId',
- 'getName',
- 'getEmail',
- 'getEndpoint',
- 'load',
- 'loadByName',
- 'save',
- 'delete',
- '__wakeup',
- ]
- )->getMock();
- $this->_emptyIntegrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
- }
- public function testCreateSuccess()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_NAME,
- 'name'
- )->will(
- $this->returnValue($this->_emptyIntegrationMock)
- );
- $this->_integrationMock->expects($this->any())->method('save')->will($this->returnSelf());
- $this->_setValidIntegrationData();
- $resultData = $this->_service->create($this->_integrationData)->getData();
- $this->assertSame($this->_integrationData, $resultData);
- }
- /**
- * @expectedException \Magento\Framework\Exception\IntegrationException
- * @expectedExceptionMessage The integration with name "Integration Name" exists.
- */
- public function testCreateIntegrationAlreadyExistsException()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_NAME,
- 'name'
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
- $this->_service->create($this->_integrationData);
- }
- public function testUpdateSuccess()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $this->_integrationMock->expects(
- $this->at(0)
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_ID
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
- $this->_setValidIntegrationData();
- $integrationData = $this->_service->update($this->_integrationData)->getData();
- $this->assertEquals($this->_integrationData, $integrationData);
- }
- public function testUpdateSuccessNameChanged()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->will(
- $this->onConsecutiveCalls($this->_integrationMock, $this->_emptyIntegrationMock)
- );
- $this->_integrationMock->expects($this->once())->method('save')->will($this->returnSelf());
- $this->_setValidIntegrationData();
- $integrationData = [
- 'integration_id' => self::VALUE_INTEGRATION_ID,
- 'name' => self::VALUE_INTEGRATION_ANOTHER_NAME,
- 'email' => self::VALUE_INTEGRATION_EMAIL,
- 'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
- ];
- $this->_integrationMock->expects($this->any())->method('getData')->will($this->returnValue($integrationData));
- $updatedData = $this->_service->update($integrationData)->getData();
- $this->assertEquals($integrationData, $updatedData);
- }
- /**
- * @expectedException \Magento\Framework\Exception\IntegrationException
- * @expectedExceptionMessage The integration with name "Another Integration Name" exists.
- */
- public function testUpdateException()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->will(
- $this->onConsecutiveCalls($this->_integrationMock, $this->_getAnotherIntegrationMock())
- );
- $this->_integrationMock->expects($this->never())->method('save')->will($this->returnSelf());
- $this->_setValidIntegrationData();
- $integrationData = [
- 'integration_id' => self::VALUE_INTEGRATION_ID,
- 'name' => self::VALUE_INTEGRATION_ANOTHER_NAME,
- 'email' => self::VALUE_INTEGRATION_EMAIL,
- 'endpoint' => self::VALUE_INTEGRATION_ENDPOINT,
- ];
- $this->_service->update($integrationData);
- }
- public function testGet()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->_integrationMock->expects($this->never())->method('save');
- $integrationData = $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
- $this->assertEquals($this->_integrationData, $integrationData);
- }
- /**
- * @expectedException \Magento\Framework\Exception\IntegrationException
- * @expectedExceptionMessage The integration with ID "1" doesn't exist.
- */
- public function testGetException()
- {
- $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
- $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->_integrationMock->expects($this->never())->method('save');
- $this->_service->get(self::VALUE_INTEGRATION_ID)->getData();
- }
- public function testFindByName()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_NAME,
- 'name'
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
- $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
- }
- public function testFindByNameNotFound()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_NAME,
- 'name'
- )->will(
- $this->returnValue($this->_emptyIntegrationMock)
- );
- $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
- $integration = $this->_service->findByName(self::VALUE_INTEGRATION_NAME);
- $this->assertNull($integration->getData());
- }
- public function testDelete()
- {
- $this->_integrationMock->expects(
- $this->once()
- )->method(
- 'getId'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ID)
- );
- $this->_integrationMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_ID
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $this->_integrationMock->expects(
- $this->once()
- )->method(
- 'delete'
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $integrationData = $this->_service->delete(self::VALUE_INTEGRATION_ID);
- $this->assertEquals($this->_integrationData[Integration::ID], $integrationData[Integration::ID]);
- }
- /**
- * @expectedException \Magento\Framework\Exception\IntegrationException
- * @expectedExceptionMessage The integration with ID "1" doesn't exist.
- */
- public function testDeleteException()
- {
- $this->_integrationMock->expects($this->any())->method('getId')->will($this->returnValue(null));
- $this->_integrationMock->expects($this->once())->method('load')->will($this->returnSelf());
- $this->_integrationMock->expects($this->never())->method('delete');
- $this->_service->delete(self::VALUE_INTEGRATION_ID);
- }
- public function testFindByConsumerId()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getData'
- )->will(
- $this->returnValue($this->_integrationData)
- );
- $this->_integrationMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_CONSUMER_ID,
- 'consumer_id'
- )->will(
- $this->returnValue($this->_integrationMock)
- );
- $integration = $this->_service->findByConsumerId(self::VALUE_INTEGRATION_CONSUMER_ID);
- $this->assertEquals($this->_integrationData[Integration::NAME], $integration->getData()[Integration::NAME]);
- }
- public function testFindByConsumerIdNotFound()
- {
- $this->_emptyIntegrationMock->expects($this->any())->method('getData')->will($this->returnValue(null));
- $this->_integrationMock->expects(
- $this->once()
- )->method(
- 'load'
- )->with(
- self::VALUE_INTEGRATION_CONSUMER_ID,
- 'consumer_id'
- )->will(
- $this->returnValue($this->_emptyIntegrationMock)
- );
- $integration = $this->_service->findByConsumerId(1);
- $this->assertNull($integration->getData());
- }
- /**
- * Set valid integration data
- */
- private function _setValidIntegrationData()
- {
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getName'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_NAME)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getEmail'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
- );
- $this->_integrationMock->expects(
- $this->any()
- )->method(
- 'getEndpoint'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
- );
- }
- /**
- * Create mock integration
- *
- * @param string $name
- * @param int $integrationId
- * @return mixed
- */
- private function _getAnotherIntegrationMock(
- $name = self::VALUE_INTEGRATION_NAME,
- $integrationId = self::VALUE_INTEGRATION_ID
- ) {
- $integrationMock = $this->getMockBuilder(
- \Magento\Integration\Model\Integration::class
- )->disableOriginalConstructor()->setMethods(
- [
- 'getData',
- 'getId',
- 'getName',
- 'getEmail',
- 'getEndpoint',
- 'load',
- 'loadByName',
- 'save',
- 'delete',
- '__wakeup',
- ]
- )->getMock();
- $integrationMock->expects($this->any())->method('getId')->will($this->returnValue($integrationId));
- $integrationMock->expects($this->any())->method('getName')->will($this->returnValue($name));
- $integrationMock->expects(
- $this->any()
- )->method(
- 'getEmail'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_EMAIL)
- );
- $integrationMock->expects(
- $this->any()
- )->method(
- 'getEndpoint'
- )->will(
- $this->returnValue(self::VALUE_INTEGRATION_ENDPOINT)
- );
- return $integrationMock;
- }
- }
|