Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportWriterTest.php
Go to the documentation of this file.
1 <?php
7 
15 
19 class ReportWriterTest extends \PHPUnit\Framework\TestCase
20 {
24  private $configInterfaceMock;
25 
29  private $reportValidatorMock;
30 
34  private $providerFactoryMock;
35 
39  private $reportProviderMock;
40 
44  private $objectManagerHelper;
45 
49  private $directoryMock;
50 
54  private $reportWriter;
55 
59  private $reportName = 'testReport';
60 
64  private $providerName = 'testProvider';
65 
69  private $providerClass = 'Magento\Analytics\Provider';
70 
74  protected function setUp()
75  {
76  $this->configInterfaceMock = $this->getMockBuilder(ConfigInterface::class)->getMockForAbstractClass();
77  $this->reportValidatorMock = $this->getMockBuilder(ReportValidator::class)
78  ->disableOriginalConstructor()->getMock();
79  $this->providerFactoryMock = $this->getMockBuilder(ProviderFactory::class)
80  ->disableOriginalConstructor()->getMock();
81  $this->reportProviderMock = $this->getMockBuilder(ReportProvider::class)
82  ->disableOriginalConstructor()->getMock();
83  $this->directoryMock = $this->getMockBuilder(WriteInterface::class)->getMockForAbstractClass();
84  $this->objectManagerHelper = new ObjectManagerHelper($this);
85 
86  $this->reportWriter = $this->objectManagerHelper->getObject(
87  ReportWriter::class,
88  [
89  'config' => $this->configInterfaceMock,
90  'reportValidator' => $this->reportValidatorMock,
91  'providerFactory' => $this->providerFactoryMock
92  ]
93  );
94  }
95 
102  public function testWrite(array $configData)
103  {
104  $errors = [];
105  $fileData = [
106  ['number' => 1, 'type' => 'Shoes Usual']
107  ];
108  $this->configInterfaceMock
109  ->expects($this->once())
110  ->method('get')
111  ->with()
112  ->willReturn([$configData]);
113  $this->providerFactoryMock
114  ->expects($this->once())
115  ->method('create')
116  ->with($this->providerClass)
117  ->willReturn($this->reportProviderMock);
118  $parameterName = isset(reset($configData)[0]['parameters']['name'])
119  ? reset($configData)[0]['parameters']['name']
120  : '';
121  $this->reportProviderMock->expects($this->once())
122  ->method('getReport')
123  ->with($parameterName ?: null)
124  ->willReturn($fileData);
125  $errorStreamMock = $this->getMockBuilder(
126  \Magento\Framework\Filesystem\File\WriteInterface::class
127  )->getMockForAbstractClass();
128  $errorStreamMock
129  ->expects($this->once())
130  ->method('lock')
131  ->with();
132  $errorStreamMock
133  ->expects($this->exactly(2))
134  ->method('writeCsv')
135  ->withConsecutive(
136  [array_keys($fileData[0])],
137  [$fileData[0]]
138  );
139  $errorStreamMock->expects($this->once())->method('unlock');
140  $errorStreamMock->expects($this->once())->method('close');
141  if ($parameterName) {
142  $this->reportValidatorMock
143  ->expects($this->once())
144  ->method('validate')
145  ->with($parameterName)
146  ->willReturn($errors);
147  }
148  $this->directoryMock
149  ->expects($this->once())
150  ->method('openFile')
151  ->with(
152  $this->stringContains('/var/tmp' . $parameterName ?: $this->reportName),
153  'w+'
154  )->willReturn($errorStreamMock);
155  $this->assertTrue($this->reportWriter->write($this->directoryMock, '/var/tmp'));
156  }
157 
165  {
166  $errors = ['orders', 'SQL Error: test'];
167  $this->configInterfaceMock->expects($this->once())->method('get')->willReturn([$configData]);
168  $errorStreamMock = $this->getMockBuilder(
169  \Magento\Framework\Filesystem\File\WriteInterface::class
170  )->getMockForAbstractClass();
171  $errorStreamMock->expects($this->once())->method('lock');
172  $errorStreamMock->expects($this->once())->method('writeCsv')->with($errors);
173  $errorStreamMock->expects($this->once())->method('unlock');
174  $errorStreamMock->expects($this->once())->method('close');
175  $this->reportValidatorMock->expects($this->once())->method('validate')->willReturn($errors);
176  $this->directoryMock->expects($this->once())->method('openFile')->with('/var/tmp' . 'errors.csv', 'w+')
177  ->willReturn($errorStreamMock);
178  $this->assertTrue($this->reportWriter->write($this->directoryMock, '/var/tmp'));
179  }
180 
184  public function testWriteEmptyReports()
185  {
186  $this->configInterfaceMock->expects($this->once())->method('get')->willReturn([]);
187  $this->reportValidatorMock->expects($this->never())->method('validate');
188  $this->directoryMock->expects($this->never())->method('openFile');
189  $this->assertTrue($this->reportWriter->write($this->directoryMock, '/var/tmp'));
190  }
191 
195  public function configDataProvider()
196  {
197  return [
198  'reportProvider' => [
199  [
200  'providers' => [
201  [
202  'name' => $this->providerName,
203  'class' => $this->providerClass,
204  'parameters' => [
205  'name' => $this->reportName
206  ],
207  ]
208  ]
209  ]
210  ],
211  ];
212  }
213 }
$errors
Definition: overview.phtml:9