I have a node type already created on my site. I want to create a node with this node type in my PHP Unit test since I am using my database but I am getting errors. It seems like I may have to recreate the node type in the PHP Unit test first and then create it.
Is it possible to create a node without creating the node type in PHP Unit?
Here is the command I am running to run a PHP Unit test:
php ./core/scripts/run-tests.sh --verbose --php /usr/local/bin/php —-dburl mysql://drupal:drupal@mariadb/drupal --url http://nginx --class "Drupal\my_module\Tests\ExpenseTest"
This is the error:
Status Group Filename Line Function
--------------------------------------------------------------------------------
Fail Completion ExpenseTest.php 64 Drupal\my_module\Tests\ExpenseTes
The test did not complete due to a fatal error.
Pass Other ExpenseTest.php 55 Drupal\my_module\Tests\ExpenseTes
Enabled modules: node, paragraphs
Fail run-tests. Unknown 0 Unknown
FATAL Drupal\my_module\Tests\ExpenseTest: test runner returned a non-zero
error code (255).
Fail Fatal erro Unknown 0 Unknown
[17-Feb-2018 17:36:34 Australia/Sydney] PHP Fatal error: Uncaught Error:
Call to a member function referencedEntities() on null in
/var/www/html/web/modules/custom/my_module/src/Expense.php:116
Fail Fatal erro Unknown 0 Unknown
Stack trace:
Fail Fatal erro Unknown 0 Unknown
#0
/var/www/html/web/modules/custom/my_module/src/Tests/ExpenseTest.php(65):
Drupal\my_module\Expense->__construct(Object(Drupal\node\Entity\Node))
Fail Fatal erro Unknown 0 Unknown
#1 /var/www/html/web/core/modules/simpletest/src/TestBase.php(963):
Drupal\my_module\Tests\ExpenseTest->testValidation()
Fail Fatal erro Unknown 0 Unknown
#2 /var/www/html/web/core/scripts/run-tests.sh(804):
Drupal\simpletest\TestBase->run(Array)
Fail Fatal erro Unknown 0 Unknown
#3 /var/www/html/web/core/scripts/run-tests.sh(67):
simpletest_script_run_one_test('44', 'Drupal\\reo_expe...')
Fail Fatal erro Unknown 0 Unknown
#4 {main}
Fail Fatal erro Unknown 0 Unknown
thrown in /var/www/html/web/modules/custom/my_module/src/Expense.php on
line 116
/var/www/html/web $
This is my code:
<?php
namespace Drupal\my_module\Tests;
use Drupal\simpletest\WebTestBase;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\my_module\Expense;
/**
* Tests views contextual links on nodes.
*
* @group reo_expense
*/
class ExpenseTest extends WebTestBase {
/**
* Node object
*
* @var /Drupal\node\Entity\Node
*/
protected $node;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node', 'paragraphs'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->node = $this->createMyNode();
}
/**
* Tests contextual links.
*/
public function testValidation() {
$expense = new Expense($this->node);
$this->assertEqual($expense->getRegionId(), 4);
}
/**
* Create Expense node.
*/
public function createMyNode() {
// Populate defaults array.
$settings = [
'title' => 'asdfasdf',
'type' => 'expense',
'uid' => \Drupal::currentUser()->id(),
];
$node = Node::create($settings);
return $node;
}
}