PHPUnit Checking file exists failing I'm using a Kernel test in Drupal to add a new instance of my custom entity. In the postSave() method of my entity, I generate a PDF using TCPDF. In the browser, this all works well and I can see the generated PDF in my file system. My issue is that when I run PHPUnit from the CLI, it produces my entity fine but it can't assert that the file exists.

class CustomEntityAddTest extends EntityKernelTestBase 
{

  /**
   * A list of modules to be enabled to run this test
   *
   * @var array
   */
  public static $modules = ['block', 'datetime', 'custom_entity', 'entity_reference', 'file', 'migrate', 'options', 'system', 'taxonomy', 'text'];

  /**
   * The Directory where generated PDFs are stored
   */
  const FILES_DIRECTORY = \Drupal\custom_entity\Entity\CustomEntity::DP_DIRECTORY_BASE;

  public function setUp() {

    parent::setUp();
  }

  /**
   * Undocumented function
   *
   * @return void
   */
  public function testAdd() {

    $custom_entity = CustomEntity::create([
      'user_id' =&gt; 1,
      'status' =&gt; 0,
      'title' =&gt; 'First Test!',
      'inventco' =&gt; CustomEntity::generateSubmissionID(),
      'abstract' =&gt; 'Test abstract',
      'created' =&gt; time(),
      'changed' =&gt; time(),
    ]);

    $this-&gt;assertInstanceOf(CustomEntity::class, $custom_entity, "An entity was not created");

    $inventco = $custom_entity-&gt;get('inventco')-&gt;getString();

    // debugging
    $path = "/var/www/drupalvm/docroot/sites/default/files/private/{$inventco}/{$inventco}_frontpage.pdf";
    print_r(scandir("/var/www/drupalvm/docroot/sites/default/files/private/{$inventco}"));
    print($path);
    // Assert DP files exist
    $this-&gt;assertFileExists($path, "Generated PDF does not exist");
  }
}


How can I assert that the file was produced? I've checked that the filepath is correct.
It was because I hadn't called the save method on the $custom_entity object.

Credit and thanks to Andy