Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
MagentoStyleTest.php
Go to the documentation of this file.
1 <?php
8 
11 use PHPUnit\Framework\TestCase;
12 use Symfony\Component\Console\Formatter\OutputFormatter;
13 use Symfony\Component\Console\Helper\SymfonyQuestionHelper;
14 use Symfony\Component\Console\Helper\TableCell;
15 use Symfony\Component\Console\Input\ArrayInput;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputDefinition;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20 
24 class MagentoStyleTest extends TestCase
25 {
31  private $magentoStyle;
32 
38  private $testOutput;
39 
43  protected function setUp()
44  {
45  $input = new ArrayInput(['name' => 'foo'], new InputDefinition([new InputArgument('name')]));
46  $this->testOutput = new TestOutput();
47  $this->magentoStyle = new MagentoStyle($input, $this->testOutput);
48  }
49 
55  public function testBlockStyle()
56  {
57  $this->magentoStyle->block(
58  ['test first message', 'test second message'],
59  'testBlockType',
60  'testBlockStyle',
61  'testBlockPrefix'
62  );
63  // @codingStandardsIgnoreStart
64  $expected = PHP_EOL . PHP_EOL . PHP_EOL .
65  '<testBlockStyle>testBlockPrefix\[testBlockType\] test first message\s+'
66  . PHP_EOL . '<testBlockStyle>testBlockPrefix\s+'
67  . PHP_EOL . '<testBlockStyle>testBlockPrefix \s+ test second message\s+'
68  . PHP_EOL . PHP_EOL;
69  // @codingStandardsIgnoreEnd
70  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Block does not match output');
71  }
72 
78  public function testTitleStyle()
79  {
80  $this->magentoStyle->title('My Title');
81  $expected = PHP_EOL . PHP_EOL . PHP_EOL . ' My Title' . PHP_EOL . ' ========' . PHP_EOL . PHP_EOL;
82  $this->assertEquals($expected, $this->testOutput->output, 'Title does not match output');
83  }
84 
90  public function testSectionStyle()
91  {
92  $this->magentoStyle->section('My Section');
93  $expected = PHP_EOL . PHP_EOL . PHP_EOL . ' My Section' . PHP_EOL . ' ----------' . PHP_EOL . PHP_EOL;
94  $this->assertEquals($expected, $this->testOutput->output, 'Section does not match output');
95  }
96 
102  public function testListingStyle()
103  {
104  $this->magentoStyle->listing(['test first element', 'test second element']);
105  $expected = PHP_EOL . ' * test first element' . PHP_EOL . ' * test second element' . PHP_EOL . PHP_EOL;
106  $this->assertEquals($expected, $this->testOutput->output, 'Listing does not match output');
107  }
108 
114  public function testTextStyle()
115  {
116  $this->magentoStyle->text('test message');
117  $expected = PHP_EOL . ' test message' . PHP_EOL;
118 
119  $this->assertEquals($expected, $this->testOutput->output, 'Text does not match output');
120  }
121 
127  public function testCommentStyle()
128  {
129  $this->magentoStyle->comment('test comment');
130  $expected = PHP_EOL . PHP_EOL . PHP_EOL . '\s+test comment\s+' . PHP_EOL . PHP_EOL;
131  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Comment does not match output');
132  }
133 
139  public function testSuccessStyle()
140  {
141  $this->magentoStyle->success('test success message');
142  $expected = PHP_EOL . PHP_EOL . PHP_EOL . ' \[SUCCESS\] test success message\s+' . PHP_EOL . PHP_EOL;
143  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Success message does not match output');
144  }
145 
151  public function testErrorStyle()
152  {
153  $this->magentoStyle->error('test error message');
154  $expected = PHP_EOL . PHP_EOL . PHP_EOL . '\s+\[ERROR\] test error message\s+' . PHP_EOL . PHP_EOL;
155  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Error message does not match output');
156  }
157 
163  public function testWarningStyle()
164  {
165  $this->magentoStyle->warning('test warning message');
166  $expected = PHP_EOL . PHP_EOL . PHP_EOL . '\s+\[WARNING\] test warning message\s+' . PHP_EOL . PHP_EOL;
167  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Warning message does not match output');
168  }
169 
175  public function testNoteStyle()
176  {
177  $this->magentoStyle->note('test note message');
178  $expected = PHP_EOL . PHP_EOL . PHP_EOL . '\s+\[NOTE\] test note message\s+' . PHP_EOL . PHP_EOL;
179  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Note message does not match output');
180  }
181 
187  public function testCautionStyle()
188  {
189  $this->magentoStyle->caution('test caution message');
190  $expected = PHP_EOL . PHP_EOL . PHP_EOL . '\s+! \[CAUTION\] test caution message\s+' . PHP_EOL . PHP_EOL;
191  $this->assertRegExp('/' . $expected . '/', $this->testOutput->output, 'Caution message does not match output');
192  }
193 
199  public function testTableStyle()
200  {
201  $headers = [
202  [new TableCell('Main table title', ['colspan' => 2])],
203  ['testHeader1', 'testHeader2', 'testHeader3'],
204  ];
205  $rows = [
206  [
207  'testValue1',
208  'testValue2',
209  new TableCell('testValue3', ['rowspan' => 2]),
210  ],
211  ['testValue4', 'testValue5'],
212  ];
213  $this->magentoStyle->table($headers, $rows);
214  $expected = ' ------------- ------------- ------------- ' . PHP_EOL .
215  ' Main table title ' . PHP_EOL .
216  ' ------------- ------------- ------------- ' . PHP_EOL .
217  ' testHeader1 testHeader2 testHeader3 ' . PHP_EOL .
218  ' ------------- ------------- ------------- ' . PHP_EOL .
219  ' testValue1 testValue2 testValue3 ' . PHP_EOL .
220  ' testValue4 testValue5 ' . PHP_EOL .
221  ' ------------- ------------- ------------- ' . PHP_EOL . PHP_EOL;
222 
223  $this->assertEquals($expected, $this->testOutput->output, 'Table does not match output');
224  }
225 
229  public function testAsk()
230  {
231  $objectManager = new ObjectManager($this);
232  $formatter = $this->getMockBuilder(OutputFormatter::class)
233  ->disableOriginalConstructor()
234  ->getMock();
235  $input = $this->getMockBuilder(InputInterface::class)
236  ->setMethods(['isInteractive'])
237  ->disableOriginalConstructor()
238  ->getMockForAbstractClass();
239  $input->expects($this->exactly(2))
240  ->method('isInteractive')
241  ->willReturn(false);
242  $output = $this->getMockBuilder(OutputInterface::class)
243  ->setMethods(['getVerbosity', 'getFormatter'])
244  ->disableOriginalConstructor()
245  ->getMockForAbstractClass();
246  $output->expects($this->once())
247  ->method('getVerbosity')
248  ->willReturn(32);
249  $output->expects($this->once())
250  ->method('getFormatter')
251  ->willReturn($formatter);
252  $magentoStyle = $objectManager->getObject(
253  MagentoStyle::class,
254  [
255  'input' => $input,
256  'output' => $output,
257  ]
258  );
259  $questionHelper = $this->getMockBuilder(SymfonyQuestionHelper::class)
260  ->disableOriginalConstructor()
261  ->getMock();
262  $questionHelper->expects($this->once())
263  ->method('ask')
264  ->willReturn('test Answer');
265  $objectManager->setBackwardCompatibleProperty($magentoStyle, 'questionHelper', $questionHelper);
266 
267  $this->assertEquals(
268  'test Answer',
269  $magentoStyle->ask('test question?', 'test default')
270  );
271  }
272 
278  public function testProgress()
279  {
280  $this->magentoStyle->progressStart(2);
281  $this->magentoStyle->progressAdvance(3);
282  $this->magentoStyle->progressFinish();
283  $expected = ' 0/2 [> ] 0%' . PHP_EOL .
284  ' 3/3 [============================] 100%' . PHP_EOL . PHP_EOL;
285  $this->assertEquals($expected, $this->testOutput->output, 'Progress does not match output');
286  }
287 }
$objectManager
Definition: bootstrap.php:17