How can I create a node in a PHPUnit Test without having to create the node type in PHPUnit 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-&gt;__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-&gt;testValidation()
    Fail      Fatal erro Unknown              0 Unknown
        #2 /var/www/html/web/core/scripts/run-tests.sh(804):
        Drupal\simpletest\TestBase-&gt;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:

&lt;?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-&gt;node = $this-&gt;createMyNode();
  }

  /**
   * Tests contextual links.
   */
  public function testValidation() {
    $expense = new Expense($this-&gt;node);
    $this-&gt;assertEqual($expense-&gt;getRegionId(), 4);
  }

  /**
   * Create Expense node.
   */
  public function createMyNode() {
    // Populate defaults array.
    $settings = [
      'title' =&gt; 'asdfasdf',
      'type' =&gt; 'expense',
      'uid' =&gt; \Drupal::currentUser()-&gt;id(),
    ];
    $node = Node::create($settings);
    return $node;
  }
}
When you create a node, you need to reference a bundle that already exists. In your case, you are referencing a bundle (expense) that is created from a custom module, but that module is not installed.

If you look at the tests for the Node module, you will notice they always install the Node module. In the test classes that expand the NodeTestBase class, that is done from that class. In other test classes, for example the NodeFieldAccessTest class (a Kernel test), that is done directly by that test class.  

In your case, changing the list of modules to install to the following one would solve the issue.

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = ['node', 'paragraphs', 'my_module'];


From the error messages, I take the machine name of the module creating the content type is my_module. If that is not correct, replace my_module with the correct machine name.