As far as I understand, Factory
and Proxy
classes are generated on the fly by the autoloader if they do not exist yet in var/generation
(see: What Triggers the Generation of a Factory in Magento 2)
But why do I get this error when referencing a new factory in a unit test?
ReflectionException: Class Magento\Framework\Api\Search\SearchCriteriaBuilderFactory does not exist
[...]/vendor/magento/framework/TestFramework/Unit/Helper/ObjectManager.php:161
use Magento\Framework\Api\Search\SearchCriteriaBuilderFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
class SearchCriteriaTest extends \PHPUnit_Framework_TestCase
{
public function testFactoryGeneration()
{
$searchCriteriaBuilderFactory = (new ObjectManager($this))->getObject(SearchCriteriaBuilderFactory::class);
}
}
I am using the bootstrap file dev/tests/unit/framework/bootstrap.php
.
Workarounds I found to generate the class:
using the real object manager (Thanks @DigitalPianism):
\Magento\Framework\App\Bootstrap::create(BP, $_SERVER)->getObjectManager()->create('\Magento\Framework\Api\Search\SearchCriteriaBuilderFactory')
run
setup:di:compile
(given the factory is referenced in a constructor)
But I still hope to find a clean and performant solution.
Also, not sure if related, but create()
of the generated factory from the unit test object manager returns null
, so I don't even have a working factory yet.
Magento\Framework\Api\Search\SearchCriteriaBuilder
?\Magento\Framework\App\Bootstrap::create(BP, $_SERVER)->getObjectManager()->create('\Magento\Framework\Api\Search\SearchCriteriaBuilderFactory');
?getObject
you callgetBuilder
? That should happen directly viagetObject
but just to test.