LifetimeTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Unit test for Magento\Cookie\Model\Config\Backend\Lifetime
  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\CookieLifetimeValidator;
  10. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  11. class LifetimeTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /** @var \PHPUnit_Framework_MockObject_MockObject | CookieLifetimeValidator */
  14. private $validatorMock;
  15. /** @var \PHPUnit_Framework_MockObject_MockObject | \Magento\Framework\Module\ModuleResource */
  16. private $resourceMock;
  17. /** @var \Magento\Cookie\Model\Config\Backend\Lifetime */
  18. private $model;
  19. protected function setUp()
  20. {
  21. $this->validatorMock = $this->getMockBuilder(
  22. \Magento\Framework\Session\Config\Validator\CookieLifetimeValidator::class
  23. )->disableOriginalConstructor()
  24. ->getMock();
  25. $this->resourceMock = $this->getMockBuilder(\Magento\Framework\Module\ModuleResource::class)
  26. ->disableOriginalConstructor('delete')
  27. ->getMock();
  28. $objectManager = new ObjectManager($this);
  29. $this->model = $objectManager->getObject(
  30. \Magento\Cookie\Model\Config\Backend\Lifetime::class,
  31. [
  32. 'configValidator' => $this->validatorMock,
  33. 'resource' => $this->resourceMock
  34. ]
  35. );
  36. }
  37. /**
  38. * Method is not publicly accessible, so it must be called through parent
  39. *
  40. * @expectedException \Magento\Framework\Exception\LocalizedException
  41. * @expectedExceptionMessage Invalid cookie lifetime: must be numeric
  42. */
  43. public function testBeforeSaveException()
  44. {
  45. $invalidCookieLifetime = 'invalid lifetime';
  46. $messages = ['must be numeric'];
  47. $this->validatorMock->expects($this->once())
  48. ->method('getMessages')
  49. ->willReturn($messages);
  50. $this->validatorMock->expects($this->once())
  51. ->method('isValid')
  52. ->with($invalidCookieLifetime)
  53. ->willReturn(false);
  54. // Test
  55. $this->model->setValue($invalidCookieLifetime)->beforeSave();
  56. }
  57. /**
  58. * Method is not publicly accessible, so it must be called through parent
  59. *
  60. * No assertions exist because the purpose of the test is to make sure that no
  61. * exception gets thrown
  62. */
  63. public function testBeforeSaveNoException()
  64. {
  65. $validCookieLifetime = 1;
  66. $this->validatorMock->expects($this->once())
  67. ->method('isValid')
  68. ->with($validCookieLifetime)
  69. ->willReturn(true);
  70. // Test
  71. $this->model->setValue($validCookieLifetime)->beforeSave();
  72. }
  73. /**
  74. * Method is not publicly accessible, so it must be called through parent
  75. *
  76. * No assertions exist because the purpose of the test is to make sure that no
  77. * exception gets thrown
  78. */
  79. public function testBeforeEmptyString()
  80. {
  81. $validCookieLifetime = '';
  82. $this->validatorMock->expects($this->never())
  83. ->method('isValid');
  84. // Test
  85. $this->model->setValue($validCookieLifetime)->beforeSave();
  86. }
  87. }