Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SimpleCollectorTest.php
Go to the documentation of this file.
1 <?php
7 
11 use Symfony\Component\Console\Input\InputInterface;
12 use Symfony\Component\Console\Output\OutputInterface;
13 use Symfony\Component\Console\Question\Question;
14 use Symfony\Component\Console\Question\QuestionFactory;
15 use Symfony\Component\Console\Helper\QuestionHelper;
16 use PHPUnit_Framework_MockObject_MockObject as MockObject;
17 
18 class SimpleCollectorTest extends \PHPUnit\Framework\TestCase
19 {
23  private $questionFactoryMock;
24 
28  private $questionHelperMock;
29 
33  private $inputMock;
34 
38  private $outputMock;
39 
43  private $model;
44 
48  protected function setUp()
49  {
50  $this->questionFactoryMock = $this->getMockBuilder(QuestionFactory::class)
51  ->disableOriginalConstructor()
52  ->setMethods(['create'])
53  ->getMock();
54  $this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
55  ->disableOriginalConstructor()
56  ->getMock();
57  $this->inputMock = $this->getMockBuilder(InputInterface::class)
58  ->getMockForAbstractClass();
59  $this->outputMock = $this->getMockBuilder(OutputInterface::class)
60  ->getMockForAbstractClass();
61 
62  $this->model = new SimpleCollector(
63  $this->questionFactoryMock,
64  $this->questionHelperMock
65  );
66  }
67 
68  public function testGetValues()
69  {
70  $configPaths = [
71  'some/config/path1',
72  'some/config/path2'
73  ];
74 
75  $pathQuestionMock = $this->getMockBuilder(Question::class)
76  ->disableOriginalConstructor()
77  ->getMock();
78  $valueQuestionMock = $this->getMockBuilder(Question::class)
79  ->disableOriginalConstructor()
80  ->getMock();
81  $this->inputMock->expects($this->exactly(2))
82  ->method('getArgument')
83  ->withConsecutive(
86  )
87  ->willReturnOnConsecutiveCalls(
88  $configPaths[0],
89  'someValue'
90  );
91  $this->questionFactoryMock->expects($this->exactly(2))
92  ->method('create')
93  ->withConsecutive(
94  [['question' => 'Please enter config path: ']],
95  [['question' => 'Please enter value: ']]
96  )
97  ->willReturnOnConsecutiveCalls(
98  $pathQuestionMock,
99  $valueQuestionMock
100  );
101 
102  $this->assertEquals(
103  ['some/config/path1' => 'someValue'],
104  $this->model->getValues(
105  $this->inputMock,
106  $this->outputMock,
107  $configPaths
108  )
109  );
110  }
111 
116  public function testWrongConfigPath()
117  {
118  $configPaths = [
119  'some/config/path1',
120  'some/config/path2'
121  ];
122 
123  $pathQuestionMock = $this->getMockBuilder(Question::class)
124  ->disableOriginalConstructor()
125  ->getMock();
126  $this->inputMock->expects($this->once())
127  ->method('getArgument')
129  ->willReturn('some/not_exist/config');
130  $this->questionFactoryMock->expects($this->once())
131  ->method('create')
132  ->with(['question' => 'Please enter config path: '])
133  ->willReturn($pathQuestionMock);
134 
135  $this->model->getValues(
136  $this->inputMock,
137  $this->outputMock,
138  $configPaths
139  );
140  }
141 
145  public function testEmptyValue()
146  {
147  $configPaths = [
148  'some/config/path1',
149  'some/config/path2'
150  ];
151  $message = 'exception message';
152 
153  $pathQuestionMock = $this->getMockBuilder(Question::class)
154  ->disableOriginalConstructor()
155  ->getMock();
156  $valueQuestionMock = $this->getMockBuilder(Question::class)
157  ->disableOriginalConstructor()
158  ->getMock();
159  $this->questionHelperMock->expects($this->once())
160  ->method('ask')
161  ->with($this->inputMock, $this->outputMock, $valueQuestionMock)
162  ->willThrowException(new LocalizedException(__($message)));
163  $this->inputMock->expects($this->exactly(2))
164  ->method('getArgument')
165  ->withConsecutive(
168  )
169  ->willReturnOnConsecutiveCalls(
170  $configPaths[0],
171  null
172  );
173  $this->questionFactoryMock->expects($this->exactly(2))
174  ->method('create')
175  ->withConsecutive(
176  [['question' => 'Please enter config path: ']],
177  [['question' => 'Please enter value: ']]
178  )
179  ->willReturnOnConsecutiveCalls(
180  $pathQuestionMock,
181  $valueQuestionMock
182  );
183 
184  $this->model->getValues(
185  $this->inputMock,
186  $this->outputMock,
187  $configPaths
188  );
189  }
190 }
__()
Definition: __.php:13
$message