Skip to content

Commit

Permalink
Merge pull request #19 from doctrine/GH-17-VerifyNoDeprecations
Browse files Browse the repository at this point in the history
[GH-17] Allow verify deprecation was not triggered in test.
  • Loading branch information
beberlei authored Mar 11, 2021
2 parents 693916c + 269e9a9 commit 011e779
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/Doctrine/Deprecations/PHPUnit/VerifyDeprecations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,19 @@ trait VerifyDeprecations
/** @var array<string,int> */
private $doctrineDeprecationsExpectations = [];

/** @var array<string,int> */
private $doctrineNoDeprecationsExpectations = [];

public function expectDeprecationWithIdentifier(string $identifier): void
{
$this->doctrineDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
}

public function expectNoDeprecationWithIdentifier(string $identifier): void
{
$this->doctrineNoDeprecationsExpectations[$identifier] = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;
}

/**
* @after
*/
Expand All @@ -34,5 +42,17 @@ public function verifyDeprecationsAreTriggered(): void
)
);
}

foreach ($this->doctrineNoDeprecationsExpectations as $identifier => $expectation) {
$actualCount = Deprecation::getTriggeredDeprecations()[$identifier] ?? 0;

$this->assertTrue(
$actualCount === $expectation,
sprintf(
"Expected deprecation with identifier '%s' was triggered by code executed in test, but expected not to.",
$identifier
)
);
}
}
}
36 changes: 36 additions & 0 deletions tests/Doctrine/Deprecations/VerifyDeprecationsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Doctrine\Deprecations;

use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use PHPUnit\Framework\TestCase;

class VerifyDeprecationsTest extends TestCase
{
use VerifyDeprecations;

/**
* @before
*/
public function setUpDisableDeprecations(): void
{
// prevent PHPUnit from throwing Deprecation exception in case trigger_error was enabled before
Deprecation::disable();
}

public function testExpectDeprecationWithIdentifier(): void
{
$this->expectDeprecationWithIdentifier('http://example.com');

Deprecation::trigger('doctrine/dbal', 'http://example.com', 'message');
}

public function testExpectNoDeprecationWithIdentifier(): void
{
$this->expectNoDeprecationWithIdentifier('http://example.com');

Deprecation::trigger('doctrine/dbal', 'http://otherexample.com', 'message');
}
}

0 comments on commit 011e779

Please sign in to comment.