|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © 2013-2017 Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Framework\App\Test\Unit\Scope; |
| 7 | + |
| 8 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 9 | +use Magento\Framework\App\ScopeInterface; |
| 10 | +use Magento\Framework\App\ScopeResolverInterface; |
| 11 | +use Magento\Framework\App\ScopeResolverPool; |
| 12 | +use Magento\Framework\Exception\NoSuchEntityException; |
| 13 | +use Magento\Framework\App\Scope\Validator; |
| 14 | +use PHPUnit_Framework_MockObject_MockObject as MockObject; |
| 15 | + |
| 16 | +class ValidatorTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var Validator |
| 20 | + */ |
| 21 | + private $model; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var ScopeResolverPool|MockObject |
| 25 | + */ |
| 26 | + private $scopeResolverPoolMock; |
| 27 | + |
| 28 | + /** |
| 29 | + * @inheritdoc |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $this->scopeResolverPoolMock = $this->getMockBuilder(ScopeResolverPool::class) |
| 34 | + ->disableOriginalConstructor() |
| 35 | + ->getMock(); |
| 36 | + |
| 37 | + $this->model = new Validator( |
| 38 | + $this->scopeResolverPoolMock |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + public function testIsValid() |
| 43 | + { |
| 44 | + $scope = 'not_default_scope'; |
| 45 | + $scopeCode = 'not_exist_scope_code'; |
| 46 | + |
| 47 | + $scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class) |
| 48 | + ->getMockForAbstractClass(); |
| 49 | + $scopeObject = $this->getMockBuilder(ScopeInterface::class) |
| 50 | + ->getMockForAbstractClass(); |
| 51 | + $scopeResolver->expects($this->once()) |
| 52 | + ->method('getScope') |
| 53 | + ->with($scopeCode) |
| 54 | + ->willReturn($scopeObject); |
| 55 | + $this->scopeResolverPoolMock->expects($this->once()) |
| 56 | + ->method('get') |
| 57 | + ->with($scope) |
| 58 | + ->willReturn($scopeResolver); |
| 59 | + |
| 60 | + $this->assertTrue($this->model->isValid($scope, $scopeCode)); |
| 61 | + } |
| 62 | + |
| 63 | + public function testIsValidDefault() |
| 64 | + { |
| 65 | + $this->assertTrue($this->model->isValid(ScopeConfigInterface::SCOPE_TYPE_DEFAULT)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 70 | + * @expectedExceptionMessage The "default" scope can't include a scope code. Try again without entering a scope |
| 71 | + */ |
| 72 | + public function testNotEmptyScopeCodeForDefaultScope() |
| 73 | + { |
| 74 | + $this->model->isValid(ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 'some_code'); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 79 | + * @expectedExceptionMessage Enter a scope before proceeding. |
| 80 | + */ |
| 81 | + public function testEmptyScope() |
| 82 | + { |
| 83 | + $this->model->isValid('', 'some_code'); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 88 | + * @expectedExceptionMessage Enter a scope code before proceeding. |
| 89 | + */ |
| 90 | + public function testEmptyScopeCode() |
| 91 | + { |
| 92 | + $this->model->isValid('not_default_scope', ''); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 97 | + * @expectedExceptionMessage The scope code can include only lowercase letters (a-z), numbers (0-9) and underscores |
| 98 | + */ |
| 99 | + public function testWrongScopeCodeFormat() |
| 100 | + { |
| 101 | + $this->model->isValid('not_default_scope', '123'); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 106 | + * @expectedExceptionMessage The "not_default_scope" value doesn't exist. Enter another value. |
| 107 | + */ |
| 108 | + public function testScopeNotExist() |
| 109 | + { |
| 110 | + $scope = 'not_default_scope'; |
| 111 | + $this->scopeResolverPoolMock->expects($this->once()) |
| 112 | + ->method('get') |
| 113 | + ->with($scope) |
| 114 | + ->willThrowException(new \InvalidArgumentException()); |
| 115 | + |
| 116 | + $this->model->isValid($scope, 'scope_code'); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @expectedException \Magento\Framework\Exception\LocalizedException |
| 121 | + * @expectedExceptionMessage The "not_exist_scope_code" value doesn't exist. Enter another value. |
| 122 | + */ |
| 123 | + public function testScopeCodeNotExist() |
| 124 | + { |
| 125 | + $scope = 'not_default_scope'; |
| 126 | + $scopeCode = 'not_exist_scope_code'; |
| 127 | + |
| 128 | + $scopeResolver = $this->getMockBuilder(ScopeResolverInterface::class) |
| 129 | + ->getMockForAbstractClass(); |
| 130 | + $scopeResolver->expects($this->once()) |
| 131 | + ->method('getScope') |
| 132 | + ->with($scopeCode) |
| 133 | + ->willThrowException(new NoSuchEntityException()); |
| 134 | + $this->scopeResolverPoolMock->expects($this->once()) |
| 135 | + ->method('get') |
| 136 | + ->with($scope) |
| 137 | + ->willReturn($scopeResolver); |
| 138 | + |
| 139 | + $this->model->isValid($scope, $scopeCode); |
| 140 | + } |
| 141 | +} |
0 commit comments