PathTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Unit test for Magento\Cookie\Model\Config\Backend\Path
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Cookie\Test\Unit\Model\Config\Backend;
  9. use Magento\Framework\Session\Config\Validator\CookiePathValidator;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. class PathTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \PHPUnit_Framework_MockObject_MockObject | CookiePathValidator */
  14. private $validatorMock;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Module\ModuleResource */
  16. private $resourceMock;
  17. /** @var \Magento\Cookie\Model\Config\Backend\Path */
  18. private $model;
  19. protected function setUp()
  20. {
  21. $this->validatorMock = $this->getMockBuilder(
  22. \Magento\Framework\Session\Config\Validator\CookiePathValidator::class
  23. )
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Module\ModuleResource::class)
  27. ->disableOriginalConstructor()
  28. ->getMock();
  29. $objectManager = new ObjectManager($this);
  30. $this->model = $objectManager->getObject(
  31. \Magento\Cookie\Model\Config\Backend\Path::class,
  32. [
  33. 'configValidator' => $this->validatorMock,
  34. 'resource' => $this->resourceMock
  35. ]
  36. );
  37. }
  38. /**
  39. * Method is not publicly accessible, so it must be called through parent
  40. *
  41. * @expectedException \Magento\Framework\Exception\LocalizedException
  42. * @expectedExceptionMessage Invalid cookie path
  43. */
  44. public function testBeforeSaveException()
  45. {
  46. $invalidCookiePath = 'invalid path';
  47. $this->validatorMock->expects($this->once())
  48. ->method('isValid')
  49. ->with($invalidCookiePath)
  50. ->willReturn(false);
  51. // Must throw exception
  52. $this->model->setValue($invalidCookiePath)->beforeSave();
  53. }
  54. /**
  55. * Method is not publicly accessible, so it must be called through parent
  56. *
  57. * No assertions exist because the purpose of the test is to make sure that no
  58. * exception gets thrown
  59. */
  60. public function testBeforeSaveNoException()
  61. {
  62. $validCookiePath = 1;
  63. $this->validatorMock->expects($this->once())
  64. ->method('isValid')
  65. ->with($validCookiePath)
  66. ->willReturn(true);
  67. $this->resourceMock->expects($this->any())->method('addCommitCallback')->willReturnSelf();
  68. // Must not throw exception
  69. $this->model->setValue($validCookiePath)->beforeSave();
  70. }
  71. /**
  72. * Method is not publicly accessible, so it must be called through parent
  73. *
  74. * Empty string should not be sent to validator
  75. */
  76. public function testBeforeSaveEmptyString()
  77. {
  78. $validCookiePath = '';
  79. $this->validatorMock->expects($this->never())
  80. ->method('isValid');
  81. $this->resourceMock->expects($this->any())->method('addCommitCallback')->willReturnSelf();
  82. // Must not throw exception
  83. $this->model->setValue($validCookiePath)->beforeSave();
  84. }
  85. }