Skip to content

Commit 9a6975f

Browse files
committed
Add exclution
Add exclution Fix README Update Travis
1 parent 1781e51 commit 9a6975f

File tree

11 files changed

+187
-5
lines changed

11 files changed

+187
-5
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ before_script:
88

99
script:
1010
- bin/phpspec status
11-
- bin/phpspec -fpretty
11+
- bin/phpspec run -fpretty

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,23 @@ extensions:
2323
```bash
2424
./bin/phpspec status
2525
```
26+
27+
**Add exclusion (via phpspec.yml)**
28+
29+
```yml
30+
#phpspec.yml
31+
knp.welldone.exclusion:
32+
- "*Controller"
33+
- "App\Entity\*"
34+
35+
extensions:
36+
- Knp\PhpSpec\WellDone\Extension
37+
```
38+
39+
**Add exclusion (via command)**
40+
41+
```bash
42+
./bin/phpspec status -e "*Controller, App\Entity\*"
43+
```
44+
45+
With command, yml parameter will be overwrite.

phpspec.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
knp.welldone.exclusion: ~
12
extensions:
23
- Knp\PhpSpec\WellDone\Extension
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace spec\Knp\PhpSpec\WellDone\Locator;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Prophecy\Argument;
7+
8+
class ResourceManagerSpec extends ObjectBehavior
9+
{
10+
function it_is_initializable()
11+
{
12+
$this->shouldHaveType('Knp\PhpSpec\WellDone\Locator\ResourceManager');
13+
}
14+
}

src/Knp/PhpSpec/WellDone/Console/Command/StatusCommand.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
99
use Knp\PhpSpec\WellDone\Formater\ProgressFormater;
10+
use Symfony\Component\Console\Input\InputOption;
1011

1112
class StatusCommand extends Command
1213
{
@@ -21,12 +22,32 @@ public function __construct(Filesystem $filesystem, ProgressFormater $formater)
2122
$this->formater = $formater;
2223
}
2324

