|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Padam87\CronBundle\Tests; |
| 4 | + |
| 5 | +use Doctrine\Common\Annotations\AnnotationReader; |
| 6 | +use Padam87\CronBundle\Annotation\Job; |
| 7 | +use Padam87\CronBundle\Tests\Resources\Command\IrrelevantAttributeCommand; |
| 8 | +use Padam87\CronBundle\Tests\Resources\Command\OneAttributeCommand; |
| 9 | +use Padam87\CronBundle\Tests\Resources\Command\TwoAttributesCommand; |
| 10 | +use Padam87\CronBundle\Util\Helper; |
| 11 | +use PHPUnit\Framework\TestCase; |
| 12 | +use Symfony\Component\Console\Application; |
| 13 | +use Symfony\Component\Console\Command\Command; |
| 14 | +use Symfony\Component\Console\Input\InputInterface; |
| 15 | + |
| 16 | +class HelperTest extends TestCase |
| 17 | +{ |
| 18 | + private function getConfig(): array |
| 19 | + { |
| 20 | + return [ |
| 21 | + 'log_dir' => '/var/log/cron', |
| 22 | + 'variables' => [], |
| 23 | + 'php_binary' => '/usr/bin/php', |
| 24 | + ]; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @test |
| 29 | + */ |
| 30 | + public function should_register_single_job_annotaion() |
| 31 | + { |
| 32 | + $commands = [ |
| 33 | + $this->createMock(Command::class), |
| 34 | + ]; |
| 35 | + |
| 36 | + $application = $this->createMock(Application::class); |
| 37 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 38 | + |
| 39 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 40 | + $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ |
| 41 | + new Job(), |
| 42 | + ]); |
| 43 | + |
| 44 | + $helper = new Helper($application, $annotationReader); |
| 45 | + |
| 46 | + $input = $this->createMock(InputInterface::class); |
| 47 | + |
| 48 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 49 | + |
| 50 | + $this->assertCount(1, $tab->getJobs()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @test |
| 55 | + */ |
| 56 | + public function should_register_multiple_job_annotaions() |
| 57 | + { |
| 58 | + $commands = [ |
| 59 | + $this->createMock(Command::class), |
| 60 | + ]; |
| 61 | + |
| 62 | + $application = $this->createMock(Application::class); |
| 63 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 64 | + |
| 65 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 66 | + $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ |
| 67 | + new Job(), |
| 68 | + new Job(), |
| 69 | + ]); |
| 70 | + |
| 71 | + $helper = new Helper($application, $annotationReader); |
| 72 | + |
| 73 | + $input = $this->createMock(InputInterface::class); |
| 74 | + |
| 75 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 76 | + |
| 77 | + $this->assertCount(2, $tab->getJobs()); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @test |
| 82 | + */ |
| 83 | + public function should_register_job_annotaions_on_multiple_commands() |
| 84 | + { |
| 85 | + $commands = [ |
| 86 | + $this->createMock(Command::class), |
| 87 | + $this->createMock(Command::class), |
| 88 | + $this->createMock(Command::class), |
| 89 | + ]; |
| 90 | + |
| 91 | + $application = $this->createMock(Application::class); |
| 92 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 93 | + |
| 94 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 95 | + $annotationReader->expects($this->exactly(3))->method('getClassAnnotations')->willReturnOnConsecutiveCalls( |
| 96 | + [new Job(), new Job()], |
| 97 | + [new Job()], |
| 98 | + [], |
| 99 | + ); |
| 100 | + |
| 101 | + $helper = new Helper($application, $annotationReader); |
| 102 | + |
| 103 | + $input = $this->createMock(InputInterface::class); |
| 104 | + |
| 105 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 106 | + |
| 107 | + $this->assertCount(3, $tab->getJobs()); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @test |
| 112 | + */ |
| 113 | + public function should_ignore_irrelevant_annotations() |
| 114 | + { |
| 115 | + $commands = [ |
| 116 | + $this->createMock(Command::class) |
| 117 | + ]; |
| 118 | + |
| 119 | + $application = $this->createMock(Application::class); |
| 120 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 121 | + |
| 122 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 123 | + $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ |
| 124 | + new Job(), |
| 125 | + new \stdClass(), |
| 126 | + ]); |
| 127 | + |
| 128 | + $helper = new Helper($application, $annotationReader); |
| 129 | + |
| 130 | + $input = $this->createMock(InputInterface::class); |
| 131 | + |
| 132 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 133 | + |
| 134 | + $this->assertCount(1, $tab->getJobs()); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * @test |
| 139 | + * @requires PHP 8.0 |
| 140 | + */ |
| 141 | + public function should_register_single_job_attribute() |
| 142 | + { |
| 143 | + $commands = [ |
| 144 | + new OneAttributeCommand(), |
| 145 | + ]; |
| 146 | + |
| 147 | + $application = $this->createMock(Application::class); |
| 148 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 149 | + |
| 150 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 151 | + $annotationReader->expects($this->never())->method('getClassAnnotations'); |
| 152 | + |
| 153 | + $helper = new Helper($application, $annotationReader); |
| 154 | + |
| 155 | + $input = $this->createMock(InputInterface::class); |
| 156 | + |
| 157 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 158 | + |
| 159 | + $this->assertCount(1, $tab->getJobs()); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @test |
| 164 | + * @requires PHP 8.0 |
| 165 | + */ |
| 166 | + public function should_register_multiple_job_attributes() |
| 167 | + { |
| 168 | + $commands = [ |
| 169 | + new TwoAttributesCommand(), |
| 170 | + ]; |
| 171 | + |
| 172 | + $application = $this->createMock(Application::class); |
| 173 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 174 | + |
| 175 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 176 | + $annotationReader->expects($this->never())->method('getClassAnnotations'); |
| 177 | + |
| 178 | + $helper = new Helper($application, $annotationReader); |
| 179 | + |
| 180 | + $input = $this->createMock(InputInterface::class); |
| 181 | + |
| 182 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 183 | + |
| 184 | + $this->assertCount(2, $tab->getJobs()); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * @test |
| 189 | + * @requires PHP 8.0 |
| 190 | + */ |
| 191 | + public function should_register_job_attributes_on_multiple_commands() |
| 192 | + { |
| 193 | + $stdClass = new \stdClass(); |
| 194 | + $commands = [ |
| 195 | + new TwoAttributesCommand(), |
| 196 | + new OneAttributeCommand(), |
| 197 | + $stdClass, |
| 198 | + ]; |
| 199 | + |
| 200 | + $application = $this->createMock(Application::class); |
| 201 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 202 | + |
| 203 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 204 | + $annotationReader->expects($this->once())->method('getClassAnnotations') |
| 205 | + ->with(new \ReflectionClass($stdClass))->willReturn([]); |
| 206 | + |
| 207 | + $helper = new Helper($application, $annotationReader); |
| 208 | + |
| 209 | + $input = $this->createMock(InputInterface::class); |
| 210 | + |
| 211 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 212 | + |
| 213 | + $this->assertCount(3, $tab->getJobs()); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * @test |
| 218 | + * @requires PHP 8.0 |
| 219 | + */ |
| 220 | + public function should_ignore_irrelevant_attributes() |
| 221 | + { |
| 222 | + $commands = [ |
| 223 | + new IrrelevantAttributeCommand(), |
| 224 | + ]; |
| 225 | + |
| 226 | + $application = $this->createMock(Application::class); |
| 227 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 228 | + |
| 229 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 230 | + $annotationReader->expects($this->never())->method('getClassAnnotations'); |
| 231 | + |
| 232 | + $helper = new Helper($application, $annotationReader); |
| 233 | + |
| 234 | + $input = $this->createMock(InputInterface::class); |
| 235 | + |
| 236 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 237 | + |
| 238 | + $this->assertCount(1, $tab->getJobs()); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @test |
| 243 | + */ |
| 244 | + public function should_process_jobs() |
| 245 | + { |
| 246 | + $command = $this->createMock(Command::class); |
| 247 | + $command->expects($this->once())->method('getName')->willReturn('my:job'); |
| 248 | + |
| 249 | + $commands = [$command]; |
| 250 | + |
| 251 | + $application = $this->createMock(Application::class); |
| 252 | + $application->expects($this->once())->method('all')->willReturn($commands); |
| 253 | + |
| 254 | + $annotationReader = $this->createMock(AnnotationReader::class); |
| 255 | + $annotationReader->expects($this->once())->method('getClassAnnotations')->willReturn([ |
| 256 | + new Job('*', '*', '*', '*', '*', null, 'myjob.log'), |
| 257 | + ]); |
| 258 | + |
| 259 | + $helper = new Helper($application, $annotationReader); |
| 260 | + |
| 261 | + $input = $this->createMock(InputInterface::class); |
| 262 | + |
| 263 | + $tab = $helper->createTab($input, $this->getConfig()); |
| 264 | + |
| 265 | + $this->assertCount(1, $tab->getJobs()); |
| 266 | + |
| 267 | + $job = $tab->getJobs()[0]; |
| 268 | + |
| 269 | + $this->assertEquals($this->getConfig()['log_dir'] . '/myjob.log', $job->logFile); |
| 270 | + $this->assertStringStartsWith($this->getConfig()['php_binary'], $job->commandLine); |
| 271 | + $this->assertStringEndsWith('my:job', $job->commandLine); |
| 272 | + } |
| 273 | +} |
0 commit comments