ScopeValidatorTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Test\Unit\Model;
  7. use Magento\Framework\App\ScopeResolverPool;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Framework\Phrase;
  10. use Magento\Store\Model\ScopeValidator;
  11. class ScopeValidatorTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var ScopeValidator
  15. */
  16. protected $model;
  17. /**
  18. * @var ScopeResolverPool|\PHPUnit_Framework_MockObject_MockObject
  19. */
  20. protected $scopeResolverPool;
  21. protected function setUp()
  22. {
  23. $this->scopeResolverPool = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverPool::class)
  24. ->disableOriginalConstructor()
  25. ->getMock();
  26. $this->model = new ScopeValidator(
  27. $this->scopeResolverPool
  28. );
  29. }
  30. public function testScopeDefault()
  31. {
  32. $scope = 'default';
  33. $scopeId = 0;
  34. $this->assertTrue($this->model->isValidScope($scope, $scopeId));
  35. }
  36. public function testInvalidScope()
  37. {
  38. $scope = 'websites';
  39. $scopeId = 1;
  40. $scopeObject = $this->getMockBuilder(\Magento\Framework\App\ScopeInterface::class)
  41. ->getMockForAbstractClass();
  42. $scopeObject->expects($this->once())
  43. ->method('getId')
  44. ->willReturn(false);
  45. $scopeResolver = $this->getMockBuilder(\Magento\Framework\App\ScopeResolverInterface::class)
  46. ->getMockForAbstractClass();
  47. $scopeResolver->expects($this->once())
  48. ->method('getScope')
  49. ->with($scopeId)
  50. ->willReturn($scopeObject);
  51. $this->scopeResolverPool->expects($this->once())
  52. ->method('get')
  53. ->with($scope)
  54. ->willReturn($scopeResolver);
  55. $this->assertFalse($this->model->isValidScope($scope, $scopeId));
  56. }
  57. public function testInvalidScopeInvalidArgumentException()
  58. {
  59. $scope = 'websites';
  60. $scopeId = 1;
  61. $this->scopeResolverPool->expects($this->once())
  62. ->method('get')
  63. ->with($scope)
  64. ->willThrowException(new \InvalidArgumentException());
  65. $this->assertFalse($this->model->isValidScope($scope, $scopeId));
  66. }
  67. public function testInvalidScopeNoSuchEntityException()
  68. {
  69. $scope = 'websites';
  70. $scopeId = 1;
  71. $this->scopeResolverPool->expects($this->once())
  72. ->method('get')
  73. ->with($scope)
  74. ->willThrowException(new NoSuchEntityException(new Phrase('no such entity exception')));
  75. $this->assertFalse($this->model->isValidScope($scope, $scopeId));
  76. }
  77. }