Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
drupal-3443205
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Issue forks
drupal-3443205
Commits
75f9e432
Verified
Commit
75f9e432
authored
7 years ago
by
Lee Rowlands
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#2935755
by alexpott, Wim Leers: Add a trait to allow dynamic setting of...
Issue
#2935755
by alexpott, Wim Leers: Add a trait to allow dynamic setting of expected deprecations
parent
b4a02309
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
core/tests/Drupal/Tests/ExpectDeprecationTest.php
+24
-0
24 additions, 0 deletions
core/tests/Drupal/Tests/ExpectDeprecationTest.php
core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
+90
-0
90 additions, 0 deletions
core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
with
114 additions
and
0 deletions
core/tests/Drupal/Tests/ExpectDeprecationTest.php
0 → 100644
+
24
−
0
View file @
75f9e432
<?php
namespace
Drupal\Tests
;
use
Drupal\Tests\Traits\ExpectDeprecationTrait
;
/**
* @coversDefaultClass \Drupal\Tests\Traits\ExpectDeprecationTrait
*
* @group Test
* @group legacy
*/
class
ExpectDeprecationTest
extends
UnitTestCase
{
use
ExpectDeprecationTrait
;
/**
* @covers ::expectDeprecation
*/
public
function
testExpectDeprecation
()
{
$this
->
expectDeprecation
(
'Test deprecation'
);
@
trigger_error
(
'Test deprecation'
,
E_USER_DEPRECATED
);
}
}
This diff is collapsed.
Click to expand it.
core/tests/Drupal/Tests/Traits/ExpectDeprecationTrait.php
0 → 100644
+
90
−
0
View file @
75f9e432
<?php
namespace
Drupal\Tests\Traits
;
use
Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListener
as
LegacySymfonyTestsListener
;
use
Symfony\Bridge\PhpUnit\SymfonyTestsListener
;
/**
* Adds the ability to dynamically set expected deprecation messages in tests.
*
* @internal
* This class should only be used by Drupal core and will be removed once
* https://github.com/symfony/symfony/pull/25757 is resolved.
*
* @todo Remove once https://github.com/symfony/symfony/pull/25757 is resolved.
*/
trait
ExpectDeprecationTrait
{
/**
* Sets an expected deprecation message.
*
* @param string $msg
* The expected deprecation message.
*/
protected
function
expectDeprecation
(
$msg
)
{
// Ensure the class or method is in the legacy group.
if
(
class_exists
(
'PHPUnit_Util_Test'
,
FALSE
))
{
$test_util
=
'PHPUnit_Util_Test'
;
$assertion_failed_error
=
'PHPUnit_Framework_AssertionFailedError'
;
}
else
{
$test_util
=
'PHPUnit\Util\Test'
;
$assertion_failed_error
=
'PHPUnit\Framework\AssertionFailedError'
;
}
$groups
=
$test_util
::
getGroups
(
get_class
(
$this
),
$this
->
getName
(
FALSE
));
if
(
!
in_array
(
'legacy'
,
$groups
,
TRUE
))
{
throw
new
$assertion_failed_error
(
'Only tests with the `@group legacy` annotation can call `setExpectedDeprecation()`.'
);
}
if
(
$trait
=
$this
->
getSymfonyTestListenerTrait
())
{
// If setting an expected deprecation there is no need to be strict about
// testing nothing as this is an assertion.
$this
->
getTestResultObject
()
->
beStrictAboutTestsThatDoNotTestAnything
(
FALSE
);
// Add the expected deprecation message to the class property.
$reflection_class
=
new
\ReflectionClass
(
$trait
);
$expected_deprecations_property
=
$reflection_class
->
getProperty
(
'expectedDeprecations'
);
$expected_deprecations_property
->
setAccessible
(
TRUE
);
$expected_deprecations
=
$expected_deprecations_property
->
getValue
(
$trait
);
$expected_deprecations
[]
=
$msg
;
$expected_deprecations_property
->
setValue
(
$trait
,
$expected_deprecations
);
// Register the error handler if necessary.
$previous_error_handler_property
=
$reflection_class
->
getProperty
(
'previousErrorHandler'
);
$previous_error_handler_property
->
setAccessible
(
TRUE
);
$previous_error_handler
=
$previous_error_handler_property
->
getValue
(
$trait
);
if
(
!
$previous_error_handler
)
{
$previous_error_handler
=
set_error_handler
([
$trait
,
'handleError'
]);
$previous_error_handler_property
->
setValue
(
$trait
,
$previous_error_handler
);
}
}
else
{
throw
new
$assertion_failed_error
(
'Can not set an expected deprecation message because the Symfony\Bridge\PhpUnit\SymfonyTestsListener is not registered as a PHPUnit test listener.'
);
}
}
/**
* Gets the SymfonyTestsListenerTrait.
*
* @return \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait|null
* The SymfonyTestsListenerTrait object or NULL is a Symfony test listener
* is not present.
*/
private
function
getSymfonyTestListenerTrait
()
{
$test_result_object
=
$this
->
getTestResultObject
();
$reflection_class
=
new
\ReflectionClass
(
$test_result_object
);
$reflection_property
=
$reflection_class
->
getProperty
(
'listeners'
);
$reflection_property
->
setAccessible
(
TRUE
);
$listeners
=
$reflection_property
->
getValue
(
$test_result_object
);
foreach
(
$listeners
as
$listener
)
{
if
(
$listener
instanceof
SymfonyTestsListener
||
$listener
instanceof
LegacySymfonyTestsListener
)
{
$reflection_class
=
new
\ReflectionClass
(
$listener
);
$reflection_property
=
$reflection_class
->
getProperty
(
'trait'
);
$reflection_property
->
setAccessible
(
TRUE
);
return
$reflection_property
->
getValue
(
$listener
);
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment