|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPFastCGI\Test\FastCGIDaemon; |
| 4 | + |
| 5 | +use PHPFastCGI\FastCGIDaemon\Command\DaemonRunCommand; |
| 6 | +use PHPFastCGI\FastCGIDaemon\DaemonFactory; |
| 7 | +use Symfony\Component\Console\Input\ArrayInput; |
| 8 | +use Symfony\Component\Console\Output\NullOutput; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests the daemon run command |
| 12 | + */ |
| 13 | +class DaemonRunCommandTest extends \PHPUnit_Framework_TestCase |
| 14 | +{ |
| 15 | + /** |
| 16 | + * Tests an \InvalidArgumentException is thrown when it isn't constructed |
| 17 | + * with a callable. |
| 18 | + * |
| 19 | + * @expectedException \InvalidArgumentException |
| 20 | + */ |
| 21 | + public function testInvalidArgumentException() |
| 22 | + { |
| 23 | + new DaemonRunCommand('name', 'description', new DaemonFactory(), 'not a callable function'); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Tests that two optional options 'port' and 'host' are added to the |
| 28 | + * command. |
| 29 | + */ |
| 30 | + public function testOptions() |
| 31 | + { |
| 32 | + $command = new DaemonRunCommand('name', 'description', new DaemonFactory(), function () { }); |
| 33 | + |
| 34 | + $definition = $command->getDefinition(); |
| 35 | + |
| 36 | + $portOption = $definition->getOption('port'); |
| 37 | + $this->assertTrue($portOption->isValueOptional()); |
| 38 | + |
| 39 | + $hostOption = $definition->getOption('host'); |
| 40 | + $this->assertTrue($hostOption->isValueOptional()); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Test invalid option configuration. An Invalid argument exception should |
| 45 | + * be thrown if the host option is supplied without the port optional also. |
| 46 | + * |
| 47 | + * @expectedException \InvalidArgumentException |
| 48 | + */ |
| 49 | + public function testInvalidOptions() |
| 50 | + { |
| 51 | + $command = new DaemonRunCommand('name', 'description', new DaemonFactory(), function () { }); |
| 52 | + |
| 53 | + $input = new ArrayInput(['--host' => '_']); |
| 54 | + $output = new NullOutput(); |
| 55 | + |
| 56 | + $command->run($input, $output); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Create a mock daemon to use for testing. |
| 61 | + */ |
| 62 | + private function createMockDaemon() |
| 63 | + { |
| 64 | + $mockDaemon = $this |
| 65 | + ->getMockBuilder('PHPFastCGI\\FastCGIDaemon\\Daemon') |
| 66 | + ->disableOriginalConstructor() |
| 67 | + ->setMethods(['setLogger', 'run']) |
| 68 | + ->getMock(); |
| 69 | + |
| 70 | + $mockDaemon->expects($this->once())->method('setLogger'); |
| 71 | + $mockDaemon->expects($this->once())->method('run'); |
| 72 | + |
| 73 | + return $mockDaemon; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a mock daemon to use for testing |
| 78 | + */ |
| 79 | + private function createMockDaemonFactory($createMethod) |
| 80 | + { |
| 81 | + $mockDaemonFactory = $this |
| 82 | + ->getMockBuilder('PHPFastCGI\\FastCGIDaemon\\DaemonFactory') |
| 83 | + ->disableOriginalConstructor() |
| 84 | + ->setMethods([$createMethod]) |
| 85 | + ->getMock(); |
| 86 | + |
| 87 | + return $mockDaemonFactory; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test the creation of the default daemon. |
| 92 | + */ |
| 93 | + public function testCreateDefaultDaemon() |
| 94 | + { |
| 95 | + $input = new ArrayInput([]); |
| 96 | + $output = new NullOutput(); |
| 97 | + |
| 98 | + $mockDaemon = $this->createMockDaemon(); |
| 99 | + $mockDaemonFactory = $this->createMockDaemonFactory('createDaemon'); |
| 100 | + |
| 101 | + $mockDaemonFactory |
| 102 | + ->expects($this->once()) |
| 103 | + ->method('createDaemon') |
| 104 | + ->will($this->returnValue($mockDaemon)); |
| 105 | + |
| 106 | + $command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, function () { }); |
| 107 | + |
| 108 | + $command->run($input, $output); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Test the creation of the default daemon given the port and the host. |
| 113 | + */ |
| 114 | + public function testCreateTcpDaemonWithHost() |
| 115 | + { |
| 116 | + $host = 'localhost'; |
| 117 | + $port = 5000; |
| 118 | + |
| 119 | + $input = new ArrayInput(['--host' => $host, '--port' => $port]); |
| 120 | + $output = new NullOutput(); |
| 121 | + |
| 122 | + $mockDaemon = $this->createMockDaemon(); |
| 123 | + $mockDaemonFactory = $this->createMockDaemonFactory('createTcpDaemon'); |
| 124 | + |
| 125 | + $kernel = function () { }; |
| 126 | + |
| 127 | + $mockDaemonFactory |
| 128 | + ->expects($this->once()) |
| 129 | + ->method('createTcpDaemon') |
| 130 | + ->with($this->equalTo($kernel), $this->equalTo($port), $this->equalTo($host)) |
| 131 | + ->will($this->returnValue($mockDaemon)); |
| 132 | + |
| 133 | + $command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, $kernel); |
| 134 | + |
| 135 | + $command->run($input, $output); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Test the creation of the TCP daemon given only the port. |
| 140 | + */ |
| 141 | + public function testCreateTcpDaemonWithoutHost() |
| 142 | + { |
| 143 | + $port = 5000; |
| 144 | + |
| 145 | + $input = new ArrayInput(['--port' => $port]); |
| 146 | + $output = new NullOutput(); |
| 147 | + |
| 148 | + $mockDaemon = $this->createMockDaemon(); |
| 149 | + $mockDaemonFactory = $this->createMockDaemonFactory('createTcpDaemon'); |
| 150 | + |
| 151 | + $kernel = function () { }; |
| 152 | + |
| 153 | + $mockDaemonFactory |
| 154 | + ->expects($this->once()) |
| 155 | + ->method('createTcpDaemon') |
| 156 | + ->with($this->equalTo($kernel), $this->equalTo($port), $this->equalTo('localhost')) |
| 157 | + ->will($this->returnValue($mockDaemon)); |
| 158 | + |
| 159 | + $command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, $kernel); |
| 160 | + |
| 161 | + $command->run($input, $output); |
| 162 | + } |
| 163 | +} |
0 commit comments