Skip to content

Commit 534d94c

Browse files
committed
Add PHP Extension class that configures Service Container to use the code of it
1 parent cef45ed commit 534d94c

File tree

3 files changed

+267
-0
lines changed

3 files changed

+267
-0
lines changed

spec/ExtensionSpec.php

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
3+
namespace spec\EcomDev\PHPSpec\MagentoDiAdapter;
4+
5+
use EcomDev\PHPSpec\MagentoDiAdapter\Generator\SimplifiedDefinedClasses;
6+
use EcomDev\PHPSpec\MagentoDiAdapter\ParameterValidator;
7+
use EcomDev\PHPSpec\MagentoDiAdapter\Runner\CollaboratorMaintainer;
8+
use Magento\Framework\Code\Generator\Io;
9+
use Magento\Framework\ObjectManager\Code\Generator;
10+
use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator;
11+
use Magento\Framework\Api\Code\Generator\Mapper as MapperGenerator;
12+
use Magento\Framework\Api\Code\Generator\SearchResults;
13+
use org\bovigo\vfs\vfsStream;
14+
use org\bovigo\vfs\vfsStreamDirectory;
15+
use PhpSpec\Extension\ExtensionInterface;
16+
use PhpSpec\ObjectBehavior;
17+
use PhpSpec\ServiceContainer;
18+
use Prophecy\Argument;
19+
20+
class ExtensionSpec extends ObjectBehavior
21+
{
22+
/**
23+
* @var ServiceContainer
24+
*/
25+
private $serviceContainer;
26+
27+
function let(ServiceContainer $serviceContainer)
28+
{
29+
$this->serviceContainer = $serviceContainer;
30+
}
31+
32+
function it_should_implement_extension_interface()
33+
{
34+
$this->shouldImplement(ExtensionInterface::class);
35+
}
36+
37+
function it_creates_parameter_validator_factory_with_internal_io(Io $io)
38+
{
39+
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.io')
40+
->willReturn($io);
41+
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
42+
->willReturn(new SimplifiedDefinedClasses());
43+
44+
$validatorFactory = $this->parameterValidatorFactory();
45+
$validatorFactory->shouldImplement(\Closure::class);
46+
$parameterValidator = $validatorFactory($this->serviceContainer);
47+
$parameterValidator->shouldImplement(ParameterValidator::class);
48+
49+
// Test pre configured generators
50+
$parameterValidator->generator('ItemFactory')
51+
->shouldImplement(Generator\Factory::class);
52+
$parameterValidator->generator('ItemRepository')
53+
->shouldImplement(Generator\Repository::class);
54+
$parameterValidator->generator('ItemConverter')
55+
->shouldImplement(Generator\Converter::class);
56+
$parameterValidator->generator('ItemPersistor')
57+
->shouldImplement(Generator\Persistor::class);
58+
$parameterValidator->generator('ItemMapper')
59+
->shouldImplement(MapperGenerator::class);
60+
$parameterValidator->generator('ItemSearchResults')
61+
->shouldImplement(SearchResults::class);
62+
63+
}
64+
65+
function it_creates_internal_io_factory_with_vfs_stream()
66+
{
67+
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.vfs')
68+
->willReturn(vfsStream::setup('custom_root_dir'));
69+
70+
$factory = $this->ioFactory();
71+
$factory->shouldImplement(\Closure::class);
72+
$factory($this->serviceContainer)->shouldImplement(Io::class);
73+
}
74+
75+
function it_creates_vfs_stream_factory()
76+
{
77+
$factory = $this->vfsFactory();
78+
$factory->shouldImplement(\Closure::class);
79+
$factory()->shouldImplement(vfsStreamDirectory::class);
80+
}
81+
82+
83+
function it_creates_simplified_defined_classes_factory()
84+
{
85+
$factory = $this->simplifiedDefinedClassesFactory();
86+
$factory->shouldImplement(\Closure::class);
87+
$factory()->shouldImplement(SimplifiedDefinedClasses::class);
88+
}
89+
90+
function it_creates_collaborator_maintainer_factory(ParameterValidator $parameterValidator)
91+
{
92+
$this->serviceContainer->get('ecomdev.phpspec.magento_di_adapter.parameter_validator')
93+
->willReturn($parameterValidator)
94+
->shouldBeCalled();
95+
96+
$factory = $this->collaboratorMaintainerFactory();
97+
$factory->shouldImplement(\Closure::class);
98+
$factory($this->serviceContainer)->shouldImplement(CollaboratorMaintainer::class);
99+
}
100+
101+
function it_adds_those_factories_into_container_on_load()
102+
{
103+
$this->serviceContainer
104+
->set('ecomdev.phpspec.magento_di_adapter.code_generator.io', Argument::type(\Closure::class))
105+
->shouldBeCalled();
106+
107+
$this->serviceContainer
108+
->set('ecomdev.phpspec.magento_di_adapter.vfs', Argument::type(\Closure::class))
109+
->shouldBeCalled();
110+
111+
$this->serviceContainer
112+
->set('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes', Argument::type(\Closure::class))
113+
->shouldBeCalled();
114+
115+
116+
$this->serviceContainer
117+
->set('ecomdev.phpspec.magento_di_adapter.parameter_validator', Argument::type(\Closure::class))
118+
->shouldBeCalled();
119+
120+
$this->serviceContainer
121+
->set('runner.maintainers.ecomdev_magento_collaborator', Argument::type(\Closure::class))
122+
->shouldBeCalled();
123+
124+
$this->load($this->serviceContainer);
125+
}
126+
}

