cirrusidentity / simplesamlphp-test-utils
辅助工具,用于测试simpleSAMLphp模块
Requires
- php: >=7.2
- codeception/aspect-mock: 3.1.*
- simplesamlphp/simplesamlphp: >=1.18
Requires (Dev)
This package is auto-updated.
Last update: 2024-08-31 00:32:09 UTC
README
simplesamlphp-test-utils
辅助工具,用于测试SimpleSAMLphp模块
安装
使用composer安装为开发依赖
composer require --dev cirrusidentity/simplesamlphp-test-utils:dev-master
更新依赖项
composer update -dev cirrusidentity/simplesamlphp-test-utils
使用方法
此项目大量使用AspectMock
来简化SSP的内部测试。根据https://github.com/Codeception/AspectMock调整你的phpunit bootstrap.php,以设置AspectMock,并确保你在phpunit.xml中设置backupGlobals="false"
。查看此项目的bootstrap.php
以获取使AspectMock与SSP的自定义类加载器和谐共处的解决方案。
您可以通过在测试中调用SanityChecker::confirmAspectMockConfigured()
来验证您的项目。查看AspectMockConfiguredTest
以获取示例。
清除静态状态
您应该在每次测试后重置AspectMock
。
use AspectMock\Test as test; ... protected function tearDown() { test::clean(); // remove all registered test doubles }
SSP的几个组件也会在所有测试中缓存一些内容。其中一些类带有\SimpleSAML\Utils\ClearableState
接口,您可以在测试的适当时间清除这些状态。
模拟重定向
您的模块可能需要将用户重定向到某个地方。如果您尝试为一个重定向创建单元测试,通常会有麻烦,因为SSP的重定向方法最终会调用exit
。您可以使用内部使用AspectMock
的MockHttp
来更改重定向方法的行为,使其抛出异常。然后您可以捕获异常并断言正确的URL正在被重定向到。
// Enable throwing an exception when redirects would normally be called. MockHttp::throwOnRedirectTrustedURL(); $params = [ 'state' => '1234' ]; try { HTTP::redirectTrustedURL('http://my.url.com', $params); $this->fail('Exception expected'); } catch (RedirectException $e) { $this->assertEquals('redirectTrustedURL', $e->getMessage()); $this->assertEquals('http://my.url.com', $e->getUrl()); $this->assertEquals($params, $e->getParams()); }
模拟身份源
查看MockAuthSourceTest。bootstrap.php
需要显式加载Source.php文件。
$kernel->loadFile($projectRoot . '/vendor/simplesamlphp/simplesamlphp/lib/SimpleSAML/Auth/Source.php');