Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
NotifyDataChangedCommandTest.php
Go to the documentation of this file.
1 <?php
7 
15 use Psr\Log\LoggerInterface;
18 
19 class NotifyDataChangedCommandTest extends \PHPUnit\Framework\TestCase
20 {
24  private $notifyDataChangedCommand;
25 
29  private $analyticsTokenMock;
30 
34  private $httpClientMock;
35 
39  public $configMock;
40 
44  private $loggerMock;
45 
46  protected function setUp()
47  {
48  $this->analyticsTokenMock = $this->getMockBuilder(AnalyticsToken::class)
49  ->disableOriginalConstructor()
50  ->getMock();
51 
52  $this->httpClientMock = $this->getMockBuilder(ClientInterface::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55 
56  $this->configMock = $this->getMockBuilder(ScopeConfigInterface::class)
57  ->disableOriginalConstructor()
58  ->getMock();
59 
60  $this->loggerMock = $this->getMockBuilder(LoggerInterface::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63  $successHandler = $this->getMockBuilder(\Magento\Analytics\Model\Connector\Http\ResponseHandlerInterface::class)
64  ->getMockForAbstractClass();
65  $successHandler->method('handleResponse')
66  ->willReturn(true);
67  $serializerMock = $this->getMockBuilder(Json::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70  $serializerMock->expects($this->any())
71  ->method('unserialize')
72  ->willReturn(['unserialized data']);
73  $objectManager = new ObjectManager($this);
74  $this->notifyDataChangedCommand = $objectManager->getObject(
75  NotifyDataChangedCommand::class,
76  [
77  'analyticsToken' => $this->analyticsTokenMock,
78  'httpClient' => $this->httpClientMock,
79  'config' => $this->configMock,
80  'responseResolver' => $objectManager->getObject(
81  ResponseResolver::class,
82  [
83  'converter' => $objectManager->getObject(
84  JsonConverter::class,
85  ['serializer' => $serializerMock]
86  ),
87  'responseHandlers' => [201 => $successHandler]
88  ]
89  ),
90  'logger' => $this->loggerMock
91  ]
92  );
93  }
94 
95  public function testExecuteSuccess()
96  {
97  $configVal = "Config val";
98  $token = "Secret token!";
99  $this->analyticsTokenMock->expects($this->once())
100  ->method('isTokenExist')
101  ->willReturn(true);
102  $this->configMock->expects($this->any())
103  ->method('getValue')
104  ->willReturn($configVal);
105  $this->analyticsTokenMock->expects($this->once())
106  ->method('getToken')
107  ->willReturn($token);
108  $this->httpClientMock->expects($this->once())
109  ->method('request')
110  ->with(
112  $configVal,
113  ['access-token' => $token, 'url' => $configVal]
114  )->willReturn(new \Zend_Http_Response(201, []));
115  $this->assertTrue($this->notifyDataChangedCommand->execute());
116  }
117 
118  public function testExecuteWithoutToken()
119  {
120  $this->analyticsTokenMock->expects($this->once())
121  ->method('isTokenExist')
122  ->willReturn(false);
123  $this->httpClientMock->expects($this->never())
124  ->method('request');
125  $this->assertFalse($this->notifyDataChangedCommand->execute());
126  }
127 }
$objectManager
Definition: bootstrap.php:17