Skip to content

Commit

Permalink
Merge pull request #22 from doctrine/GH-20-TriggerWhenNotFromPackage
Browse files Browse the repository at this point in the history
[GH-20] Add tests for triggerIfCalledFromOutside
  • Loading branch information
beberlei authored Mar 13, 2021
2 parents 98cd98d + d4ad283 commit 7a0388d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 3 deletions.
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,11 @@
},
"autoload": {
"psr-4": {"Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations"}
},
"autoload-dev": {
"psr-4": {
"DeprecationTests\\": "tests/fixtures/src",
"Doctrine\\Foo\\": "tests/fixtures/vendor/doctrine/foo"
}
}
}
6 changes: 4 additions & 2 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public static function triggerIfCalledFromOutside(string $package, string $link,
// dependency and the caller is not a file in that package.
// When $package is installed as a root package, then this deprecation
// is always ignored
if (strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false &&
strpos($backtrace[1]['file'], '/tests/') !== false) {
if (
strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false &&
strpos($backtrace[1]['file'], '/tests/') !== false
) {
return;
}

Expand Down
41 changes: 40 additions & 1 deletion tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Doctrine\Deprecations;

use DeprecationTests\Foo;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\Foo\Baz;
use PHPUnit\Framework\Error\Deprecated;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -185,7 +187,44 @@ public function testDeprecationWithIgnoredPackage(): void
$this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations());
}

public function testDeprecationCalledFromOutside(): void
public function testDeprecationIfCalledFromOutside(): void
{
Deprecation::enableWithTriggerError();

try {
Foo::triggerDependencyWithDeprecation();

$this->fail('Not expected to get here');
} catch (Throwable $e) {
$this->assertEquals(
'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:16 called by Foo.php:14, https://github.com/doctrine/foo, package doctrine/foo)',
$e->getMessage()
);

$this->assertEquals(['https://github.com/doctrine/foo' => 1], Deprecation::getTriggeredDeprecations());
}
}

public function testDeprecationIfCalledFromOutsideNotTriggeringFromInside(): void
{
Deprecation::enableWithTriggerError();

Foo::triggerDependencyWithDeprecationFromInside();

$this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount());
}

public function testDeprecationIfCalledFromOutsideNotTriggeringFromInsideClass(): void
{
Deprecation::enableWithTriggerError();

$baz = new Baz();
$baz->usingOldFunc();

$this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount());
}

public function testDeprecationCalledFromOutsideInRoot(): void
{
Deprecation::enableWithTriggerError();

Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace DeprecationTests;

use Doctrine\Foo\Bar;

class Foo
{
public function triggerDependencyWithDeprecation(): void
{
$bar = new Bar();
$bar->oldFunc();
}

public function triggerDependencyWithDeprecationFromInside(): void
{
$bar = new Bar();
$bar->newFunc();
}
}
24 changes: 24 additions & 0 deletions tests/fixtures/vendor/doctrine/foo/Bar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Doctrine\Foo;

use Doctrine\Deprecations\Deprecation;

class Bar
{
public function oldFunc(): void
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/foo',
'https://github.com/doctrine/foo',
'Bar::oldFunc() is deprecated, use Bar::newFunc() instead.'
);
}

public function newFunc(): void
{
$this->oldFunc();
}
}
14 changes: 14 additions & 0 deletions tests/fixtures/vendor/doctrine/foo/Baz.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Doctrine\Foo;

class Baz
{
public function usingOldFunc(): void
{
$bar = new Bar();
$bar->oldFunc();
}
}

0 comments on commit 7a0388d

Please sign in to comment.