Why factory is null in test form? - php

I am trying to test my form extending TypeTestCase class
class ProjectTypeTest extends TypeTestCase
{
private $entityManager;
private $securityContext;
private $translator;
private $formFactory;
protected function setUp()
{
// mock any dependencies
$this->entityManager = $this->createMock("Doctrine\ORM\EntityManagerInterface");
$this->securityContext = $this->createMock("Symfony\Component\Security\Core\SecurityContextInterface");
$this->translator = $this->createMock("Symfony\Component\Translation\TranslatorInterface");
$this->formFactory = $this->createMock("AppBundle\FormTemplate\Factory\FormFactory");
}
public function testSubmitValidData()
{
$type = new ProjectType($this->entityManager,$this->securityContext, $this->translator, $this->formFactory);
$this->factory->create($type);
}
}
But, When I called $this->factory->create($type), returns :
Call to a member function create() on null
the factory property is null !
I'am using Symfony\Component\Form\Test\TypeTestCase and also I've used use Symfony\Component\Form\Tests\Extension\Core\Type\TypeTestCase and returns the same outcome.
Or, What am I doing bad ?.
Or, How do I test a Form ?

The problem is $this->factory is not being initialized. That should be done by FormIntegrationTestCase, which is extended by TypeTestCase and then by your test class.
Your setUp() is overriding the original setUp(), which initializes $this->factory, so you should call the parent one:
protected function setUp()
{
parent::setUp();
// mock any dependencies
$this->entityManager = $this->createMock("Doctrine\ORM\EntityManagerInterface");
$this->securityContext = $this->createMock("Symfony\Component\Security\Core\SecurityContextInterface");
$this->translator = $this->createMock("Symfony\Component\Translation\TranslatorInterface");
$this->formFactory = $this->createMock("AppBundle\FormTemplate\Factory\FormFactory");
}

Related

inheritance with diffrent constructors and obejct created in parent

