forked from ezsystems/ezpublish-kernel
/
ConsoleCommandListenerTest.php
132 lines (117 loc) · 4.47 KB
/
ConsoleCommandListenerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* File containing the ConsoleCommandListenerTest class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Bundle\EzPublishCoreBundle\Tests\EventListener;
use eZ\Bundle\EzPublishCoreBundle\EventListener\ConsoleCommandListener;
use eZ\Bundle\EzPublishCoreBundle\Tests\EventListener\Stubs\TestOutput;
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\Output;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ConsoleCommandListenerTest extends TestCase
{
/**
* @var array
*/
private $siteAccessList = ['default', 'site1'];
/**
* @var SiteAccess
*/
private $siteAccess;
/**
* @var EventDispatcherInterface|MockObject
*/
private $dispatcher;
/**
* @var ConsoleCommandListener
*/
private $listener;
/**
* @var InputDefinition;
*/
private $inputDefinition;
/**
* @var Command
*/
private $command;
/**
* @var TestOutput
*/
private $testOutput;
public function setUp()
{
parent::setUp();
$this->siteAccess = new SiteAccess();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->listener = new ConsoleCommandListener('default', $this->siteAccessList, $this->dispatcher);
$this->listener->setSiteAccess($this->siteAccess);
$this->dispatcher->addSubscriber($this->listener);
$this->command = new Command('test:siteaccess');
$this->inputDefinition = new InputDefinition(array(new InputOption('siteaccess', null, InputOption::VALUE_OPTIONAL)));
$this->testOutput = new TestOutput(Output::VERBOSITY_QUIET, true);
}
public function testGetSubscribedEvents()
{
$this->assertSame(
array(
ConsoleEvents::COMMAND => array(array('onConsoleCommand', -1)),
),
$this->listener->getSubscribedEvents()
);
}
/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\. Valid siteaccesses are/
*/
public function testInvalidSiteAccessDev()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(true);
$this->listener->onConsoleCommand($event);
}
/**
* @expectedException \eZ\Publish\Core\MVC\Exception\InvalidSiteAccessException
* @expectedExceptionMessageRegExp /^Invalid siteaccess 'foo', matched by .+\.$/
*/
public function testInvalidSiteAccessProd()
{
$this->dispatcher->expects($this->never())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'foo'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->setDebug(false);
$this->listener->onConsoleCommand($event);
}
public function testValidSiteAccess()
{
$this->dispatcher->expects($this->once())
->method('dispatch');
$input = new ArrayInput(array('--siteaccess' => 'site1'), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->onConsoleCommand($event);
$this->assertEquals(new SiteAccess('site1', 'cli'), $this->siteAccess);
}
public function testDefaultSiteAccess()
{
$this->dispatcher->expects($this->once())
->method('dispatch');
$input = new ArrayInput(array(), $this->inputDefinition);
$event = new ConsoleCommandEvent($this->command, $input, $this->testOutput);
$this->listener->onConsoleCommand($event);
$this->assertEquals(new SiteAccess('default', 'cli'), $this->siteAccess);
}
}