概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:40:01 | 145 次阅读 | 评论: 0 | 来源: 网络整理

Symfony is designed from the ground up with code-testability in mind. In order to make your code which utilizes session easily testable we provide two separate mock storage mechanisms for both unit testing and functional testing.

Testing code using real sessions is tricky because PHP’s workflow state is global and it is not possible to have multiple concurrent sessions in the same PHP process.

The mock storage engines simulate the PHP session workflow without actually starting one allowing you to test your code without complications. You may also run multiple instances in the same PHP process.

The mock storage drivers do not read or write the system globals session_id() or session_name(). Methods are provided to simulate this if required:

Unit Testing

For unit testing where it is not necessary to persist the session, you should simply swap out the default storage engine with MockArraySessionStorage:

use SymfonyComponentHttpFoundationSessionStorageMockArraySessionStorage;
use SymfonyComponentHttpFoundationSessionSession;

$session = new Session(new MockArraySessionStorage());

Functional Testing

For functional testing where you may need to persist session data across separate PHP processes, simply change the storage engine to MockFileSessionStorage:

use SymfonyComponentHttpFoundationSessionSession;
use SymfonyComponentHttpFoundationSessionStorageMockFileSessionStorage;

$session = new Session(new MockFileSessionStorage());
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务