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? – Raphael at Digital Pianism May 24 '16 at 09:03\Magento\Framework\App\Bootstrap::create(BP, $_SERVER)->getObjectManager()->create('\Magento\Framework\Api\Search\SearchCriteriaBuilderFactory');? – Raphael at Digital Pianism May 24 '16 at 09:14getObjectyou callgetBuilder? That should happen directly viagetObjectbut just to test. – Raphael at Digital Pianism May 24 '16 at 10:31