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

Internal: Fix PHPUnit deprecated prophecy integration #3190

Merged
merged 1 commit into from
Oct 31, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

namespace Drupal\entity_access_by_field\Tests;

use Prophecy\PhpUnit\ProphecyTrait;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\entity_access_by_field\EntityAccessHelper;
use Prophecy\Prophet;

/**
* Unit test of entity_access_by_field hook_node_access implementation.
*/
class EntityAccessTest extends UnitTestCase {

use ProphecyTrait;

/**
* The field type random machinename.
*
Expand Down Expand Up @@ -50,12 +48,34 @@ class EntityAccessTest extends UnitTestCase {
*/
protected $nodeOwnerId;

/**
* The prophecy object.
*
* @var \Prophecy\Prophet
*/
private $prophet;

/**
* Set up.
*/
protected function setUp(): void {
parent::setUp();
$this->prophet = new Prophet();
}

/**
* Tear down.
*/
protected function tearDown(): void {
$this->prophet->checkPredictions();
}
Comment on lines +69 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a good idea to do this in tear down rather than on a per-test basis? I'm not sure how reporting will differ ("There was an error in cleaning up" vs "Your test failed").

I.e. the underlying question is: Does PHPUnit already consider the test case a success if it performs tearDown? If the answer is yes, we should move this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Alexander,

Is it a good idea to do this in tear down rather than on a per-test basis? I'm not sure how reporting will differ ("There was an error in cleaning up" vs "Your test failed").

According to https://phpunit.readthedocs.io/en/9.5/fixtures.html#fixtures setUp() and tearDown() template methods are run once for each test method (and on fresh instances) of the test case class. and Example Example 4.2 has a nice explanation on how they work.

I.e. the underlying question is: Does PHPUnit already consider the test case a success if it performs tearDown? If the answer is yes, we should move this.

According to official documentation from prophecy tearDown() is where we should call checkPredictions()
Please check here for more information: https://github.com/phpspec/prophecy#predictions

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If official documentation says it, then that's good enough for me 👍


/**
* Tests the EntityAccessHelper::entityAccessCheck for Neutral Access.
*/
public function testNeutralAccess(): void {

$node = $this->prophesize(NodeInterface::class);
$node = $this->prophet->prophesize(NodeInterface::class);

$this->fieldType = $this->randomMachineName();
$fieldDefinitionInterface = $this->createMock('Drupal\Core\Field\FieldDefinitionInterface');
Expand All @@ -68,7 +88,7 @@ public function testNeutralAccess(): void {

$op = 'view';

$account = $this->prophesize(AccountInterface::class)->reveal();
$account = $this->prophet->prophesize(AccountInterface::class)->reveal();

/** @var \Drupal\node\NodeInterface $node */
/** @var \Drupal\Core\Session\AccountInterface $account */
Expand All @@ -88,7 +108,7 @@ public function testNeutralAccess(): void {
* Tests the EntityAccessHelper::entityAccessCheck for Forbidden Access.
*/
public function testForbiddenAccess(): void {
$node = $this->prophesize(NodeInterface::class);
$node = $this->prophet->prophesize(NodeInterface::class);
$node->getEntityTypeId()->willReturn('node');
$node->bundle()->willReturn('article');

Expand All @@ -115,7 +135,7 @@ public function testForbiddenAccess(): void {

$op = 'view';

$account = $this->prophesize(AccountInterface::class);
$account = $this->prophet->prophesize(AccountInterface::class);
$account->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
->willReturn(FALSE);
$account->isAuthenticated()->willReturn(TRUE);
Expand All @@ -141,7 +161,7 @@ public function testForbiddenAccess(): void {
* Tests the EntityAccessHelper::entityAccessCheck for Allowed Access.
*/
public function testAllowedAccess(): void {
$node = $this->prophesize(NodeInterface::class);
$node = $this->prophet->prophesize(NodeInterface::class);
$node->getEntityTypeId()->willReturn('node');
$node->bundle()->willReturn('article');

Expand Down Expand Up @@ -172,7 +192,7 @@ public function testAllowedAccess(): void {

$op = 'view';

$account = $this->prophesize(AccountInterface::class);
$account = $this->prophet->prophesize(AccountInterface::class);
$account->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
->willReturn(TRUE);
$account->isAuthenticated()->willReturn(TRUE);
Expand All @@ -197,7 +217,7 @@ public function testAllowedAccess(): void {
* Tests the EntityAccessHelper::entityAccessCheck for Author Access Allowed.
*/
public function testAuthorAccessAllowed(): void {
$node = $this->prophesize(NodeInterface::class);
$node = $this->prophet->prophesize(NodeInterface::class);
$node->getEntityTypeId()->willReturn('node');
$node->bundle()->willReturn('article');

Expand Down Expand Up @@ -226,7 +246,7 @@ public function testAuthorAccessAllowed(): void {

$op = 'view';

$account = $this->prophesize(AccountInterface::class);
$account = $this->prophet->prophesize(AccountInterface::class);
$account->hasPermission('view ' . $this->fieldId . ':' . $this->fieldValue . ' content')
->willReturn(FALSE);
$account->isAuthenticated()->willReturn(TRUE);
Expand Down