I've got a form, which when I use normally works perfectly fine, but when I load the form in phpunit I get the following error :
- Drupal\digest\tests\DigestSubscriptionFormTest::testFormDisplay Exception: Warning: array_flip(): Can only flip STRING and INTEGER values! Drupal\Core\Entity\EntityStorageBase->loadMultiple()() (Line: 261)
Within buildForm
I have something like this :
$digest_storage = $this->entityTypeManager->getStorage('digest');
$digests = $digest_storage->loadMultiple();
$subscription_storage = $this->entityTypeManager->getStorage('subscription');
/** @var \Drupal\digest\Entity\DigestInterface $digest */
foreach ($digests as $digest) {
$query = $subscription_storage->getQuery();
// There should never be more than one ID returned.
$subscription_id = $query->condition('digest', $digest->id())
->condition('uid', $this->currentUser->id())
->execute();
/** @var \Drupal\digest\Entity\SubscriptionInterface $subscription_entity */
$subscription_entity = $subscription_storage->load(current($subscription_id));
$entity_exists = isset($subscription_entity);
Now when I am using this on my own it works just fine. No errors or warnings at all.
As soon as I get the page from phpunit I get the above error. Here's the test
public function testFormDisplay() {
$this->drupalLogin($this->adminUser);
// Create some basic digests to view.
$digest_storage = \Drupal::entityTypeManager()->getStorage('digest');
$digest_storage->create([
'id' => 'viewOne',
'label' => 'View One',
'enabled' => TRUE,
'title' => 'Custom Title',
'description' => 'Custom description',
'send_at' => 'Monday',
])->save();
$digest_storage->create([
'id' => 'viewTwo',
'label' => 'View Two',
'enabled' => FALSE,
'title' => 'Ghost',
'description' => 'I\'m invisible',
'send_at' => 'Friday',
])->save();
// Load the subscription form.
$this->drupalGet(Url::fromRoute('digest.digest_subscriptions'));
Adding the drupalGet
line makes the test fail. If I remove the creation of the entities it works.
I'm not only wondering how to fix this but also why? This is a functional test so it should have a full site meaning I should be able to use the entityTypeManager as normal correct? Mocking is only needed for unit and some kernel tests right?