Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ReportValidatorTest.php
Go to the documentation of this file.
1 <?php
7 
15 
16 class ReportValidatorTest extends \PHPUnit\Framework\TestCase
17 {
21  private $connectionFactoryMock;
22 
26  private $queryFactoryMock;
27 
31  private $queryMock;
32 
36  private $connectionMock;
37 
41  private $selectMock;
42 
46  private $objectManagerHelper;
47 
51  private $reportValidator;
52 
56  protected function setUp()
57  {
58  $this->connectionFactoryMock = $this->getMockBuilder(ConnectionFactory::class)
59  ->disableOriginalConstructor()->getMock();
60  $this->queryFactoryMock = $this->getMockBuilder(QueryFactory::class)
61  ->disableOriginalConstructor()->getMock();
62  $this->queryMock = $this->getMockBuilder(Query::class)->disableOriginalConstructor()
63  ->getMock();
64  $this->connectionMock = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
65  $this->selectMock = $this->getMockBuilder(Select::class)->disableOriginalConstructor()
66  ->getMock();
67  $this->objectManagerHelper = new ObjectManagerHelper($this);
68 
69  $this->reportValidator = $this->objectManagerHelper->getObject(
70  ReportValidator::class,
71  [
72  'connectionFactory' => $this->connectionFactoryMock,
73  'queryFactory' => $this->queryFactoryMock
74  ]
75  );
76  }
77 
84  public function testValidate($reportName, $result, \PHPUnit\Framework\MockObject\Stub $queryReturnStub)
85  {
86  $connectionName = 'testConnection';
87  $this->queryFactoryMock->expects($this->once())
88  ->method('create')
89  ->willReturn($this->queryMock);
90  $this->queryMock->expects($this->once())->method('getConnectionName')->willReturn($connectionName);
91  $this->connectionFactoryMock->expects($this->once())->method('getConnection')
92  ->with($connectionName)
93  ->willReturn($this->connectionMock);
94  $this->queryMock->expects($this->atLeastOnce())->method('getSelect')->willReturn($this->selectMock);
95  $this->selectMock->expects($this->once())->method('limit')->with(0);
96  $this->connectionMock->expects($this->once())->method('query')->with($this->selectMock)->will($queryReturnStub);
97  $this->assertEquals($result, $this->reportValidator->validate($reportName));
98  }
99 
105  public function errorDataProvider()
106  {
107  $reportName = 'test';
108  $errorMessage = 'SQL Error 42';
109  return [
110  [
111  $reportName,
112  'expectedResult' => [],
113  'queryReturnStub' => $this->returnValue(null)
114  ],
115  [
116  $reportName,
117  'expectedResult' => [$reportName, $errorMessage],
118  'queryReturnStub' => $this->throwException(new \Zend_Db_Statement_Exception($errorMessage))
119  ]
120  ];
121  }
122 }
testValidate($reportName, $result, \PHPUnit\Framework\MockObject\Stub $queryReturnStub)