Skip to content

Commit

Permalink
[GH-20] Add tests for triggerIfCalledFromOutside
Browse files Browse the repository at this point in the history
  • Loading branch information
beberlei committed Mar 13, 2021
1 parent 907ac04 commit d3153a1
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 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"
}
}
}
37 changes: 37 additions & 0 deletions tests/Doctrine/Deprecations/DeprecationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,41 @@ public function testDeprecationWithIgnoredPackage(): void
$this->assertEquals(1, Deprecation::getUniqueTriggeredDeprecationsCount());
$this->assertEquals(['https://github.com/doctrine/orm/issue/1234' => 1], Deprecation::getTriggeredDeprecations());
}

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

try {
\DeprecationTests\Foo::triggerDependencyWithDeprecation();

$this->fail('Not expected to get here');
} catch (\Throwable $e) {
$this->assertEquals(
'Bar::oldFunc() is deprecated, use Bar::newFunc() instead. (Bar.php:14 called by Foo.php:12, 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();

\DeprecationTests\Foo::triggerDependencyWithDeprecationFromInside();

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

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

$baz = new \Doctrine\Foo\Baz();
$baz->usingOldFunc();

$this->assertEquals(0, Deprecation::getUniqueTriggeredDeprecationsCount());
}
}
20 changes: 20 additions & 0 deletions tests/fixtures/src/Foo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace DeprecationTests;

use Doctrine\Foo\Bar;

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

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

namespace Doctrine\Foo;

use Doctrine\Deprecations\Deprecation;

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

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

namespace Doctrine\Foo;

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

0 comments on commit d3153a1

Please sign in to comment.