ConfigTest.php 2.9 KB

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