The class A has a constructor with some Interfaces.
The class A has a __construct where a new object $this->foo = new FooController(); will be spawned
The class B extends A but has its own __construct with only one param. calling parent::__construct() with the same params of A::__construct does not work since this changes the B::__construct
class B accesses $this->foo which was created in the constructor of class A
How create $b = new B($some_variable='foo'); without reusing all constructor variables of A::class and have the $b->foo avaliable (which was created in A::class ?
Your hints are highly appreciated.
class A extends AbstractController
{
protected FooController $foo;
protected EntityManagerInterface $manager;
protected Security $security;
protected RequestStack $requestStack;
public function __construct(EntityManagerInterface $manager, Security $security, RequestStack $requestStack)
{
$this->manager = $manager;
$this->security = $security;
$this->requestStack = $requestStack;
$this->foo = new FooController();
}
}
class B extends A
{
public function __construct($some_variable)
{
parent::__construct()
$this->foo = $some_variable;
}
}

Set properties before calling the parent constructor

In some particular situations, we need to call the parent constructor parent::__construct(); after properties initialization like the bellow code (Symfony Command code, for more details see Symfony documentation):
<?php
class CreateUserCommand extends Command
{
// ...
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager, bool $requirePassword = false)
{
// best practices recommend to call the parent constructor first and
// then set your own properties. That wouldn't work in this case
// because configure() needs the properties set in this constructor
$this->entityManager = $entityManager;
$this->requirePassword = $requirePassword;
parent::__construct();
}
I wonder how we can deal with this case if we make the Constructor in PHP 8 property promotion style :
<?php
public function __construct(private EntityManagerInterface $entityManager, private bool
$requirePassword = false)
{
parent::__construct();
}

Call to a parent class member from child without calling constructor dependencies

I have my base controller and my content controller extending it like below, and I'm getting a Call to a member function error() on null so my question is:
Do I have to call the parent constructor?
If "yes" then is it better to have a service because the parent controller has some injected dependencies that I don't want to inject myself by calling the parent constructor
use Psr\Log\LoggerInterface;
class BaseController extends AbstractController
{
/** #var LoggerInterface */
protected $logger;
/**
* BaseController constructor.
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
}
class ContentController extends BaseController
{
private $contentRepository;
private $breadcrumbService;
public function __construct(
ContentRepository $contentRepository,
BreadcrumbInterface $breadcrumbService
) {
$this->contentRepository = $contentRepository;
$this->breadcrumbService = $breadcrumbService;
}
public function contentPage(...)
{
try {
....
} catch (\Throwable $exception) {
$this->logger->error(...);
throw $exception;
}
}
You do not need to call the constructor. But you do need to set the $logger property if you want to use it.
class ContentController extends BaseController
{
private $contentRepository;
private $breadcrumbService;
public function __construct(
LoggerInterface $logger,
ContentRepository $contentRepository,
BreadcrumbInterface $breadcrumbService
) {
$this->contentRepository = $contentRepository;
$this->breadcrumbService = $breadcrumbService;
$this->logger = $logger;
}
}
With the above you no longer need to call parent::__construct(). But it's simply good practice, painless and harmless:
Just do:
public function __construct(
LoggerInterface $logger,
ContentRepository $contentRepository,
BreadcrumbInterface $breadcrumbService
) {
$this->contentRepository = $contentRepository;
$this->breadcrumbService = $breadcrumbService;
parent::__construct($logger);
}
But if the only purpose of BaseController is to provide some logging methods, it's probably better to simply inject the logger service in ContentController and use it directly.

Injecting problem - missing parent constructor call

I am trying to inject my BaseService within antoher service where I need to call my repository that I wrote in BaseService.
I think it's pretty simple thing but it marks __construct part with :
Missing parent constructor call
I made that logic in BaseService and it works
class BaseService
{
/** #var ContainerInterface */
public $container;
public $em;
public function __construct(ContainerInterface $container, EntityManagerInterface $em)
{
$this->container = $container;
$this->em = $em;
}
/**
* #return \Doctrine\Common\Persistence\ObjectRepository|\Doctrine\ORM\EntityRepository
*/
public function getMyDataRepository()
{
return $this->em->getRepository(MyData::class);
}
}
and my other service:
class DataService extends AbstractAdmin
{
public function __construct(BaseService $baseService)
{
$this->baseService = $baseService;
}
public function getTransactions(Card $card)
{
return $this->getMyDataRepository()
->createQueryBuilder('c')
->getQuery();
}
}
I found an answer.
I did it like this:
public $baseService;
public function __construct($code, $class, $baseControllerName, BaseService $baseService)
{
parent::__construct($code, $class, $baseControllerName);
$this->baseService = $baseService;
}
As Abstract Admin has its constructor.
You forgot to add parent constructor of AbstractAdmin on DataService.
class DataService extends AbstractAdmin
{
public function __construct(BaseService $baseService)
{
parent::__construct(AbstractAdmin dependencies goes here);
$this->baseService = $baseService;
}
public function getTransactions(Card $card)
{
return $this->getMyDataRepository()
->createQueryBuilder('c')
->getQuery();
}
I dont know which dependencies need your AbstractAdmin

Symfony 3 unit test form with constructor

I am trying to unit test a form which has 2 dependencies (ObjectManager and EventDispatcher)
I had tried to follow official doc but without success.
My testing file:
<?php
namespace Lch\MediaBundle\Tests\Form;
use Lch\MediaBundle\Form\AddImageType;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
class AddImageTypeTest extends TypeTestCase
{
private $entityManager;
private $eventDispatcher;
protected function setUp()
{
$this->entityManager = $this->createMock(ObjectManager::class);
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
parent::setUp();
}
protected function getExtensions()
{
$type = new AddImageType($this->entityManager, $this->eventDispatcher);
return array(
new PreloadedExtension(array($type), array()),
);
}
public function testSubmitValidData()
{
$form = $this->factory->create(AddImageType::class);
}
}
I got this error when I execute my test suite:
TypeError: Argument 1 passed to
LCH\MediaBundle\Form\AddImageType::__construct() must implement
interface Doctrine\Common\Persistence\ObjectManager, none given,
called in
/home/matthieu/www/lch/media/src/Lch/MediaBundle/vendor/symfony/symfony/src/Symfony/Component/Form/FormRegistry.php
on line 85
It seems that the job I do in the getExtensions method is not working, but cannot figure it out.
Does anyone have a clue?
ObjectManager is an interface, meaning you can't instantiate or pass it directly to other constructors.
If you are using Doctrine, replace it with Doctrine\ORM\EntityManager which implements ObjectManager interface and can be instantiated, otherwise replace it with your own implementation.
<?php
namespace Lch\MediaBundle\Tests\Form;
use Lch\MediaBundle\Form\AddImageType;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\PreloadedExtension;
use Symfony\Component\Form\Test\TypeTestCase;
class AddImageTypeTest extends TypeTestCase
{
private $entityManager;
private $eventDispatcher;
protected function setUp()
{
$this->entityManager = $this->createMock(EntityManager::class);
$this->eventDispatcher = $this->createMock(EventDispatcher::class);
parent::setUp();
}
protected function getExtensions()
{
$type = new AddImageType($this->entityManager, $this->eventDispatcher);
return array(
new PreloadedExtension(array($type), array()),
);
}
public function testSubmitValidData()
{
$form = $this->factory->create(AddImageType::class);
}
}

Categories