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

[GH-20] Fix tests condition for detecting outside/inside code and imp… #23

Merged
merged 1 commit into from
Mar 13, 2021
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
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
},
"autoload-dev": {
"psr-4": {
"DeprecationTests\\": "tests/fixtures/src",
"Doctrine\\Foo\\": "tests/fixtures/vendor/doctrine/foo"
"DeprecationTests\\": "test_fixtures/src",
"Doctrine\\Foo\\": "test_fixtures/vendor/doctrine/foo"
}
}
}
18 changes: 9 additions & 9 deletions lib/Doctrine/Deprecations/Deprecation.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ 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
) {
return;
}

if (strpos($backtrace[1]['file'], '/vendor/' . $package . '/') !== false) {
return;
// first check that the caller is not from a tests folder, in which case we always let deprecations pass
if (strpos($backtrace[1]['file'], '/tests/') === false) {
if (strpos($backtrace[0]['file'], '/vendor/' . $package . '/') === false) {
return;
}

if (strpos($backtrace[1]['file'], '/vendor/' . $package . '/') !== false) {
return;
}
}

if (array_key_exists($link, self::$ignoredLinks)) {
Expand Down
File renamed without changes.
20 changes: 20 additions & 0 deletions test_fixtures/src/RootDeprecation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace DeprecationTests;

use Doctrine\Deprecations\Deprecation;

class RootDeprecation
{
public static function run()
{
Deprecation::triggerIfCalledFromOutside(
'doctrine/orm',
'https://github.com/doctrine/deprecations/4444',
'this is deprecated %s %d',
'foo',
1234
);

}
}
13 changes: 4 additions & 9 deletions tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\Deprecations;

use DeprecationTests\Foo;
use DeprecationTests\RootDeprecation;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\Foo\Baz;
use PHPUnit\Framework\Error\Deprecated;
Expand Down Expand Up @@ -229,24 +230,18 @@ public function testDeprecationCalledFromOutsideInRoot(): void
Deprecation::enableWithTriggerError();

$this->expectDeprecation();
$this->expectDeprecationMessage('this is deprecated foo 1234 (DeprecationTest.php');
$this->expectDeprecationMessage('this is deprecated foo 1234 (RootDeprecation.php');

$this->expectDeprecationWithIdentifier('https://github.com/doctrine/deprecations/4444');

$e = null;
try {
Deprecation::triggerIfCalledFromOutside(
'doctrine/orm',
'https://github.com/doctrine/deprecations/4444',
'this is deprecated %s %d',
'foo',
1234
);
RootDeprecation::run();

$this->fail('Should never be reached because of deprecation exception');
} catch (Throwable $e) {
$this->assertStringMatchesFormat(
'this is deprecated foo 1234 (DeprecationTest.php:%d called by TestCase.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)',
'this is deprecated foo 1234 (RootDeprecation.php:%d called by DeprecationTest.php:%d, https://github.com/doctrine/deprecations/4444, package doctrine/orm)',
$e->getMessage()
);
$this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount());
Expand Down