DomainTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cookie\Test\Unit\Model\Config\Backend;
  7. use Magento\Framework\Exception\LocalizedException;
  8. use Magento\Framework\Session\Config\Validator\CookieDomainValidator;
  9. /**
  10. * Test \Magento\Cookie\Model\Config\Backend\Domain
  11. */
  12. class DomainTest extends \PHPUnit\Framework\TestCase
  13. {
  14. /** @var \Magento\Framework\Model\ResourceModel\AbstractResource | \PHPUnit_Framework_MockObject_MockObject */
  15. protected $resourceMock;
  16. /** @var \Magento\Cookie\Model\Config\Backend\Domain */
  17. protected $domain;
  18. /**
  19. * @var CookieDomainValidator | \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $validatorMock;
  22. protected function setUp()
  23. {
  24. $eventDispatcherMock = $this->createMock(\Magento\Framework\Event\Manager::class);
  25. $contextMock = $this->createMock(\Magento\Framework\Model\Context::class);
  26. $contextMock->expects(
  27. $this->any()
  28. )->method(
  29. 'getEventDispatcher'
  30. )->will(
  31. $this->returnValue($eventDispatcherMock)
  32. );
  33. $this->resourceMock = $this->createPartialMock(\Magento\Framework\Model\ResourceModel\AbstractResource::class, [
  34. '_construct',
  35. 'getConnection',
  36. 'getIdFieldName',
  37. 'beginTransaction',
  38. 'save',
  39. 'commit',
  40. 'addCommitCallback',
  41. 'rollBack',
  42. ]);
  43. $this->validatorMock = $this->getMockBuilder(
  44. \Magento\Framework\Session\Config\Validator\CookieDomainValidator::class
  45. )->disableOriginalConstructor()
  46. ->getMock();
  47. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  48. $this->domain = $helper->getObject(
  49. \Magento\Cookie\Model\Config\Backend\Domain::class,
  50. [
  51. 'context' => $contextMock,
  52. 'resource' => $this->resourceMock,
  53. 'configValidator' => $this->validatorMock,
  54. ]
  55. );
  56. }
  57. /**
  58. * @covers \Magento\Cookie\Model\Config\Backend\Domain::beforeSave
  59. * @dataProvider beforeSaveDataProvider
  60. *
  61. * @param string $value
  62. * @param bool $isValid
  63. * @param int $callNum
  64. * @param int $callGetMessages
  65. */
  66. public function testBeforeSave($value, $isValid, $callNum, $callGetMessages = 0)
  67. {
  68. $this->resourceMock->expects($this->any())->method('addCommitCallback')->will($this->returnSelf());
  69. $this->resourceMock->expects($this->any())->method('commit')->will($this->returnSelf());
  70. $this->resourceMock->expects($this->any())->method('rollBack')->will($this->returnSelf());
  71. $this->validatorMock->expects($this->exactly($callNum))
  72. ->method('isValid')
  73. ->will($this->returnValue($isValid));
  74. $this->validatorMock->expects($this->exactly($callGetMessages))
  75. ->method('getMessages')
  76. ->will($this->returnValue(['message']));
  77. $this->domain->setValue($value);
  78. try {
  79. $this->domain->beforeSave();
  80. if ($callGetMessages) {
  81. $this->fail('Failed to throw exception');
  82. }
  83. } catch (LocalizedException $e) {
  84. $this->assertEquals('Invalid domain name: message', $e->getMessage());
  85. }
  86. }
  87. /**
  88. * @return array
  89. */
  90. public function beforeSaveDataProvider()
  91. {
  92. return [
  93. 'not string' => [['array'], false, 1, 1],
  94. 'invalid hostname' => ['http://', false, 1, 1],
  95. 'valid hostname' => ['hostname.com', true, 1, 0],
  96. 'empty string' => ['', false, 0, 0],
  97. ];
  98. }
  99. }