Skip to content

Commit 515b268

Browse files
authored
Merge pull request #22 from smartboxgroup/smoke-tests-label
Adding label WIP to smoke tests
2 parents 88ffc0a + c87a95f commit 515b268

6 files changed

Lines changed: 209 additions & 26 deletions

File tree

Command/SmokeTestRunCommand.php

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Smartbox\CoreBundle\Command;
46

7+
use Smartbox\CoreBundle\Utils\SmokeTest\Input\SmokeTestLabel;
58
use Smartbox\CoreBundle\Utils\SmokeTest\Output\SmokeTestOutputInterface;
69
use Smartbox\CoreBundle\Utils\SmokeTest\Output\SmokeTestOutputMessage;
710
use Smartbox\CoreBundle\Utils\SmokeTest\SmokeTestInterface;
@@ -41,6 +44,17 @@ protected function configure()
4144
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
4245
'A set of tests id that will be skipped',
4346
[]
47+
)->addOption(
48+
'skipLabel',
49+
'z',
50+
InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY,
51+
'A set of tests label that will be skipped. By default all the tests with label wip will be skipped',
52+
[SmokeTestLabel::LABEL_WIP]
53+
)->addOption(
54+
'all',
55+
'a',
56+
InputOption::VALUE_NONE,
57+
'If set will runs all tests, including tests skipped by default'
4458
)
4559
->addOption('showSkipped', null, InputOption::VALUE_NONE, 'If set will show the list of skipped tests')
4660
->addOption(
@@ -91,6 +105,8 @@ protected function execute(InputInterface $in, OutputInterface $out)
91105
$json = $in->getOption('json');
92106
$output = $in->getOption('output');
93107
$test = $in->getArgument('test');
108+
$skipLabels = $in->getOption('skipLabel');
109+
$allTests = $in->getOption('all');
94110
$exitCode = 0;
95111

96112
if (!$silent && !$json) {
@@ -132,35 +148,15 @@ function ($key) use ($test) {
132148
// executes all tests (checking for label filters)
133149
\ksort($smokeTests, SORT_STRING);
134150

135-
// filter by labels
136-
if (!empty($labels)) {
137-
$filteredSmokeTests = [];
138-
139-
foreach ($smokeTests as $key => $smokeTestInfo) {
140-
if (!empty(\array_intersect($labels, $smokeTestInfo['labels']))) {
141-
$filteredSmokeTests[$key] = $smokeTestInfo;
142-
} else {
143-
$skipped[$key] = $smokeTestInfo;
144-
}
145-
}
146-
147-
$smokeTests = $filteredSmokeTests;
151+
// filter by labels and skipLabels options.
152+
// If no label was passed as parameter it will filter by skip (wip) labels
153+
if (!$allTests) {
154+
$smokeTests = $this->filterByLabels($smokeTests, $labels, $skipLabels, $skipped);
148155
}
149156

150-
// filter by skipped
157+
// filter by skiptTests (id) options
151158
if (!empty($skipTests)) {
152-
$smokeTests = \array_filter(
153-
$smokeTests,
154-
function ($smokeTestInfo) use ($skipTests, &$skipped) {
155-
$key = $smokeTestInfo['id'];
156-
$skipping = \in_array($key, $skipTests);
157-
if ($skipping) {
158-
$skipped[$key] = $smokeTestInfo;
159-
}
160-
161-
return !$skipping;
162-
}
163-
);
159+
$smokeTests = $this->filterBySkipTests($smokeTests, $skipTests, $skipped);
164160
}
165161
}
166162

@@ -341,4 +337,49 @@ public function getTestNames()
341337
{
342338
return \array_keys($this->smokeTests);
343339
}
340+
341+
/**
342+
* Filter smoke tests according to the label.
343+
*
344+
* @param array $smokeTests
345+
* @param array $labels
346+
* @param array $skipLabels
347+
* @param array $skipped
348+
*
349+
* @return array
350+
*/
351+
protected function filterByLabels(array $smokeTests, array $labels, array $skipLabels, array &$skipped): array
352+
{
353+
return \array_filter($smokeTests, function ($smokeTestInfo) use ($labels, &$skipped, $skipLabels) {
354+
$key = $smokeTestInfo['id'];
355+
if (!empty(\array_intersect($labels, $smokeTestInfo['labels'])) ||
356+
(!empty(\array_diff($smokeTestInfo['labels'], $skipLabels)) && empty($labels))
357+
) {
358+
return $smokeTestInfo;
359+
}
360+
361+
$skipped[$key] = $smokeTestInfo;
362+
});
363+
}
364+
365+
/**
366+
* Filter smoke tests according to the skipTests option.
367+
*
368+
* @param array $smokeTests
369+
* @param array $skipTests
370+
* @param array $skipped
371+
*
372+
* @return array
373+
*/
374+
protected function filterBySkipTests(array $smokeTests, array $skipTests, array &$skipped): array
375+
{
376+
return \array_filter($smokeTests, function ($smokeTestInfo) use ($skipTests, &$skipped) {
377+
$key = $smokeTestInfo['id'];
378+
if (\in_array($key, $skipTests)) {
379+
$skipped[$key] = $smokeTestInfo;
380+
}
381+
382+
return $smokeTestInfo;
383+
});
384+
}
344385
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Smartbox\CoreBundle\Tests\Command;
6+
7+
use Smartbox\CoreBundle\Utils\SmokeTest\Output\SmokeTestOutputInterface;
8+
use Smartbox\CoreBundle\Utils\SmokeTest\SmokeTestInterface;
9+
use Symfony\Bundle\FrameworkBundle\Console\Application;
10+
use Smartbox\CoreBundle\Command\SmokeTestRunCommand;
11+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
12+
use Symfony\Component\Console\Input\ArrayInput;
13+
use Symfony\Component\Console\Output\NullOutput;
14+
use Symfony\Component\Console\Tester\CommandTester;
15+
16+
/**
17+
* @group smoke-test
18+
*/
19+
class SmokeTestRunCommandTest extends KernelTestCase
20+
{
21+
public function testExecuteCommand()
22+
{
23+
$kernel = $this->createKernel();
24+
$kernel->boot();
25+
26+
$application = new Application($kernel);
27+
$application->add(new SmokeTestRunCommand());
28+
29+
$command = $application->find('smartbox:smoke-test');
30+
31+
$input = ['command' => $command->getName()];
32+
33+
$commandTester = new CommandTester($command);
34+
$commandTester->execute($input);
35+
36+
$output = $commandTester->getDisplay();
37+
38+
$this->assertInternalType('string', $output);
39+
$this->assertContains('Smoke Tests', $output);
40+
$this->assertNotContains('Error', $output);
41+
}
42+
43+
public function testSmokeTestCommandNoLabels()
44+
{
45+
$smokeTestOutput = $this->createMock(SmokeTestOutputInterface::class);
46+
$smokeTestOutput->expects($this->any())->method('isOK')->will($this->returnValue(true));
47+
$smokeTestOutput->expects($this->any())->method('getMessages')->will($this->returnValue([]));
48+
49+
$smokeTest1 = $this->createMock(SmokeTestInterface::class);
50+
$smokeTest1->expects($this->never())->method('run')->will($this->returnValue($smokeTestOutput));
51+
$smokeTest1->expects($this->never())->method('getDescription');
52+
53+
$smokeTest2 = $this->createMock(SmokeTestInterface::class);
54+
$smokeTest2->expects($this->once())->method('run')->will($this->returnValue($smokeTestOutput));
55+
$smokeTest2->expects($this->once())->method('getDescription');
56+
57+
$smokeTestRunCommand = new SmokeTestRunCommand();
58+
$smokeTestRunCommand->addTest('id1', $smokeTest1, 'run', 'getDescription', ['wip']);
59+
$smokeTestRunCommand->addTest('id2', $smokeTest2, 'run', 'getDescription', ['critical']);
60+
61+
$smokeTestRunCommand->run(new ArrayInput(['--label' => []]), new NullOutput());
62+
}
63+
64+
public function testSmokeTestCommandWipLabel()
65+
{
66+
$smokeTestOutput = $this->createMock(SmokeTestOutputInterface::class);
67+
$smokeTestOutput->expects($this->any())->method('isOK')->will($this->returnValue(true));
68+
$smokeTestOutput->expects($this->any())->method('getMessages')->will($this->returnValue([]));
69+
70+
$smokeTest1 = $this->createMock(SmokeTestInterface::class);
71+
$smokeTest1->expects($this->once())->method('run')->will($this->returnValue($smokeTestOutput));
72+
$smokeTest1->expects($this->once())->method('getDescription');
73+
74+
$smokeTest2 = $this->createMock(SmokeTestInterface::class);
75+
$smokeTest2->expects($this->never())->method('run')->will($this->returnValue($smokeTestOutput));
76+
$smokeTest2->expects($this->never())->method('getDescription');
77+
78+
$smokeTestRunCommand = new SmokeTestRunCommand();
79+
$smokeTestRunCommand->addTest('id1', $smokeTest1, 'run', 'getDescription', ['wip']);
80+
$smokeTestRunCommand->addTest('id2', $smokeTest2, 'run', 'getDescription', ['critical']);
81+
82+
$smokeTestRunCommand->run(new ArrayInput(['--label' => ['wip']]), new NullOutput());
83+
}
84+
85+
public function testSmokeTestCommandAllTests()
86+
{
87+
$smokeTestOutput = $this->createMock(SmokeTestOutputInterface::class);
88+
$smokeTestOutput->expects($this->any())->method('isOK')->will($this->returnValue(true));
89+
$smokeTestOutput->expects($this->any())->method('getMessages')->will($this->returnValue([]));
90+
91+
$smokeTest1 = $this->createMock(SmokeTestInterface::class);
92+
$smokeTest1->expects($this->once())->method('run')->will($this->returnValue($smokeTestOutput));
93+
$smokeTest1->expects($this->once())->method('getDescription');
94+
95+
$smokeTest2 = $this->createMock(SmokeTestInterface::class);
96+
$smokeTest2->expects($this->once())->method('run')->will($this->returnValue($smokeTestOutput));
97+
$smokeTest2->expects($this->once())->method('getDescription');
98+
99+
$smokeTestRunCommand = new SmokeTestRunCommand();
100+
$smokeTestRunCommand->addTest('id1', $smokeTest1, 'run', 'getDescription', ['wip']);
101+
$smokeTestRunCommand->addTest('id2', $smokeTest2, 'run', 'getDescription', ['critical']);
102+
103+
$smokeTestRunCommand->run(new ArrayInput(['--all' => true]), new NullOutput());
104+
}
105+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Smartbox\CoreBundle\Utils\SmokeTest\Input;
4+
5+
/**
6+
* Class SmokeTestLabel
7+
*/
8+
class SmokeTestLabel
9+
{
10+
/**
11+
* The label defines the critical level for smoke tests.
12+
*/
13+
const LABEL_WIP = 'wip';
14+
const LABEL_IMPORTANT = 'important';
15+
const LABEL_CRITICAL = 'critical';
16+
}

docker-compose.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: '3'
2+
3+
services:
4+
php:
5+
build: ./docker/php
6+
working_dir: /app
7+
volumes:
8+
- .:/app

docker/php/Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM composer:1 AS composer
2+
3+
FROM php:7.0-fpm
4+
5+
COPY --from=composer /usr/bin/composer /usr/bin/composer
6+
7+
RUN apt-get update && apt-get install -qq \
8+
git \
9+
unzip
10+
11+
COPY php.ini /usr/local/etc/php/php.ini

docker/php/php.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
; PHP memory limit
2+
memory_limit=-1

0 commit comments

Comments
 (0)