QueryResolverTest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Quote\Test\Unit\Model;
  7. use Magento\Framework\Serialize\SerializerInterface;
  8. class QueryResolverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Quote\Model\QueryResolver
  12. */
  13. private $quoteResolver;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. private $configMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $cacheMock;
  22. /**
  23. * @var SerializerInterface|\PHPUnit_Framework_MockObject_MockObject
  24. */
  25. private $serializer;
  26. protected function setUp()
  27. {
  28. $this->configMock = $this->createMock(\Magento\Framework\App\ResourceConnection\ConfigInterface::class);
  29. $this->cacheMock = $this->createMock(\Magento\Framework\Config\CacheInterface::class);
  30. $this->serializer = $this->getMockForAbstractClass(SerializerInterface::class);
  31. $this->quoteResolver = new \Magento\Quote\Model\QueryResolver(
  32. $this->configMock,
  33. $this->cacheMock,
  34. 'connection_config_cache',
  35. $this->serializer
  36. );
  37. }
  38. public function testIsSingleQueryWhenDataWereCached()
  39. {
  40. $serializedData = '{"checkout":true}';
  41. $data = ['checkout' => true];
  42. $this->cacheMock
  43. ->expects($this->once())
  44. ->method('load')
  45. ->with('connection_config_cache')
  46. ->willReturn($serializedData);
  47. $this->serializer->expects($this->once())
  48. ->method('unserialize')
  49. ->with($serializedData)
  50. ->willReturn($data);
  51. $this->assertTrue($this->quoteResolver->isSingleQuery());
  52. }
  53. /**
  54. * @param string $connectionName
  55. * @param bool $isSingleQuery
  56. *
  57. * @dataProvider isSingleQueryWhenDataNotCachedDataProvider
  58. */
  59. public function testIsSingleQueryWhenDataNotCached($connectionName, $isSingleQuery)
  60. {
  61. $data = ['checkout' => $isSingleQuery];
  62. $serializedData = '{"checkout":true}';
  63. $this->cacheMock
  64. ->expects($this->once())
  65. ->method('load')
  66. ->with('connection_config_cache')
  67. ->willReturn(false);
  68. $this->serializer->expects($this->never())
  69. ->method('unserialize');
  70. $this->configMock
  71. ->expects($this->once())
  72. ->method('getConnectionName')
  73. ->with('checkout_setup')
  74. ->willReturn($connectionName);
  75. $this->serializer->expects($this->once())
  76. ->method('serialize')
  77. ->with($data)
  78. ->willReturn($serializedData);
  79. $this->cacheMock
  80. ->expects($this->once())
  81. ->method('save')
  82. ->with($serializedData, 'connection_config_cache', []);
  83. $this->assertEquals($isSingleQuery, $this->quoteResolver->isSingleQuery());
  84. }
  85. /**
  86. * @return array
  87. */
  88. public function isSingleQueryWhenDataNotCachedDataProvider()
  89. {
  90. return [
  91. ['default', true],
  92. ['checkout', false],
  93. ];
  94. }
  95. }