Skip to content

Commit 87d2cdd

Browse files
committed
Merge branch 'release/0.1.1'
2 parents 70a516b + 957ab11 commit 87d2cdd

11 files changed

+78
-20
lines changed

RELEASE_NOTES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Release 0.1.0
22

3+
Added usage of PHPSpec TypeHintIndex
4+
5+
# Release 0.1.0
6+
37
Magento DI adapter for PHPSpec auto-wiring in `let()` or `it_*()` methods.
48

59
Supports such generators:

spec/ExtensionSpec.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use org\bovigo\vfs\vfsStream;
1414
use org\bovigo\vfs\vfsStreamDirectory;
1515
use PhpSpec\Extension\ExtensionInterface;
16+
use PhpSpec\Loader\Transformer\TypeHintIndex;
1617
use PhpSpec\ObjectBehavior;
1718
use PhpSpec\ServiceContainer;
1819
use Prophecy\Argument;
@@ -34,13 +35,16 @@ function it_should_implement_extension_interface()
3435
$this->shouldImplement(ExtensionInterface::class);
3536
}
3637

37-
function it_creates_parameter_validator_factory_with_internal_io(Io $io)
38+
function it_creates_parameter_validator_factory_with_internal_io(Io $io, TypeHintIndex $typeHintIndex)
3839
{
3940
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.io')
4041
->willReturn($io);
4142
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
4243
->willReturn(new SimplifiedDefinedClasses());
4344

45+
$this->serviceContainer->get('loader.transformer.typehintindex')
46+
->willReturn($typeHintIndex);
47+
4448
$validatorFactory = $this->parameterValidatorFactory();
4549
$validatorFactory->shouldImplement(\Closure::class);
4650
$parameterValidator = $validatorFactory($this->serviceContainer);

spec/Fixture/SignatureClass.php

+5
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ function non_existent_class_param(InvalidClass $parameter)
1515

1616
}
1717

18+
function type_hint_index_resolved_class(TypeHintClassFactory $parameter)
19+
{
20+
21+
}
22+
1823
}

spec/Fixture/TypeHintClass.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;
4+
5+
6+
class TypeHintClass
7+
{
8+
9+
}

spec/Fixture/ValidClass.php

-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: ivan
5-
* Date: 23/05/16
6-
* Time: 13:58
7-
*/
82

93
namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;
104

spec/Fixture/ValidInterface.php

-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: ivan
5-
* Date: 23/05/16
6-
* Time: 13:56
7-
*/
82

93
namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;
104

