#PHPUnit & Laravel - Storage facade throws Trying to access array on value of type null
14 messages · Page 1 of 1 (latest)
I must add - I've gone through the generic make sure it extends the right TestCase, make sure setUp calls parent::setUp first etc 🙂
@terse knoll Show code.
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
Show the full class, so we can see what class you’re extending, etc.
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
Can you add syntax highlighting, please. It’s making your code difficult to read when it’s just lines and lines of grey text.