ServiceConfigTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\WebapiAsync\Test\Unit\Model;
  8. use Magento\Framework\Serialize\SerializerInterface;
  9. use Magento\Webapi\Model\Cache\Type\Webapi;
  10. use Magento\Webapi\Model\Config;
  11. use Magento\Webapi\Model\Config\Reader;
  12. class ServiceConfigTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /**
  15. * @var Config
  16. */
  17. private $config;
  18. /**
  19. * @var Webapi|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $webapiCacheMock;
  22. /**
  23. * @var Reader|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $configReaderMock;
  26. /**
  27. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $serializerMock;
  30. protected function setUp()
  31. {
  32. $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  33. $this->webapiCacheMock = $this->createMock(\Magento\Webapi\Model\Cache\Type\Webapi::class);
  34. $this->configReaderMock = $this->createMock(\Magento\Webapi\Model\Config\Reader::class);
  35. $this->serializerMock = $this->createMock(SerializerInterface::class);
  36. $this->config = $objectManager->getObject(
  37. Config::class,
  38. [
  39. 'cache' => $this->webapiCacheMock,
  40. 'configReader' => $this->configReaderMock,
  41. 'serializer' => $this->serializerMock
  42. ]
  43. );
  44. }
  45. public function testGetServices()
  46. {
  47. $data = ['foo' => 'bar'];
  48. $serializedData = 'serialized data';
  49. $this->webapiCacheMock->expects($this->once())
  50. ->method('load')
  51. ->with(Config::CACHE_ID)
  52. ->willReturn($serializedData);
  53. $this->serializerMock->expects($this->once())
  54. ->method('unserialize')
  55. ->with($serializedData)
  56. ->willReturn($data);
  57. $this->config->getServices();
  58. $this->assertEquals($data, $this->config->getServices());
  59. }
  60. public function testGetServicesNoCache()
  61. {
  62. $data = ['foo' => 'bar'];
  63. $serializedData = 'serialized data';
  64. $this->webapiCacheMock->expects($this->once())
  65. ->method('load')
  66. ->with(Config::CACHE_ID)
  67. ->willReturn(false);
  68. $this->serializerMock->expects($this->never())
  69. ->method('unserialize');
  70. $this->configReaderMock->expects($this->once())
  71. ->method('read')
  72. ->willReturn($data);
  73. $this->serializerMock->expects($this->once())
  74. ->method('serialize')
  75. ->with($data)
  76. ->willReturn($serializedData);
  77. $this->webapiCacheMock->expects($this->once())
  78. ->method('save')
  79. ->with(
  80. $serializedData,
  81. Config::CACHE_ID
  82. );
  83. $this->config->getServices();
  84. $this->assertEquals($data, $this->config->getServices());
  85. }
  86. }