25+
public function configure()
26+
{
27+
$this
28+
->setName('status')
29+
->setDescription('Say which class has spec or not.')
30+
->setDefinition(array(
31+
new InputOption('exclude', 'e', InputOption::VALUE_REQUIRED, 'File exclusion pattern (ex : "*Controller, App\Entity\*")', null)
32+
))
33+
;
34+
}
35+
2436
protected function execute(InputInterface $input, OutputInterface $output)
2537
{
2638
$container = $this->getApplication()->getContainer();
2739
$container->configure();
2840

29-
$resources = $container->get('locator.resource_manager')->locateResources('');
41+
$exclusion = $container->getParam('knp.welldone.exclusion', '');
42+
if (null !== $input->getOption('exclude')) {
43+
$exclusion = $input->getOption('exclude');
44+
}
45+
46+
if (is_array($exclusion)) {
47+
$exclusion = implode(', ', $exclusion);
48+
}
49+
50+
$resources = $container->get('locator.resource_manager')->locateResourcesWithExclusion($exclusion);
3051

3152
foreach ($this->buildMessages($resources) as $message) {
3253
$output->writeln($message);

src/Knp/PhpSpec/WellDone/Extension.php

+12
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpSpec\Extension\ExtensionInterface;
66
use PhpSpec\ServiceContainer;
77
use Symfony\Component\Console\Input\InputArgument;
8+
use Knp\PhpSpec\WellDone\Locator\ResourceManager;
89
use Knp\PhpSpec\WellDone\Locator\NoSpecLocator;
910
use Knp\PhpSpec\WellDone\Locator\ResourceInspector;
1011
use Knp\PhpSpec\WellDone\Util\Filesystem;
@@ -41,6 +42,17 @@ protected function setupCommands(ServiceContainer $container)
4142

4243
protected function setupLocators(ServiceContainer $container)
4344
{
45+
$container->setShared('locator.resource_manager', function ($c) {
46+
$manager = new ResourceManager();
47+
48+
array_map(
49+
array($manager, 'registerLocator'),
50+
$c->getByPrefix('locator.locators')
51+
);
52+
53+
return $manager;
54+
});
55+
4456
$container->addConfigurator(function ($c) {
4557
$suites = $c->getParam('suites', array('main' => ''));
4658

src/Knp/PhpSpec/WellDone/Formater/ProgressFormater.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function buildProgressBar(array $resources)
1919
$notDone = $this->filter($resources, false);
2020

2121
$length = count($done) + count($notDone);
22-
$state = (count($done) * $this->max) / $length;
22+
$state = $length > 0 ? (count($done) * $this->max) / $length : $this->max;
2323

2424
$progress = sprintf(
2525
'<%s>%s</%s>',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Knp\PhpSpec\WellDone\Locator;
4+
5+
interface ExclusionLocatorInterface
6+
{
7+
public function findResourcesWithExclusion($query);
8+
public function supportsExclusionQuery($query);
9+
}

src/Knp/PhpSpec/WellDone/Locator/NoSpecLocator.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PhpSpec\Locator\PSR0\PSR0Resource;
77
use Knp\PhpSpec\WellDone\Locator\ResourceInspector;
88

9-
class NoSpecLocator extends PSR0Locator
9+
class NoSpecLocator extends PSR0Locator implements ExclusionLocatorInterface
1010
{
1111
public function __construct(ResourceInspector $inspector, $srcNamespace = '', $specNamespacePrefix = 'spec', $srcPath = 'src', $specPath = '.')
1212
{
@@ -20,7 +20,18 @@ public function getAllResources()
2020
return $this->findNotSpecResources($this->getFullSrcPath());
2121
}
2222

23-
protected function findNotSpecResources($path)
23+
public function findResourcesWithExclusion($query)
24+
{
25+
return $this->findNotSpecResources($this->getFullSrcPath(), $query);
26+
}
27+
28+
public function supportsExclusionQuery($query)
29+
{
30+
$query = preg_replace('/[A-Za-z_\\*]/', '', $query);
31+
return empty($query);
32+
}
33+
34+
protected function findNotSpecResources($path, $query = null)
2435
{
2536
if (!$this->getFilesystem()->pathExists($path)) {
2637
return array();
@@ -32,7 +43,9 @@ protected function findNotSpecResources($path)
3243
if ($this->inspector->isClass($resource)
3344
&& !$this->inspector->isAbstract($resource)
3445
&& !$this->inspector->hasSpec($resource)
46+
&& !$this->inspector->matchQueries($resource, $query)
3547
) {
48+
3649
$resources[] = $resource;
3750
}
3851
}

src/Knp/PhpSpec/WellDone/Locator/ResourceInspector.php

+22
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ public function isAbstract(PSR0Resource $resource)
3434
return $this->is($resource, T_ABSTRACT);
3535
}
3636

37+
public function matchQueries(PSR0Resource $resource, $queries, $delim = ',')
38+
{
39+
foreach (explode($delim, $queries) as $query) {
40+
if (true === $this->matchQuery($resource, trim($query))) {
41+
42+
return true;
43+
}
44+
}
45+
46+
return false;
47+
}
48+
49+
public function matchQuery(PSR0Resource $resource, $query)
50+
{
51+
$query = str_replace('/', '\\', $query);
52+
$query = str_replace('\\', '\\\\', $query);
53+
$query = str_replace('*', '(.*)', $query);
54+
$query = sprintf('/^%s$/', $query);
55+
56+
return 1 == preg_match($query, $resource->getSrcClassname());
57+
}
58+
3759
protected function is(PSR0Resource $resource, $tag)
3860
{
3961
$tokens = $this->filesystem->getTokens($resource->getSrcFilename());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Knp\PhpSpec\WellDone\Locator;
4+
5+
use PhpSpec\Locator\ResourceManager as BaseManager;
6+
use PhpSpec\Locator\ResourceLocatorInterface;
7+
8+
class ResourceManager extends BaseManager
9+
{
10+
protected $locators = array();
11+
12+
public function registerLocator(ResourceLocatorInterface $locator)
13+
{
14+
$this->locators[] = $locator;
15+
16+
@usort($this->locators, function ($locator1, $locator2) {
17+
return $locator2->getPriority() - $locator1->getPriority();
18+
});
19+
}
20+
21+
public function locateResources($query)
22+
{
23+
$resources = array();
24+
foreach ($this->locators as $locator) {
25+
if (empty($query)) {
26+
$resources = array_merge($resources, $locator->getAllResources());
27+
continue;
28+
}
29+
30+
if (!$locator->supportsQuery($query)) {
31+
continue;
32+
}
33+
34+
$resources = array_merge($resources, $locator->findResources($query));
35+
}
36+
37+
return array_values($resources);
38+
}
39+
40+
public function locateResourcesWithExclusion($query)
41+
{
42+
$resources = array();
43+
foreach ($this->locators as $locator) {
44+
if ($locator instanceof ExclusionLocatorInterface) {
45+
if (!$locator->supportsExclusionQuery($query)) {
46+
continue;
47+
}
48+
49+
$resources = array_merge($resources, $locator->findResourcesWithExclusion($query));
50+
} else {
51+
$resources = array_merge($resources, $locator->getAllResources());
52+
}
53+
}
54+
55+
return array_values($resources);
56+
}
57+
58+
public function createResource($classname)
59+
{
60+
foreach ($this->locators as $locator) {
61+
if ($locator->supportsClass($classname)) {
62+
return $locator->createResource($classname);
63+
}
64+
}
65+
66+
throw new RuntimeException(sprintf(
67+
'Can not find appropriate suite scope for class `%s`.', $classname
68+
));
69+
}
70+
}

0 commit comments

Comments
 (0)