Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow final classes to be mocked #140

Merged
merged 11 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"dg/bypass-finals": "^1.0",
"drupal/coder": "^8.3",
"donatj/mock-webserver": "dev-master"
}
Expand Down
5 changes: 0 additions & 5 deletions drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ services:
arguments: ['@service_container']
tags:
- { name: drush.command }
helfi_api_base.test_commands:
class: \Drupal\helfi_api_base\Commands\TestCommands
arguments: ['@file_system']
tags:
- { name: drush.command }
helfi_api_base.locale_commands:
class: \Drupal\helfi_api_base\Commands\LocaleCommands
arguments: ['@language_manager', '@file_system', '@string_translation', '@extension.list.module']
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ parameters:
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: '#^Method Symfony\\Contracts\\EventDispatcher\\EventDispatcherInterface::dispatch\(\) invoked with 2 parameters, 1 required.#'
path: src/Commands/DeployCommands.php
count: 2
-
message: '#^Unsafe usage of new static#'
path: src/Plugin/migrate/source/HttpSourcePluginBase.php
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
<listener class="\Drupal\Tests\Listeners\DrupalListener">
</listener>
</listeners>
<extensions>
<extension class="\Drupal\helfi_api_base\BypassFinalHook"/>
</extensions>
<coverage cacheDirectory=".phpunit.cache/code-coverage" processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
Expand Down
22 changes: 22 additions & 0 deletions src/BypassFinalHook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_api_base;

use DG\BypassFinals;
use PHPUnit\Runner\BeforeTestHook;

/**
* Allow final classes to be mocked.
*/
final class BypassFinalHook implements BeforeTestHook {

/**
* {@inheritdoc}
*/
public function executeBeforeTest(string $test): void {
BypassFinals::enable();
}

}
7 changes: 4 additions & 3 deletions src/Commands/DeployCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

namespace Drupal\helfi_api_base\Commands;

