Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
YesNoTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Symfony\Component\Console\Helper\QuestionHelper;
10 use Symfony\Component\Console\Input\InputInterface;
11 use Symfony\Component\Console\Output\OutputInterface;
12 use Symfony\Component\Console\Question\QuestionFactory;
13 use Symfony\Component\Console\Question\Question;
14 
15 class YesNoTest extends \PHPUnit\Framework\TestCase
16 {
20  private $inputMock;
21 
25  private $outputMock;
26 
30  private $questionHelperMock;
31 
35  private $questionFactoryMock;
36 
40  private $questionPerformer;
41 
45  protected function setUp()
46  {
47  $this->inputMock = $this->getMockBuilder(InputInterface::class)
48  ->getMockForAbstractClass();
49  $this->outputMock = $this->getMockBuilder(OutputInterface::class)
50  ->getMockForAbstractClass();
51  $this->questionFactoryMock = $this->getMockBuilder(QuestionFactory::class)
52  ->disableOriginalConstructor()
53  ->setMethods(['create'])
54  ->getMock();
55  $this->questionHelperMock = $this->getMockBuilder(QuestionHelper::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58 
59  $this->questionPerformer = new YesNo($this->questionHelperMock, $this->questionFactoryMock);
60  }
61 
67  public function testExecute($answer, $expectedResult)
68  {
69  $firstMessage = 'First message';
70  $secondMessage = 'Second message';
71  $messages = [$firstMessage, $secondMessage];
72 
74  $questionMock = $this->getMockBuilder(Question::class)
75  ->disableOriginalConstructor()
76  ->getMock();
77  $questionMock->expects($this->once())
78  ->method('setValidator');
79  $this->questionFactoryMock->expects($this->once())
80  ->method('create')
81  ->with(['question' => $firstMessage . PHP_EOL . $secondMessage . PHP_EOL])
82  ->willReturn($questionMock);
83  $this->questionHelperMock->expects($this->once())
84  ->method('ask')
85  ->with($this->inputMock, $this->outputMock, $questionMock)
86  ->willReturn($answer);
87  $this->inputMock->expects($this->once())
88  ->method('isInteractive')
89  ->willReturn(true);
90 
91  $this->assertSame(
92  $expectedResult,
93  $this->questionPerformer->execute($messages, $this->inputMock, $this->outputMock)
94  );
95  }
96 
100  public function executeDataProvider()
101  {
102  return [
103  ['yes', true],
104  ['Yes', true],
105  ['YES', true],
106  ['y', true],
107  ['Y', true],
108  ['ya', false],
109  ['no', false],
110  ['NO', false],
111  ['n', false],
112  ['N', false],
113  ['Not', false],
114  ['anykey', false]
115  ];
116  }
117 }