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
8 
9 class DataTest extends \PHPUnit\Framework\TestCase
10 {
16  protected $helper;
17 
23  protected $scopeConfigMock;
24 
31 
38 
43 
44  protected function setUp()
45  {
46  $this->objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
47  $className = \Magento\Contact\Helper\Data::class;
48  $arguments = $this->objectManagerHelper->getConstructArguments($className);
52  $context = $arguments['context'];
53  $this->scopeConfigMock = $context->getScopeConfig();
54  $this->customerSessionMock = $arguments['customerSession'];
55  $this->customerViewHelperMock = $arguments['customerViewHelper'];
56  $this->helper = $this->objectManagerHelper->getObject($className, $arguments);
57  }
58 
59  public function testIsEnabled()
60  {
61  $this->scopeConfigMock->expects($this->once())
62  ->method('getValue')
63  ->willReturn('1');
64 
65  $this->assertTrue(is_string($this->helper->isEnabled()));
66  }
67 
68  public function testIsNotEnabled()
69  {
70  $this->scopeConfigMock->expects($this->once())
71  ->method('getValue')
72  ->willReturn(null);
73 
74  $this->assertTrue(null === $this->helper->isEnabled());
75  }
76 
77  public function testGetUserNameNotLoggedIn()
78  {
79  $this->customerSessionMock->expects($this->once())
80  ->method('isLoggedIn')
81  ->willReturn(false);
82 
83  $this->assertEmpty($this->helper->getUserName());
84  }
85 
86  public function testGetUserName()
87  {
88  $this->customerSessionMock->expects($this->once())
89  ->method('isLoggedIn')
90  ->willReturn(true);
91 
92  $customerDataObject = $this->getMockBuilder(\Magento\Customer\Model\Data\Customer::class)
93  ->disableOriginalConstructor()
94  ->getMock();
95  $this->customerSessionMock->expects($this->once())
96  ->method('getCustomerDataObject')
97  ->willReturn($customerDataObject);
98 
99  $this->customerViewHelperMock->expects($this->once())
100  ->method('getCustomerName')
101  ->willReturn(' customer name ');
102 
103  $this->assertEquals('customer name', $this->helper->getUserName());
104  }
105 
106  public function testGetUserEmailNotLoggedIn()
107  {
108  $this->customerSessionMock->expects($this->once())
109  ->method('isLoggedIn')
110  ->willReturn(false);
111 
112  $this->assertEmpty($this->helper->getUserEmail());
113  }
114 
115  public function testGetUserEmail()
116  {
117  $this->customerSessionMock->expects($this->once())
118  ->method('isLoggedIn')
119  ->willReturn(true);
120 
121  $customerDataObject = $this->createMock(\Magento\Customer\Api\Data\CustomerInterface::class);
122  $customerDataObject->expects($this->once())
123  ->method('getEmail')
124  ->willReturn('[email protected]');
125 
126  $this->customerSessionMock->expects($this->once())
127  ->method('getCustomerDataObject')
128  ->willReturn($customerDataObject);
129 
130  $this->assertEquals('[email protected]', $this->helper->getUserEmail());
131  }
132 
133  public function testGetPostValue()
134  {
135  $postData = ['name' => 'Some Name', 'email' => 'Some Email'];
136 
137  $dataPersistorMock = $this->getMockBuilder(\Magento\Framework\App\Request\DataPersistorInterface::class)
138  ->getMockForAbstractClass();
139  $dataPersistorMock->expects($this->once())
140  ->method('get')
141  ->with('contact_us')
142  ->willReturn($postData);
143  $dataPersistorMock->expects($this->once())
144  ->method('clear')
145  ->with('contact_us');
146 
147  $this->objectManagerHelper->setBackwardCompatibleProperty($this->helper, 'dataPersistor', $dataPersistorMock);
148 
149  $this->assertSame($postData['name'], $this->helper->getPostValue('name'));
150  $this->assertSame($postData['email'], $this->helper->getPostValue('email'));
151  }
152 }
$arguments
if($currentSelectedMethod==$_code) $className
Definition: form.phtml:31