Score:0

Kernel Test: Error: Call to a member function get() on null

cn flag

It's my first time with kernel tests.

I'm working with this test:

<?php

namespace Drupal\Tests\info_irpf_calculator\Kernel;

use Drupal\info_irpf_calculator\Model\IrpfUserData;
use Drupal\info_irpf_calculator\Model\IrpfUserDataInterface;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests the IRPF calculator.
 *
 * @group info_irpf_calculator
 *
 * @coversDefaultClass \Drupal\info_irpf_calculator\IrpfCalculator
 */
class IrpfCalculatorTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = ['info_irpf_calculator'];

  /**
   * The IRPF calculator service.
   *
   * @var \Drupal\info_irpf_calculator\IrpfCalculatorInterface
   */
  protected $irpfCalculator;

  /**
   * The IRPF specific values plugin manager.
   *
   * @var \Drupal\info_irpf_calculator\Plugin\IrpfSpecificValuesManagerInterface
   */
  protected $irpfSpecificValuesManager;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->irpfCalculator = $this->container->get('info_irpf_calculator.calculator');
    $this->irpfSpecificValuesManager = $this->container->get('plugin.manager.irpf_specific_values');
  }

  /**
   * Test values.
   *
   * @covers \Drupal\info_irpf_calculator\IrpfCalculator::getValues
   */
  public function testGetValues() {
    $user_data = new IrpfUserData([
      'age' => 50,
      'annual_income' => 12000.00,
      'is_pension' => FALSE,
      'declaration_type' => IrpfUserDataInterface::DECLARATION_TYPE_INDIVIDUAL,
      'single_parent' => FALSE,
      'descendant' => 2,
      'descendant_under_3' => 1,
    ]);

    $values = $this->irpfCalculator->getValues($user_data);
    $regions = count($this->irpfSpecificValuesManager->getDefinitions());
    $this->assertCount($regions, $values);
  }

}

When I run it: vendor/bin/phpunit --filter IrpfCalculatorTest

It give me back this error:

There was 1 error:

1) Drupal\Tests\info_irpf_calculator\Kernel\IrpfCalculatorTest::testGetValues
Error: Call to a member function get() on null

/var/www/html/web/modules/custom/info_irpf_calculator/src/Plugin/IrpfSpecificValues/AndalusiaIrpfSpecificValues.php:55
/var/www/html/web/modules/custom/info_irpf_calculator/src/Plugin/IrpfSpecificValues/IrpfSpecificValues.php:75
/var/www/html/web/modules/custom/info_irpf_calculator/src/IrpfCalculator.php:46
/var/www/html/web/modules/custom/info_irpf_calculator/tests/src/Kernel/IrpfCalculatorTest.php:63

The line in AndalusiaIrpfSpecificValues.php is:

  protected function getMinPersonal(IrpfUserDataInterface $user_data) {
    $elements = $this->taxValues->get('get_min_personal_fieldset');
    $value = $this->getMinPersonalTrait($elements, $user_data);
    return $value;
  }

$this->taxValues comes from the construct:

$this->taxValues = $this->entityTypeManager->getStorage('info_irpf_tax_values')->load(self::NAME);

The problem is that when I run the test the class doesn't load the configuration, but I use the website it works fine.

How can I make to load the configuration when I run the test?

Thanks.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.