I have a ParentTestCase that sets up a few common variables, and these are then failing in PHPStan with the message:
24 Call to an undefined method Mockery\LegacyMockInterface|Psr\Log\LoggerInterface::shouldReceive().
<?php
declare(strict_types=1);
namespace Tests\Action;
use DI\ContainerBuilder;
use Exception;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use Psr\Log\LoggerInterface;
use Slim\App;
use Slim\Factory\AppFactory;
class ActionTestCase extends MockeryTestCase
{
/**
* @var LoggerInterface|LegacyMockInterface|MockInterface
*/
protected $logger;
protected function setUp(): void
{
parent::setUp();
$this->logger = Mockery::mock(LoggerInterface::class);
}
/**
* @return App
* @throws Exception
*/
protected function getAppInstance(): App
{
// Instantiate PHP-DI ContainerBuilder
$containerBuilder = new ContainerBuilder();
// Set up settings
$settings = require __DIR__ . '/../../app/settings.php';
$settings($containerBuilder);
// Set up dependencies
$dependencies = require __DIR__ . '/../../app/dependencies.php';
$dependencies($containerBuilder);
// Set up repositories
$repositories = require __DIR__ . '/../../app/repositories.php';
$repositories($containerBuilder);
// Build PHP-DI Container instance
$container = $containerBuilder->build();
$container->set(LoggerInterface::class, $this->logger);
// Instantiate the app
AppFactory::setContainer($container);
$app = AppFactory::create();
// Register routes
$routes = require __DIR__ . '/../../app/routes.php';
$routes($app);
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
return $app;
}
}
<?php
declare(strict_types=1);
namespace Tests\Action\Auth;
use Tests\Action\ActionTestCase;
class AuthLinkActionTest extends ActionTestCase
{
/**
* @throws Exception
*/
public function testLinkNoParamsLogsAndRedirects(): void
{
$app = $this->getAppInstance();
$this->logger->shouldReceive('critical')->once()
->with('Authentication error: Could not link client to application')
->andReturnSelf();
$request = $this->createRequest('GET', '/auth/link');
$response = $app->handle($request);
self::assertEquals(302, $response->getStatusCode());
self::assertEquals('/', $response->getHeaderLine('Location'));
}
}
The same thing happens if I move the setUp function to the AuthLinkActionTest class.
I have found if I do the following, it works, but that defeats the point of having a "common" variable
<?php
declare(strict_types=1);
namespace Tests\Action\Auth;
use Tests\Action\ActionTestCase;
class AuthLinkActionTest extends ActionTestCase
{
/**
* @throws Exception
*/
public function testLinkNoParamsLogsAndRedirects(): void
{
$app = $this->getAppInstance();
$this->logger = Mockery::mock(LoggerInterface::class);
$this->logger->shouldReceive('critical')->once()
->with('Authentication error: Could not link client to application')
->andReturnSelf();
$request = $this->createRequest('GET', '/auth/link');
$response = $app->handle($request);
self::assertEquals(302, $response->getStatusCode());
self::assertEquals('/', $response->getHeaderLine('Location'));
}
}