11-
125
interface ValidInterface
136
{
147

spec/ParameterValidatorSpec.php

+26-2
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
use Magento\Framework\Filesystem\Driver\File;
99
use org\bovigo\vfs\vfsStream;
1010
use org\bovigo\vfs\vfsStreamDirectory;
11+
use PhpSpec\Loader\Transformer\TypeHintIndex;
1112
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\Catcher;
1213
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\SignatureClass;
1314
use PhpSpec\ObjectBehavior;
1415
use Prophecy\Argument;
16+
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\TypeHintClass;
17+
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\ValidClass;
1518

1619
class ParameterValidatorSpec extends ObjectBehavior
1720
{
@@ -43,14 +46,20 @@ class ParameterValidatorSpec extends ObjectBehavior
4346
*/
4447
private $classReflection;
4548

46-
function let()
49+
/**
50+
* @var TypeHintIndex
51+
*/
52+
private $typeHintIndex;
53+
54+
function let(TypeHintIndex $typeHintIndex)
4755
{
4856
$this->vfs = vfsStream::setup('testcase');
4957
$this->io = new Io(new File(), $this->vfs->url());
5058
$this->definedClasses = new SimplifiedDefinedClasses();
5159
$this->classReflection = new \ReflectionClass(SignatureClass::class);
60+
$this->typeHintIndex = $typeHintIndex;
5261

53-
$this->beConstructedWith($this->io, $this->definedClasses);
62+
$this->beConstructedWith($this->io, $this->definedClasses, $this->typeHintIndex);
5463
}
5564

5665
function it_is_possible_to_add_multiple_entity_generators()
@@ -97,6 +106,21 @@ function it_generates_a_class_via_generator_for_parameter_that_does_not_exits()
97106
$this->shouldCreateFile($this->vfs->url() . '/spec/EcomDev/PHPSpec/MagentoDiAdapter/Fixture/ValidClassFactory.php');
98107
}
99108

109+
function it_supports_type_hint_index_method_data_retrieval()
110+
{
111+
$this->typeHintIndex->lookup(SignatureClass::class, 'type_hint_index_resolved_class', '$parameter')
112+
->willReturn(TypeHintClass::class . 'Factory')
113+
->shouldBeCalled();
114+
115+
$this->addGenerator(Generator\Factory::class, Generator\Factory::ENTITY_TYPE)->shouldReturn($this);
116+
117+
$functionReflection = $this->classReflection->getMethod('type_hint_index_resolved_class');
118+
119+
$this->validate($functionReflection)->shouldReturn($this);
120+
121+
$this->shouldCreateFile($this->vfs->url() . '/spec/EcomDev/PHPSpec/MagentoDiAdapter/Fixture/TypeHintClassFactory.php');
122+
}
123+
100124
function it_does_not_generate_a_class_for_which_we_do_not_have_a_rule()
101125
{
102126
$functionReflection = $this->classReflection->getMethod('non_existent_class_param');

spec/Runner/CollaboratorMaintainerSpec.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function it_should_support_any_example_node(ExampleNode $example)
3939

4040
function it_has_higher_priority_than_current_collaborator_maintainer()
4141
{
42-
$this->getPriority()->shouldReturn(49);
42+
$this->getPriority()->shouldReturn(51);
4343
}
4444

4545

src/Extension.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public function parameterValidatorFactory()
7070
return function (ServiceContainer $container) {
7171
$parameterValidator = new ParameterValidator(
7272
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.io'),
73-
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
73+
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes'),
74+
$container->get('loader.transformer.typehintindex')
7475
);
7576

7677
$parameterValidator

src/ParameterValidator.php

+25-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Magento\Framework\Code\Generator\DefinedClasses;
66
use Magento\Framework\Code\Generator\Io;
7+
use PhpSpec\Loader\Transformer\TypeHintIndex;
78

89
/**
910
* Validates parameters for Magento DI container
@@ -31,16 +32,25 @@ class ParameterValidator
3132
*/
3233
private $generators = [];
3334

35+
/**
36+
* Type hint index to check for tokenizer resolved classes
37+
*
38+
* @var TypeHintIndex
39+
*/
40+
private $typeHintIndex;
41+
3442
/**
3543
* Configures dependencies of parameter validator
3644
*
3745
* @param Io $generationIo
3846
* @param DefinedClasses $definedClasses
47+
* @param TypeHintIndex $typeHintIndex
3948
*/
40-
public function __construct(Io $generationIo, DefinedClasses $definedClasses)
49+
public function __construct(Io $generationIo, DefinedClasses $definedClasses, TypeHintIndex $typeHintIndex)
4150
{
4251
$this->generationIo = $generationIo;
4352
$this->definedClasses = $definedClasses;
53+
$this->typeHintIndex = $typeHintIndex;
4454
}
4555

4656
/***
@@ -126,6 +136,20 @@ private function validateParameter(\ReflectionParameter $parameter)
126136
include $generator->generate();
127137
}
128138
};
139+
140+
if (($reflectionClass = $parameter->getDeclaringClass())
141+
&& ($reflectionMethod = $parameter->getDeclaringFunction())) {
142+
$type = $this->typeHintIndex->lookup(
143+
$reflectionClass->getName(),
144+
$reflectionMethod->getName(),
145+
'$' . $parameter->getName()
146+
);
147+
148+
if ($type && !class_exists($type) && !interface_exists($type)) {
149+
$catcher($type);
150+
return $this;
151+
}
152+
}
129153

130154
spl_autoload_register($catcher);
131155
try {

src/Runner/CollaboratorMaintainer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,6 @@ public function teardown(
9393
*/
9494
public function getPriority()
9595
{
96-
return 49;
96+
return 51;
9797
}
9898
}

0 commit comments

Comments
 (0)