#PHPUnit & Laravel - Storage facade throws Trying to access array on value of type null

14 messages · Page 1 of 1 (latest)

terse knoll
#

Hey,
I've got some now failing tests because in the FilesystemManager class, $this->app is null. If I call dd($this->app) in the test itself, it returns the right app instance but in FilesystemManager the app class is null. Anyone seen this before?

#

I must add - I've gone through the generic make sure it extends the right TestCase, make sure setUp calls parent::setUp first etc 🙂

vivid raptor
#

@terse knoll Show code.

terse knoll
#
    public function testDeletingAssetDeletesFromStorage()
    {
        $asset = Asset::factory()->create();
        Storage::partialMock()->shouldReceive('delete')->once()->andReturnTrue();
        Storage::partialMock()->shouldReceive('path')->once()->andReturn($asset->path);
        File::partialMock()->shouldReceive('exists')->once()->andReturnTrue();
        File::partialMock()->shouldReceive('files')->once()->andReturn(
            [
                $this->faker->filePath(),
                $this->faker->filePath(),
            ]
        );
        Storage::partialMock()->shouldNotReceive('deleteDirectory');
        $asset->delete();
    }
#

that's 'the test

#

the exception gets thrown from the event listener in the asset model

#
    public function testDeletingAssetDeletesFromStorage()
    {
        $asset = Asset::factory()->create();
        Storage::partialMock()->shouldReceive('delete')->once()->andReturnTrue();
        Storage::partialMock()->shouldReceive('path')->once()->andReturn($asset->path);
        File::partialMock()->shouldReceive('exists')->once()->andReturnTrue();
        File::partialMock()->shouldReceive('files')->once()->andReturn(
            [
                $this->faker->filePath(),
                $this->faker->filePath(),
            ]
        );
        Storage::partialMock()->shouldNotReceive('deleteDirectory');
        $asset->delete();
    }
#

the stack trace is:

ErrorException: Trying to access array offset on value of type null

/Users/jaetoole/Developer/nea/web-api/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:270
/Users/jaetoole/Developer/nea/web-api/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php:350
/Users/jaetoole/Developer/nea/web-api/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php:81
/Users/jaetoole/Developer/nea/web-api/vendor/laravel/framework/src/Illuminate/Filesystem/FilesystemManager.php:427
/Users/jaetoole/Developer/nea/web-api/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:338
/Users/jaetoole/Developer/nea/web-api/src/Domain/Assets/Models/Asset.php:37

banging in a dd in the FilesystemManager shows $this->app is null, but in the test it's not

vivid raptor
terse knoll
#

that's the class

#
<?php

namespace Tests;

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\Support\TenantedTest;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    use DatabaseMigrations;
    use TenantedTest;
    use WithFaker;

    protected function setUpTraits()
    {
        $uses = parent::setUpTraits();
        if (isset($uses[TenantedTest::class])) {
            $this->setupTenant();
        }
    }

    public function invokeMethod(object &$object, string $methodName, array $parameters = [])
    {
        $reflection = new \ReflectionClass(get_class($object));
        $method = $reflection->getMethod($methodName);
        $method->setAccessible(true);

        return $method->invokeArgs($object, $parameters);
    }
}

This is TestCase

vivid raptor
#

Can you add syntax highlighting, please. It’s making your code difficult to read when it’s just lines and lines of grey text.

terse knoll