This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
Fixes order in which default initializers are injected #119
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a9724df
Reproduce issue #100 without mvc and modulemanager dependancies
Slamdunk 707f6a6
Merge pull request #117 from Slamdunk/testcase/issue-100
weierophinney 81c5d3f
Fixes order in which default initializers are injected
weierophinney 83ab4fe
Incorporated feedback
weierophinney 0365eab
Refactor tests
weierophinney 6e7d469
Ensure injectFactory is called before callElementInit in all versions
weierophinney b839697
Use `{@inheritDoc}` on `configure()` override
weierophinney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
/** | ||
* @link http://github.com/zendframework/zend-form for the canonical source repository | ||
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Form\Integration; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase; | ||
use Prophecy\Argument; | ||
use Zend\Form\Element; | ||
use Zend\Form\Form; | ||
use Zend\Form\FormElementManager; | ||
use Zend\Form\FormElementManagerFactory; | ||
use Zend\ServiceManager\Config; | ||
use Zend\ServiceManager\InitializerInterface; | ||
use Zend\ServiceManager\ServiceManager; | ||
|
||
class ServiceManagerTest extends TestCase | ||
{ | ||
public function testInitInitializerShouldBeCalledAfterAllOtherInitializers() | ||
{ | ||
// Reproducing the behaviour of a full stack MVC + ModuleManager | ||
$serviceManagerConfig = new Config([ | ||
'factories' => [ | ||
'FormElementManager' => FormElementManagerFactory::class, | ||
], | ||
]); | ||
|
||
$serviceManager = new ServiceManager(); | ||
$serviceManagerConfig->configureServiceManager($serviceManager); | ||
|
||
$formElementManager = $serviceManager->get('FormElementManager'); | ||
|
||
$test = 0; | ||
$spy = function () use (&$test) { | ||
TestCase::assertEquals(1, $test); | ||
}; | ||
|
||
$element = $this->prophesize(Element::class); | ||
$element->init()->will($spy); | ||
|
||
$initializer = $this->prophesize(InitializerInterface::class); | ||
$incrementTest = function () use (&$test) { | ||
$test += 1; | ||
}; | ||
|
||
if (method_exists($serviceManager, 'configure')) { | ||
$initializer->__invoke( | ||
$serviceManager, | ||
$element->reveal() | ||
)->will($incrementTest)->shouldBeCalled(); | ||
} else { | ||
$initializer->initialize( | ||
$element->reveal(), | ||
$formElementManager | ||
)->will($incrementTest)->shouldBeCalled(); | ||
} | ||
|
||
$formElementManagerConfig = new Config([ | ||
'factories' => [ | ||
'InitializableElement' => function () use ($element) { | ||
return $element->reveal(); | ||
}, | ||
], | ||
'initializers' => [ | ||
$initializer->reveal(), | ||
], | ||
]); | ||
|
||
$formElementManagerConfig->configureServiceManager($formElementManager); | ||
|
||
$formElementManager->get('InitializableElement'); | ||
} | ||
|
||
public function testInjectFactoryInitializerShouldTriggerBeforeInitInitializer() | ||
{ | ||
// Reproducing the behaviour of a full stack MVC + ModuleManager | ||
$serviceManagerConfig = new Config([ | ||
'factories' => [ | ||
'FormElementManager' => FormElementManagerFactory::class, | ||
], | ||
]); | ||
|
||
$serviceManager = new ServiceManager(); | ||
$serviceManagerConfig->configureServiceManager($serviceManager); | ||
|
||
$formElementManager = $serviceManager->get('FormElementManager'); | ||
|
||
$initializer = $this->prophesize(InitializerInterface::class); | ||
$formElementManagerAssertion = function ($form) use ($formElementManager) { | ||
TestCase::assertInstanceOf(Form::class, $form); | ||
TestCase::assertSame($formElementManager, $form->getFormFactory()->getFormElementManager()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because |
||
return true; | ||
}; | ||
if (method_exists($serviceManager, 'configure')) { | ||
$initializer->__invoke( | ||
$serviceManager, | ||
Argument::that($formElementManagerAssertion) | ||
)->shouldBeCalled(); | ||
} else { | ||
$initializer->initialize( | ||
Argument::that($formElementManagerAssertion), | ||
$formElementManager | ||
)->shouldBeCalled(); | ||
} | ||
|
||
$formElementManagerConfig = new Config([ | ||
'factories' => [ | ||
'MyForm' => function () { | ||
return new TestAsset\Form(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Ocramius Had to use a test asset here. When I tried mocking, I was hitting out-of-memory errors. Worked fine for the initializers, but not for the form. |
||
}, | ||
], | ||
'initializers' => [ | ||
$initializer->reveal(), | ||
], | ||
]); | ||
|
||
$formElementManagerConfig->configureServiceManager($formElementManager); | ||
|
||
/** @var TestAsset\Form */ | ||
$form = $formElementManager->get('MyForm'); | ||
$this->assertSame($formElementManager, $form->elementManagerAtInit); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
/** | ||
* @link http://github.com/zendframework/zend-form for the canonical source repository | ||
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Form\Integration\TestAsset; | ||
|
||
use Zend\Form\Form as BaseForm; | ||
|
||
class Form extends BaseForm | ||
{ | ||
/** | ||
* @param null|\Zend\Form\FormElementManager | ||
*/ | ||
public $elementManagerAtInit; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function init() | ||
{ | ||
$this->elementManagerAtInit = $this->getFormFactory()->getFormElementManager(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
copyright wording inconsistency