src/Extension.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace EcomDev\PHPSpec\MagentoDiAdapter;
4+
5+
use EcomDev\PHPSpec\MagentoDiAdapter\Generator\SimplifiedDefinedClasses;
6+
use EcomDev\PHPSpec\MagentoDiAdapter\Runner\CollaboratorMaintainer;
7+
use Magento\Framework\Code\Generator\Io;
8+
use Magento\Framework\Filesystem\Driver\File;
9+
use Magento\Framework\ObjectManager\Code\Generator;
10+
use Magento\Framework\ObjectManager\Profiler\Code\Generator as ProfilerGenerator;
11+
use Magento\Framework\Api\Code\Generator\Mapper as MapperGenerator;
12+
use Magento\Framework\Api\Code\Generator\SearchResults;
13+
14+
use org\bovigo\vfs\vfsStream;
15+
use PhpSpec\Extension\ExtensionInterface;
16+
use PhpSpec\ServiceContainer;
17+
18+
/**
19+
* Magento 2.0 DI adapter PHPSpec extension
20+
*
21+
* We are suppressing mess detector rules,
22+
* as it is the only way to configure PHPSpec container
23+
*
24+
* @SuppressWarnings(CouplingBetweenObjects)
25+
* @SuppressWarnings(StaticAccess)
26+
*/
27+
class Extension implements ExtensionInterface
28+
{
29+
30+
/**
31+
* Load collaborator into PHPSpec ServiceContainer
32+
*
33+
* @param ServiceContainer $container
34+
*/
35+
public function load(ServiceContainer $container)
36+
{
37+
$container->set(
38+
'ecomdev.phpspec.magento_di_adapter.vfs',
39+
$this->vfsFactory()
40+
);
41+
42+
$container->set(
43+
'ecomdev.phpspec.magento_di_adapter.code_generator.io',
44+
$this->ioFactory()
45+
);
46+
47+
$container->set(
48+
'ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes',
49+
$this->simplifiedDefinedClassesFactory()
50+
);
51+
52+
$container->set(
53+
'ecomdev.phpspec.magento_di_adapter.parameter_validator',
54+
$this->parameterValidatorFactory()
55+
);
56+
57+
$container->set(
58+
'runner.maintainers.ecomdev_magento_collaborator',
59+
$this->collaboratorMaintainerFactory()
60+
);
61+
}
62+
63+
/**
64+
* Factory for instantiation of parameter validator
65+
*
66+
* @return \Closure
67+
*/
68+
public function parameterValidatorFactory()
69+
{
70+
return function (ServiceContainer $container) {
71+
$parameterValidator = new ParameterValidator(
72+
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.io'),
73+
$container->get('ecomdev.phpspec.magento_di_adapter.code_generator.defined_classes')
74+
);
75+
76+
$parameterValidator
77+
->addGenerator(Generator\Factory::class, Generator\Factory::ENTITY_TYPE)
78+
->addGenerator(Generator\Repository::class, Generator\Repository::ENTITY_TYPE)
79+
->addGenerator(Generator\Converter::class, Generator\Converter::ENTITY_TYPE)
80+
->addGenerator(Generator\Persistor::class, Generator\Persistor::ENTITY_TYPE)
81+
->addGenerator(MapperGenerator::class, MapperGenerator::ENTITY_TYPE)
82+
->addGenerator(SearchResults::class, SearchResults::ENTITY_TYPE)
83+
;
84+
85+
return $parameterValidator;
86+
};
87+
}
88+
89+
/**
90+
* Factory for instantiating Magento code generator IO adapter
91+
*
92+
* @return \Closure
93+
*/
94+
public function ioFactory()
95+
{
96+
return function (ServiceContainer $container) {
97+
return new Io(new File(), $container->get('ecomdev.phpspec.magento_di_adapter.vfs')->url());
98+
};
99+
}
100+
101+
/**
102+
* Factory for instantiating vfs
103+
*
104+
* @return \Closure
105+
*/
106+
public function vfsFactory()
107+
{
108+
return function () {
109+
return vfsStream::setup(uniqid('ecomdev_phpspec_magento_di'));
110+
};
111+
}
112+
113+
/**
114+
* Defined classes factory
115+
*
116+
* @return \Closure
117+
*/
118+
public function simplifiedDefinedClassesFactory()
119+
{
120+
return function () {
121+
return new SimplifiedDefinedClasses();
122+
};
123+
}
124+
125+
/**
126+
* Collaborator maintainer factory
127+
*
128+
* @return \Closure
129+
*/
130+
public function collaboratorMaintainerFactory()
131+
{
132+
return function (ServiceContainer $container) {
133+
return new CollaboratorMaintainer(
134+
$container->get('ecomdev.phpspec.magento_di_adapter.parameter_validator')
135+
);
136+
};
137+
}
138+
}

src/Runner/CollaboratorMaintainer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use PhpSpec\Runner\MatcherManager;
1010
use PhpSpec\SpecificationInterface;
1111

12+
/**
13+
* Collaborator maintainer for Magento DI classes auto-generation
14+
*/
1215
class CollaboratorMaintainer implements MaintainerInterface
1316
{
1417
/**

0 commit comments

Comments
 (0)