Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportProviderTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ReportProviderTest extends \PHPUnit\Framework\TestCase
12 {
16  private $subject;
17 
21  private $queryMock;
22 
26  private $selectMock;
27 
31  private $iteratorMock;
32 
36  private $statementMock;
37 
41  private $connectionMock;
42 
46  private $queryFactoryMock;
47 
51  private $objectManagerHelper;
52 
56  private $connectionFactoryMock;
57 
61  private $iteratorFactoryMock;
62 
66  protected function setUp()
67  {
68  $this->selectMock = $this->getMockBuilder(
69  \Magento\Framework\DB\Select::class
70  )
71  ->disableOriginalConstructor()
72  ->getMock();
73 
74  $this->queryMock = $this->getMockBuilder(
75  \Magento\Analytics\ReportXml\Query::class
76  )
77  ->disableOriginalConstructor()
78  ->getMock();
79  $this->queryMock->expects($this->any())
80  ->method('getSelect')
81  ->willReturn($this->selectMock);
82 
83  $this->iteratorMock = $this->getMockBuilder(
84  \IteratorIterator::class
85  )
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->statementMock = $this->getMockBuilder(
90  \Magento\Framework\DB\Statement\Pdo\Mysql::class
91  )
92  ->disableOriginalConstructor()
93  ->getMock();
94  $this->statementMock->expects($this->any())
95  ->method('getIterator')
96  ->willReturn($this->iteratorMock);
97 
98  $this->connectionMock = $this->getMockBuilder(
99  \Magento\Framework\DB\Adapter\AdapterInterface::class
100  )
101  ->disableOriginalConstructor()
102  ->getMock();
103 
104  $this->queryFactoryMock = $this->getMockBuilder(
105  \Magento\Analytics\ReportXml\QueryFactory::class
106  )
107  ->disableOriginalConstructor()
108  ->getMock();
109 
110  $this->iteratorFactoryMock = $this->getMockBuilder(
111  \Magento\Analytics\ReportXml\IteratorFactory::class
112  )
113  ->disableOriginalConstructor()
114  ->getMock();
115  $this->iteratorMock = $this->getMockBuilder(
116  \IteratorIterator::class
117  )
118  ->disableOriginalConstructor()
119  ->getMock();
120  $this->objectManagerHelper =
121  new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
122 
123  $this->connectionFactoryMock = $this->getMockBuilder(
124  \Magento\Analytics\ReportXml\ConnectionFactory::class
125  )
126  ->disableOriginalConstructor()
127  ->getMock();
128 
129  $this->subject = $this->objectManagerHelper->getObject(
130  \Magento\Analytics\ReportXml\ReportProvider::class,
131  [
132  'queryFactory' => $this->queryFactoryMock,
133  'connectionFactory' => $this->connectionFactoryMock,
134  'iteratorFactory' => $this->iteratorFactoryMock
135  ]
136  );
137  }
138 
142  public function testGetReport()
143  {
144  $reportName = 'test_report';
145  $connectionName = 'sales';
146 
147  $this->queryFactoryMock->expects($this->once())
148  ->method('create')
149  ->with($reportName)
150  ->willReturn($this->queryMock);
151 
152  $this->connectionFactoryMock->expects($this->once())
153  ->method('getConnection')
154  ->with($connectionName)
155  ->willReturn($this->connectionMock);
156 
157  $this->queryMock->expects($this->once())
158  ->method('getConnectionName')
159  ->willReturn($connectionName);
160 
161  $this->queryMock->expects($this->once())
162  ->method('getConfig')
163  ->willReturn(
164  [
165  'connection' => $connectionName
166  ]
167  );
168 
169  $this->connectionMock->expects($this->once())
170  ->method('query')
171  ->with($this->selectMock)
172  ->willReturn($this->statementMock);
173 
174  $this->iteratorFactoryMock->expects($this->once())
175  ->method('create')
176  ->with($this->statementMock, null)
177  ->willReturn($this->iteratorMock);
178  $this->assertEquals($this->iteratorMock, $this->subject->getReport($reportName));
179  }
180 }