Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SettingsTest.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Test\Bootstrap;
11 
12 class SettingsTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_object;
18 
22  protected $_fixtureDir;
23 
31  public function __construct($name = null, array $data = [], $dataName = '')
32  {
33  parent::__construct($name, $data, $dataName);
34  $this->_fixtureDir = realpath(__DIR__ . '/_files') . '/';
35  }
36 
37  protected function setUp()
38  {
39  $this->_object = new \Magento\TestFramework\Bootstrap\Settings(
40  $this->_fixtureDir,
41  [
42  'item_label' => 'Item Label',
43  'number_of_items' => 42,
44  'item_price' => 12.99,
45  'is_in_stock' => true,
46  'free_shipping' => 'enabled',
47  'zero_value' => '0',
48  'test_file' => 'metrics.php',
49  'all_xml_files' => '*.xml',
50  'all_xml_or_one_php_file' => '{*.xml,4.php}',
51  'one_xml_or_any_php_file' => '1.xml;?.php',
52  'config_file_with_dist' => '1.xml',
53  'config_file_no_dist' => '2.xml',
54  'no_config_file_dist' => '3.xml'
55  ]
56  );
57  }
58 
59  protected function tearDown()
60  {
61  $this->_object = null;
62  }
63 
69  {
70  new \Magento\TestFramework\Bootstrap\Settings('non_existing_dir', []);
71  }
72 
79  public function testGet($settingName, $defaultValue, $expectedResult)
80  {
81  $this->assertSame($expectedResult, $this->_object->get($settingName, $defaultValue));
82  }
83 
84  public function getDataProvider()
85  {
86  return [
87  'string type' => ['item_label', null, 'Item Label'],
88  'integer type' => ['number_of_items', null, 42],
89  'float type' => ['item_price', null, 12.99],
90  'boolean type' => ['is_in_stock', null, true],
91  'non-existing' => ['non_existing', null, null],
92  'zero string' => ['zero_value', '1', '0'],
93  'default value' => ['non_existing', 'default', 'default']
94  ];
95  }
96 
102  public function testGetAsBoolean($settingName, $expectedResult)
103  {
104  $this->assertSame($expectedResult, $this->_object->getAsBoolean($settingName));
105  }
106 
107  public function getAsBooleanDataProvider()
108  {
109  return [
110  'non-enabled string' => ['item_label', false],
111  'non-enabled boolean' => ['is_in_stock', false],
112  'enabled string' => ['free_shipping', true]
113  ];
114  }
115 
122  public function testGetAsFile($settingName, $defaultValue, $expectedResult)
123  {
124  $this->assertSame($expectedResult, $this->_object->getAsFile($settingName, $defaultValue));
125  }
126 
127  public function getAsFileDataProvider()
128  {
129  return [
130  'existing file' => ['test_file', '', "{$this->_fixtureDir}metrics.php"],
131  'zero value setting' => ['zero_value', 'default_should_be_ignored', "{$this->_fixtureDir}0"],
132  'empty default value' => ['non_existing_file', '', ''],
133  'zero default value' => ['non_existing_file', '0', "{$this->_fixtureDir}0"],
134  'default value' => ['non_existing_file', 'metrics.php', "{$this->_fixtureDir}metrics.php"]
135  ];
136  }
137 
143  public function testGetAsMatchingPaths($settingName, $expectedResult)
144  {
145  $actualResult = $this->_object->getAsMatchingPaths($settingName);
146  if (is_array($actualResult)) {
147  sort($actualResult);
148  }
149  $this->assertEquals($expectedResult, $actualResult);
150  }
151 
153  {
154  return [
155  'single pattern' => [
156  'all_xml_files',
157  ["{$this->_fixtureDir}1.xml", "{$this->_fixtureDir}2.xml"],
158  ],
159  'pattern with braces' => [
160  'all_xml_or_one_php_file',
161  ["{$this->_fixtureDir}1.xml", "{$this->_fixtureDir}2.xml", "{$this->_fixtureDir}4.php"],
162  ],
163  'multiple patterns' => [
164  'one_xml_or_any_php_file',
165  ["{$this->_fixtureDir}1.xml", "{$this->_fixtureDir}4.php"],
166  ],
167  'non-existing setting' => ['non_existing', []],
168  'setting with zero value' => ['zero_value', ["{$this->_fixtureDir}0"]]
169  ];
170  }
171 
177  public function testGetAsConfigFile($settingName, $expectedResult)
178  {
179  $actualResult = $this->_object->getAsConfigFile($settingName);
180  if (is_array($actualResult)) {
181  sort($actualResult);
182  }
183  $this->assertEquals($expectedResult, $actualResult);
184  }
185 
186  public function getAsConfigFileDataProvider()
187  {
188  return [
189  'config file & dist file' => ['config_file_with_dist', "{$this->_fixtureDir}1.xml"],
190  'config file & no dist file' => ['config_file_no_dist', "{$this->_fixtureDir}2.xml"],
191  'no config file & dist file' => ['no_config_file_dist', "{$this->_fixtureDir}3.xml.dist"]
192  ];
193  }
194 
200  public function testGetAsConfigFileException($settingName, $expectedExceptionMsg)
201  {
202  $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
203  $this->expectExceptionMessage((string)$expectedExceptionMsg);
204  $this->_object->getAsConfigFile($settingName);
205  }
206 
208  {
209  return [
210  'non-existing setting' => [
211  'non_existing',
212  __("Setting 'non_existing' specifies the non-existing file ''."),
213  ],
214  'non-existing file' => [
215  'item_label',
216  __("Setting 'item_label' specifies the non-existing file '%1Item Label.dist'.", $this->_fixtureDir),
217  ]
218  ];
219  }
220 }
testGetAsConfigFileException($settingName, $expectedExceptionMsg)
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
__()
Definition: __.php:13
testGetAsBoolean($settingName, $expectedResult)
__construct($name=null, array $data=[], $dataName='')
testGetAsFile($settingName, $defaultValue, $expectedResult)
testGetAsMatchingPaths($settingName, $expectedResult)
testGetAsConfigFile($settingName, $expectedResult)
testGet($settingName, $defaultValue, $expectedResult)
if(!isset($_GET['name'])) $name
Definition: log.php:14