use Drupal\helfi_api_base\Event\PostDeployEvent;
use Drupal\helfi_api_base\Event\PreDeployEvent;
use Drush\Attributes\Command;
use Drush\Commands\DrushCommands;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand Down Expand Up @@ -36,7 +37,7 @@ public function __construct(
*/
#[Command(name: 'helfi:post-deploy')]
public function postDeploy() : int {
$this->eventDispatcher->dispatch(new Event(), 'helfi_api_base.post_deploy');
$this->eventDispatcher->dispatch(new PostDeployEvent());
return DrushCommands::EXIT_SUCCESS;
}

Expand All @@ -50,7 +51,7 @@ public function postDeploy() : int {
*/
#[Command(name: 'helfi:pre-deploy')]
public function preDeploy() : int {
$this->eventDispatcher->dispatch(new Event(), 'helfi_api_base.pre_deploy');
$this->eventDispatcher->dispatch(new PreDeployEvent());
return DrushCommands::EXIT_SUCCESS;
}

Expand Down
44 changes: 0 additions & 44 deletions src/Commands/TestCommands.php

This file was deleted.

4 changes: 4 additions & 0 deletions src/Entity/Revision/RevisionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableStorageInterface;
use Drupal\Core\Entity\TranslatableRevisionableInterface;

/**
Expand Down Expand Up @@ -102,6 +103,7 @@ public function deleteRevisions(string $entityType, array $revisionIds) : void {

$storage = $this->entityTypeManager
->getStorage($entityType);
assert($storage instanceof RevisionableStorageInterface);

foreach ($revisionIds as $id) {
$storage->deleteRevision($id);
Expand Down Expand Up @@ -131,6 +133,8 @@ public function getRevisionsPerLanguage(
$this->assertEntityType($entityType);

$storage = $this->entityTypeManager->getStorage($entityType);
assert($storage instanceof RevisionableStorageInterface);

$definition = $this->entityTypeManager->getDefinition($entityType);

$revision_ids = $this->connection->query(
Expand Down
13 changes: 13 additions & 0 deletions src/Event/PostDeployEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_api_base\Event;

use Symfony\Contracts\EventDispatcher\Event;

/**
* Post-deploy event.
*/
final class PostDeployEvent extends Event {
}
13 changes: 13 additions & 0 deletions src/Event/PreDeployEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types = 1);

namespace Drupal\helfi_api_base\Event;

use Symfony\Contracts\EventDispatcher\Event;

/**
* Pre-deploy event.
*/
final class PreDeployEvent extends Event {
}
6 changes: 4 additions & 2 deletions src/EventSubscriber/DeployHookEventSubscriberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Drupal\helfi_api_base\EventSubscriber;

use Drupal\helfi_api_base\Event\PostDeployEvent;
use Drupal\helfi_api_base\Event\PreDeployEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\EventDispatcher\Event;

Expand Down Expand Up @@ -35,8 +37,8 @@ public function onPreDeploy(Event $event) : void {
*/
public static function getSubscribedEvents() : array {
return [
'helfi_api_base.post_deploy' => ['onPostDeploy'],
'helfi_api_base.pre_deploy' => ['onPreDeploy'],
hyrsky marked this conversation as resolved.
Show resolved Hide resolved
PostDeployEvent::class => ['onPostDeploy'],
PreDeployEvent::class => ['onPreDeploy'],
];
}

Expand Down
2 changes: 2 additions & 0 deletions tests/src/Kernel/EnvironmentResponseSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private function getResponseEvent(Request $request = NULL) : ResponseEvent {
return new ResponseEvent(
$this->container->get('http_kernel'),
$request,
// @todo Rename this once Core requires 7.0 symfony.
// @phpstan-ignore-next-line
HttpKernelInterface::MASTER_REQUEST,
new HtmlResponse()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use Drupal\helfi_api_base\Environment\EnvironmentEnum;
use Drupal\helfi_api_base\Environment\Project;
use Drupal\helfi_api_base\Event\PostDeployEvent;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\helfi_api_base\Traits\EnvironmentResolverTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\Role;
use Drupal\user\UserInterface;
use Symfony\Contracts\EventDispatcher\Event;

/**
* Tests EnsureApiAccountSubscriber.
Expand Down Expand Up @@ -76,9 +76,9 @@ public function testPasswordReset(): void {

$this->assertFalse(user_load_by_name('helfi-admin'));
// Make sure account is created if one does not exist yet.
/** @var \Drupal\helfi_api_base\EventSubscriber\EnsureApiAccountsSubscriber $service */
$service = $this->container->get('helfi_api_base.ensure_api_accounts_subscriber');
$service->onPostDeploy(new Event());
/** @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $service */
$service = $this->container->get('event_dispatcher');
$service->dispatch(new PostDeployEvent());
$account = user_load_by_name('helfi-admin');
$this->assertInstanceOf(UserInterface::class, $account);
$this->assertTrue($account->hasRole('debug_api'));
Expand All @@ -96,7 +96,7 @@ public function testPasswordReset(): void {
],
])
->save();
$service->onPostDeploy(new Event());
$service->dispatch(new PostDeployEvent());
$account = user_load_by_name('helfi-admin');
$this->assertInstanceOf(UserInterface::class, $account);
$this->assertEquals('[email protected]', $account->getEmail());
Expand Down
3 changes: 2 additions & 1 deletion tests/src/Kernel/TestLoggerTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Drupal\Tests\helfi_api_base\Kernel;

use Drupal\Core\Utility\Error;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\helfi_api_base\Traits\TestLoggerTrait;

Expand Down Expand Up @@ -49,7 +50,7 @@ public function testDoesNotExpectLogEntry() : void {
*/
public function testWatchdogException() : void {
$this->expectLogMessage('Test message', \InvalidArgumentException::class);
watchdog_exception('helfi_api_base', new \InvalidArgumentException('Test message'));
Error::logException($this->testLogger, new \InvalidArgumentException('Test message'));
}

}
7 changes: 4 additions & 3 deletions tests/src/Unit/Commands/DeployCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace Drupal\Tests\helfi_api_base\Unit\Commands;

use Drupal\helfi_api_base\Commands\DeployCommands;
use Drupal\helfi_api_base\Event\PostDeployEvent;
use Drupal\helfi_api_base\Event\PreDeployEvent;
use Drupal\Tests\UnitTestCase;
use Drush\Commands\DrushCommands;
use Prophecy\PhpUnit\ProphecyTrait;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
Expand All @@ -25,7 +26,7 @@ class DeployCommandsTest extends UnitTestCase {
*/
public function testPostDeploy() : void {
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new Event(), 'helfi_api_base.post_deploy')->shouldBeCalledTimes(1);
$eventDispatcher->dispatch(new PostDeployEvent())->shouldBeCalledTimes(1);
$sut = new DeployCommands($eventDispatcher->reveal());
$this->assertEquals(DrushCommands::EXIT_SUCCESS, $sut->postDeploy());
}
Expand All @@ -36,7 +37,7 @@ public function testPostDeploy() : void {
*/
public function testPreDeploy() : void {
$eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
$eventDispatcher->dispatch(new Event(), 'helfi_api_base.pre_deploy')->shouldBeCalledTimes(1);
$eventDispatcher->dispatch(new PreDeployEvent())->shouldBeCalledTimes(1);
$sut = new DeployCommands($eventDispatcher->reveal());
$this->assertEquals(DrushCommands::EXIT_SUCCESS, $sut->preDeploy());
}
Expand Down