CodeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GoogleOptimizer\Test\Unit\Helper;
  7. class CodeTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. protected $_codeModelMock;
  13. /**
  14. * @var \Magento\GoogleOptimizer\Helper\Code
  15. */
  16. protected $_helper;
  17. protected function setUp()
  18. {
  19. $this->_codeModelMock = $this->createMock(\Magento\GoogleOptimizer\Model\Code::class);
  20. $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  21. $this->_helper = $objectManagerHelper->getObject(
  22. \Magento\GoogleOptimizer\Helper\Code::class,
  23. ['code' => $this->_codeModelMock]
  24. );
  25. }
  26. public function testLoadingCodeForCategoryEntity()
  27. {
  28. $categoryMock = $this->createMock(\Magento\Catalog\Model\Category::class);
  29. $categoryId = 1;
  30. $storeId = 1;
  31. $categoryMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($categoryId));
  32. $categoryMock->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
  33. $this->_codeModelMock->expects(
  34. $this->once()
  35. )->method(
  36. 'loadByEntityIdAndType'
  37. )->with(
  38. $categoryId,
  39. \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_CATEGORY,
  40. $storeId
  41. );
  42. $this->assertEquals(
  43. $this->_codeModelMock,
  44. $this->_helper->getCodeObjectByEntity(
  45. $categoryMock,
  46. \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_CATEGORY
  47. )
  48. );
  49. }
  50. public function testLoadingCodeForProductEntity()
  51. {
  52. $productMock = $this->createMock(\Magento\Catalog\Model\Product::class);
  53. $categoryId = 1;
  54. $storeId = 1;
  55. $productMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($categoryId));
  56. $productMock->expects($this->once())->method('getStoreId')->will($this->returnValue($storeId));
  57. $this->_codeModelMock->expects(
  58. $this->once()
  59. )->method(
  60. 'loadByEntityIdAndType'
  61. )->with(
  62. $categoryId,
  63. \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PRODUCT,
  64. $storeId
  65. );
  66. $this->assertEquals(
  67. $this->_codeModelMock,
  68. $this->_helper->getCodeObjectByEntity(
  69. $productMock,
  70. \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PRODUCT
  71. )
  72. );
  73. }
  74. public function testLoadingCodeForPageEntity()
  75. {
  76. $pageMock = $this->createMock(\Magento\Cms\Model\Page::class);
  77. $categoryId = 1;
  78. $pageMock->expects($this->exactly(2))->method('getId')->will($this->returnValue($categoryId));
  79. $this->_codeModelMock->expects(
  80. $this->once()
  81. )->method(
  82. 'loadByEntityIdAndType'
  83. )->with(
  84. $categoryId,
  85. \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE
  86. );
  87. $this->assertEquals(
  88. $this->_codeModelMock,
  89. $this->_helper->getCodeObjectByEntity($pageMock, \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE)
  90. );
  91. }
  92. /**
  93. * @expectedException \InvalidArgumentException
  94. * @expectedExceptionMessage The model class is not valid
  95. */
  96. public function testExceptionNotValidEntityType()
  97. {
  98. $entity = $this->createMock(\Magento\Cms\Model\Block::class);
  99. $entityId = 1;
  100. $entity->expects($this->exactly(2))->method('getId')->will($this->returnValue($entityId));
  101. $this->_codeModelMock->expects($this->never())->method('loadByEntityIdAndType');
  102. $this->assertEquals(
  103. $this->_codeModelMock,
  104. $this->_helper->getCodeObjectByEntity($entity, \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE)
  105. );
  106. }
  107. /**
  108. * @expectedException \InvalidArgumentException
  109. * @expectedExceptionMessage The model is empty
  110. */
  111. public function testExceptionEmptyEntity()
  112. {
  113. $entity = $this->createMock(\Magento\Cms\Model\Block::class);
  114. $entityId = 0;
  115. $entity->expects($this->exactly(1))->method('getId')->will($this->returnValue($entityId));
  116. $this->_codeModelMock->expects($this->never())->method('loadByEntityIdAndType');
  117. $this->assertEquals(
  118. $this->_codeModelMock,
  119. $this->_helper->getCodeObjectByEntity($entity, \Magento\GoogleOptimizer\Model\Code::ENTITY_TYPE_PAGE)
  120. );
  121. }
  122. }