Skip to content

Commit

Permalink
Merge branch 'release/0.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanChepurnyi committed May 24, 2016
2 parents 70a516b + 957ab11 commit 87d2cdd
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 20 deletions.
4 changes: 4 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release 0.1.0

Added usage of PHPSpec TypeHintIndex

# Release 0.1.0

Magento DI adapter for PHPSpec auto-wiring in `let()` or `it_*()` methods.

Supports such generators:
Expand Down
6 changes: 5 additions & 1 deletion spec/ExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PhpSpec\Extension\ExtensionInterface;
use PhpSpec\Loader\Transformer\TypeHintIndex;
use PhpSpec\ObjectBehavior;
use PhpSpec\ServiceContainer;
use Prophecy\Argument;
Expand All @@ -34,13 +35,16 @@ function it_should_implement_extension_interface()
$this->shouldImplement(ExtensionInterface::class);
}

function it_creates_parameter_validator_factory_with_internal_io(Io $io)
function it_creates_parameter_validator_factory_with_internal_io(Io $io, TypeHintIndex $typeHintIndex)
{
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.io')
->willReturn($io);
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
->willReturn(new SimplifiedDefinedClasses());

$this->serviceContainer->get('loader.transformer.typehintindex')
->willReturn($typeHintIndex);

$validatorFactory = $this->parameterValidatorFactory();
$validatorFactory->shouldImplement(\Closure::class);
$parameterValidator = $validatorFactory($this->serviceContainer);
Expand Down
5 changes: 5 additions & 0 deletions spec/Fixture/SignatureClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ function non_existent_class_param(InvalidClass $parameter)

}

function type_hint_index_resolved_class(TypeHintClassFactory $parameter)
{

}

}
9 changes: 9 additions & 0 deletions spec/Fixture/TypeHintClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;


class TypeHintClass
{

}
6 changes: 0 additions & 6 deletions spec/Fixture/ValidClass.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
<?php
/**
* Created by PhpStorm.
* User: ivan
* Date: 23/05/16
* Time: 13:58
*/

namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;

Expand Down
7 changes: 0 additions & 7 deletions spec/Fixture/ValidInterface.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php
/**
* Created by PhpStorm.
* User: ivan
* Date: 23/05/16
* Time: 13:56
*/

namespace spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture;


interface ValidInterface
{

Expand Down
28 changes: 26 additions & 2 deletions spec/ParameterValidatorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
use Magento\Framework\Filesystem\Driver\File;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use PhpSpec\Loader\Transformer\TypeHintIndex;
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\Catcher;
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\SignatureClass;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\TypeHintClass;
use spec\EcomDev\PHPSpec\MagentoDiAdapter\Fixture\ValidClass;

class ParameterValidatorSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -43,14 +46,20 @@ class ParameterValidatorSpec extends ObjectBehavior
*/
private $classReflection;

function let()
/**
* @var TypeHintIndex
*/
private $typeHintIndex;

function let(TypeHintIndex $typeHintIndex)
{
$this->vfs = vfsStream::setup('testcase');
$this->io = new Io(new File(), $this->vfs->url());
$this->definedClasses = new SimplifiedDefinedClasses();
$this->classReflection = new \ReflectionClass(SignatureClass::class);
$this->typeHintIndex = $typeHintIndex;

$this->beConstructedWith($this->io, $this->definedClasses);
$this->beConstructedWith($this->io, $this->definedClasses, $this->typeHintIndex);
}

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

function it_supports_type_hint_index_method_data_retrieval()
{
$this->typeHintIndex->lookup(SignatureClass::class, 'type_hint_index_resolved_class', '$parameter')
->willReturn(TypeHintClass::class . 'Factory')
->shouldBeCalled();

$this->addGenerator(Generator\Factory::class, Generator\Factory::ENTITY_TYPE)->shouldReturn($this);

$functionReflection = $this->classReflection->getMethod('type_hint_index_resolved_class');

$this->validate($functionReflection)->shouldReturn($this);

$this->shouldCreateFile($this->vfs->url() . '/spec/EcomDev/PHPSpec/MagentoDiAdapter/Fixture/TypeHintClassFactory.php');
}

function it_does_not_generate_a_class_for_which_we_do_not_have_a_rule()
{
$functionReflection = $this->classReflection->getMethod('non_existent_class_param');
Expand Down
2 changes: 1 addition & 1 deletion spec/Runner/CollaboratorMaintainerSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function it_should_support_any_example_node(ExampleNode $example)

function it_has_higher_priority_than_current_collaborator_maintainer()
{
$this->getPriority()->shouldReturn(49);
$this->getPriority()->shouldReturn(51);
}


Expand Down
3 changes: 2 additions & 1 deletion src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public function parameterValidatorFactory()
return function (ServiceContainer $container) {
$parameterValidator = new ParameterValidator(
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.io'),
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes'),
$container->get('loader.transformer.typehintindex')
);

$parameterValidator
Expand Down
26 changes: 25 additions & 1 deletion src/ParameterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Magento\Framework\Code\Generator\DefinedClasses;
use Magento\Framework\Code\Generator\Io;
use PhpSpec\Loader\Transformer\TypeHintIndex;

/**
* Validates parameters for Magento DI container
Expand Down Expand Up @@ -31,16 +32,25 @@ class ParameterValidator
*/
private $generators = [];

/**
* Type hint index to check for tokenizer resolved classes
*
* @var TypeHintIndex
*/
private $typeHintIndex;

/**
* Configures dependencies of parameter validator
*
* @param Io $generationIo
* @param DefinedClasses $definedClasses
* @param TypeHintIndex $typeHintIndex
*/
public function __construct(Io $generationIo, DefinedClasses $definedClasses)
public function __construct(Io $generationIo, DefinedClasses $definedClasses, TypeHintIndex $typeHintIndex)
{
$this->generationIo = $generationIo;
$this->definedClasses = $definedClasses;
$this->typeHintIndex = $typeHintIndex;
}

/***
Expand Down Expand Up @@ -126,6 +136,20 @@ private function validateParameter(\ReflectionParameter $parameter)
include $generator->generate();
}
};

if (($reflectionClass = $parameter->getDeclaringClass())
&& ($reflectionMethod = $parameter->getDeclaringFunction())) {
$type = $this->typeHintIndex->lookup(
$reflectionClass->getName(),
$reflectionMethod->getName(),
'$' . $parameter->getName()
);

if ($type && !class_exists($type) && !interface_exists($type)) {
$catcher($type);
return $this;
}
}

spl_autoload_register($catcher);
try {
Expand Down
2 changes: 1 addition & 1 deletion src/Runner/CollaboratorMaintainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,6 @@ public function teardown(
*/
public function getPriority()
{
return 49;
return 51;
}
}

0 comments on commit 87d2cdd

Please sign in to comment.