PHPUnit测试中需要第三方静态方法调用
我有一个定制的SimpleSAMLphp模块类,我想用PHPUnit测试它PHPUnit测试中需要第三方静态方法调用,php,phpunit,Php,Phpunit,我有一个定制的SimpleSAMLphp模块类,我想用PHPUnit测试它 class sspmod_wcmccore_Auth_Process_EnforceAuthzPolicy extends sspmod_ldap_Auth_Process_BaseFilter { public function __construct($config, $reserved) { } /** * Enforce the authorization policy for the app
class sspmod_wcmccore_Auth_Process_EnforceAuthzPolicy extends sspmod_ldap_Auth_Process_BaseFilter {
public function __construct($config, $reserved) {
}
/**
* Enforce the authorization policy for the application.
*
* @param array &$request The current request
*/
public function process(&$request) {
assert('is_array($request)');
assert('array_key_exists("Attributes", $request)');
$entityID = $request['core:SP'];
$config = SimpleSAML_Configuration::getConfig('../authz-policies.php');
$policies = $config->getValue('policies');
if (array_key_exists($entityID, $policies)) {
// Perform some checks and set $authorized
if (!$authorized) {
$id = SimpleSAML_Auth_State::saveState($request, 'wcmccore:authz');
$url = SimpleSAML\Module::getModuleURL('wcmccore/not-authorized.php');
SimpleSAML_Utilities::redirect($url, array('StateId' => $id));
}
}
}
}
我想模拟SimpleSAML_Utilities::redirect
调用,期望它被调用一次
final class EnforceAuthzPolicyTest extends TestCase
{
public function testCallsRedirectWhenNotAuthorized(): void
{
$request = array(
'Attributes' => array(
'memberOf' => array(),
'isMemberOf' => array(),
),
'core:SP' => 'box.net'
);
$ss_util_mock = $this->createMock(SimpleSAML_Utilities::class);
$ss_util_mock->expects($this->once())
->method('redirect');
$eap = new sspmod_wcmccore_Auth_Process_EnforceAuthzPolicy(Null, Null);
$eap->process($request);
}
}
现在似乎没有调用
$ss\u util\u mock
,我知道这表明mock是“附加的”,但我不知道如何在不修改原始类的情况下实现这一点。我可以使用
<?php declare(strict_types=1);
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase;
require_once('core/lib/Auth/Process/EnforceAuthzPolicy.php');
final class EnforceAuthzPolicyTest extends TestCase {
// Use the PHPUnit Mockery integration so assertions are added
use MockeryPHPUnitIntegration;
public function tearDown(): void {
\Mockery::close();
}
public function testCallsRedirectWhenNotAuthorized(): void {
$request = array(
'Attributes' => array(
'memberOf' => array(),
'isMemberOf' => array(),
),
'core:SP' => 'box.net'
);
// Create a SimpleSAML_Utilities mock and expect it to receive ::redirect
$mock = \Mockery::mock('alias:SimpleSAML_Utilities');
$mock->shouldReceive('redirect');
// Create a new instance of the class we are testing
// and call the method that we expect to trigger the above static method call.
$enforcer = new sspmod_wcmccore_Auth_Process_EnforceAuthzPolicy(Null, Null);
$enforcer->process($request);
}
}