Skip to content

Commit c61a4e5

Browse files
authored
Test suite (#15)
* Test suite * Deprecation fixes * PHP version constant check fix
1 parent d89538d commit c61a4e5

File tree

12 files changed

+398
-9
lines changed

12 files changed

+398
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "master"
7+
push:
8+
branches:
9+
- "master"
10+
11+
jobs:
12+
phpunit:
13+
name: "PHPUnit"
14+
runs-on: "ubuntu-20.04"
15+
16+
strategy:
17+
matrix:
18+
php-version:
19+
- "7.4"
20+
- "8.0"
21+
- "8.1"
22+
dependencies:
23+
- "highest"
24+
include:
25+
- php-version: "7.4"
26+
dependencies: "lowest"
27+
28+
steps:
29+
- name: "Checkout"
30+
uses: "actions/checkout@v2"
31+
with:
32+
fetch-depth: 2
33+
34+
- name: "Install PHP"
35+
uses: "shivammathur/setup-php@v2"
36+
with:
37+
php-version: "${{ matrix.php-version }}"
38+
coverage: "pcov"
39+
ini-values: "zend.assertions=1"
40+
41+
- name: "Install dependencies with Composer"
42+
uses: "ramsey/composer-install@v1"
43+
with:
44+
dependency-versions: "${{ matrix.dependencies }}"
45+
46+
- name: "Run PHPUnit"
47+
run: "vendor/bin/phpunit"

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
vendor
22
composer.lock
3+
phpunit.xml
4+
.phpunit.result.cache
5+
docker-compose.yml
6+
Dockerfile

Tests/HelperTest.php

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Padam87\CronBundle\Tests\Resources\Command;
4+
5+
use Padam87\CronBundle\Annotation\Job;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[Job]
9+
#[\stdClass]
10+
class IrrelevantAttributeCommand extends Command
11+
{
12+
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Padam87\CronBundle\Tests\Resources\Command;
4+
5+
use Padam87\CronBundle\Annotation\Job;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[Job]
9+
class OneAttributeCommand extends Command
10+
{
11+
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Padam87\CronBundle\Tests\Resources\Command;
4+
5+
use Padam87\CronBundle\Annotation\Job;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[Job]
9+
#[Job]
10+
class TwoAttributesCommand extends Command
11+
{
12+
13+
}

Tests/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
\Locale::setDefault('en');
4+
5+
if (!is_file($autoloadFile = __DIR__.'/../vendor/autoload.php')) {
6+
throw new \LogicException('Could not find autoload.php in vendor/. Did you run "composer install --dev"?');
7+
}
8+
9+
require $autoloadFile;

Util/Helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function createTab(InputInterface $input, ?array $config = null): Tab
3131

3232
$reflectionClass = new \ReflectionClass($commandInstance);
3333

34-
if (PHP_MAJOR_VERSION >= 8.0) {
34+
if (PHP_MAJOR_VERSION >= 8) {
3535
$attributes = $reflectionClass->getAttributes(Job::class);
3636

3737
if (count($attributes) > 0) {

0 commit comments

Comments
 (0)