Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
7 
9 
13 class DataTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $_filesystem;
19 
23  protected $helper;
24 
28  protected $configMock;
29 
33  protected $factoryMock;
34 
35  protected function setUp()
36  {
37  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
38  $className = \Magento\Captcha\Helper\Data::class;
39  $arguments = $objectManagerHelper->getConstructArguments($className);
41  $context = $arguments['context'];
42  $this->configMock = $context->getScopeConfig();
43  $this->_filesystem = $arguments['filesystem'];
44  $storeManager = $arguments['storeManager'];
45  $storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($this->_getWebsiteStub()));
46  $storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->_getStoreStub()));
47  $this->factoryMock = $arguments['factory'];
48  $this->helper = $objectManagerHelper->getObject($className, $arguments);
49  }
50 
54  public function testGetCaptcha()
55  {
56  $this->configMock->expects(
57  $this->once()
58  )->method(
59  'getValue'
60  )->with(
61  'customer/captcha/type'
62  )->will(
63  $this->returnValue('zend')
64  );
65 
66  $this->factoryMock->expects(
67  $this->once()
68  )->method(
69  'create'
70  )->with(
71  $this->equalTo('Zend')
72  )->will(
73  $this->returnValue(
74  new \Magento\Captcha\Model\DefaultModel(
75  $this->createMock(\Magento\Framework\Session\SessionManager::class),
76  $this->createMock(\Magento\Captcha\Helper\Data::class),
77  $this->createPartialMock(\Magento\Captcha\Model\ResourceModel\LogFactory::class, ['create']),
78  'user_create'
79  )
80  )
81  );
82 
83  $this->assertInstanceOf(\Magento\Captcha\Model\DefaultModel::class, $this->helper->getCaptcha('user_create'));
84  }
85 
89  public function testGetConfigNode()
90  {
91  $this->configMock->expects(
92  $this->once()
93  )->method(
94  'getValue'
95  )->with(
96  'customer/captcha/enable',
97  \Magento\Store\Model\ScopeInterface::SCOPE_STORE
98  )->will(
99  $this->returnValue('1')
100  );
101 
102  $this->helper->getConfig('enable');
103  }
104 
105  public function testGetFonts()
106  {
107  $fontPath = 'path/to/fixture.ttf';
108  $expectedFontPath = 'lib/' . $fontPath;
109 
110  $libDirMock = $this->createMock(\Magento\Framework\Filesystem\Directory\Read::class);
111  $libDirMock->expects($this->once())
112  ->method('getAbsolutePath')
113  ->with($fontPath)
114  ->will($this->returnValue($expectedFontPath));
115  $this->_filesystem->expects($this->once())
116  ->method('getDirectoryRead')
118  ->will($this->returnValue($libDirMock));
119 
120  $configData = ['font_code' => ['label' => 'Label', 'path' => $fontPath]];
121 
122  $this->configMock->expects(
123  $this->any()
124  )->method(
125  'getValue'
126  )->with(
127  'captcha/fonts',
128  'default'
129  )->will(
130  $this->returnValue($configData)
131  );
132 
133  $fonts = $this->helper->getFonts();
134  $this->assertArrayHasKey('font_code', $fonts);
135  // fixture
136  $this->assertArrayHasKey('label', $fonts['font_code']);
137  $this->assertArrayHasKey('path', $fonts['font_code']);
138  $this->assertEquals('Label', $fonts['font_code']['label']);
139  $this->assertEquals($expectedFontPath, $fonts['font_code']['path']);
140  }
141 
146  public function testGetImgDir()
147  {
148  $dirWriteMock = $this->createPartialMock(
149  \Magento\Framework\Filesystem\Directory\Write::class,
150  ['changePermissions', 'create', 'getAbsolutePath']
151  );
152 
153  $this->_filesystem->expects(
154  $this->once()
155  )->method(
156  'getDirectoryWrite'
157  )->with(
159  )->will(
160  $this->returnValue($dirWriteMock)
161  );
162 
163  $dirWriteMock->expects(
164  $this->once()
165  )->method(
166  'getAbsolutePath'
167  )->with(
168  '/captcha/base'
169  )->will(
170  $this->returnValue(TESTS_TEMP_DIR . '/captcha/base')
171  );
172 
173  $this->assertFileNotExists(TESTS_TEMP_DIR . '/captcha');
174  $result = $this->helper->getImgDir();
175  $this->assertStringStartsWith(TESTS_TEMP_DIR, $result);
176  $this->assertStringEndsWith('captcha/base/', $result);
177  }
178 
183  public function testGetImgUrl()
184  {
185  $this->assertEquals($this->helper->getImgUrl(), 'http://localhost/pub/media/captcha/base/');
186  }
187 
193  protected function _getWebsiteStub()
194  {
195  $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['getCode', '__wakeup']);
196 
197  $website->expects($this->any())->method('getCode')->will($this->returnValue('base'));
198 
199  return $website;
200  }
201 
207  protected function _getStoreStub()
208  {
209  $store = $this->createMock(\Magento\Store\Model\Store::class);
210 
211  $store->expects($this->any())->method('getBaseUrl')->will($this->returnValue('http://localhost/pub/media/'));
212 
213  return $store;
214  }
215 }
$storeManager
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31