这是我正在运行的代码:
$zf1 = $this->getModule('ZF1');
$zf1->_loadPage('POST', '/user/login', $params);
$zf1->setCookie('PHPSESSID', $zf1->grabCookie('PHPSESSID'));
输出看起来像这样:
[Page] /user/login
[Response] 200
[Request Cookies] []
[Response Headers] []
[Session] []
[Cookie Jar] []
[Cookie Jar] ["PHPSESSID=; path=/; httponly"]
它不显示 header 或 cookie。
我正在尝试测试登录该站点,获取 cookie,然后设置 cookie 以执行其他操作。
另外,使用 REST 模块测试:
$this->getModule('REST')->sendPOST('/user/login', $params);
$this->debugSection('Cookies', $this->getModule('REST')->client->getCookieJar()->all());
虽然它有 Set-Cookie header ,但它也不返回 cookie:
[Request headers] []
[Request] POST https://www.domain.com/user/login {"email":"testing@testing.com","password":"testing"}
[Response] <html> ....html goes here..... </html>
[Headers] {"Date":["Thu, 10 Dec 2015 07:29:07 GMT"],"Server":["Apache/2.4.16 (Unix) PHP/7.0.0 OpenSSL/0.9.8zg"],"X-Powered-By":["PHP/7.0.0"],"Set-Cookie":["PHPSESSID=d8ov6rpd7ggg00npe4dinubpi7; path=/"],"Expires":["Thu, 19 Nov 1981 08:52:00 GMT"],"Cache-Control":["no-store, no-cache, must-revalidate"],"Pragma":["no-cache"],"Content-Length":["866"],"Content-Type":["text/html; charset=UTF-8"]}
[Status] 200
[Cookies] [{}]
这是怎么回事?我使用的是 Codeception 版本 2.1.4 和 PHP 7。我也刚刚尝试使用 PHP 5.5,结果相同。也运行了“ Composer 更新”,但这并没有改变任何东西。
codeception.yml
actor: Tester
paths:
tests: tests
log: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
colors: true
memory_limit: 1024M
extensions:
enabled:
- Codeception\Extension\RunFailed
modules:
config:
Db:
dsn: 'mysql:host=127.0.0.1;dbname=somedb_test'
user: 'root'
password: ''
dump: tests/_data/dump.sql
populate: true
cleanup: false
reconnect: true
functional.suite.yml:
class_name: FunctionalTester
modules:
enabled:
- ZF1:
- Db
- \Helper\Functional
- REST:
depends: PhpBrowser
url: 'https://www.domain.com'
config:
ZF1:
env: testing
config: application/configs/application.ini
app_path: application
lib_path: library
更新
现在...我真的很困惑。我能够登录,但是在我尝试在我的助手中调用 _request() 之后它失去了 session ?例如,
我可以使用 $I->amOnPage(...) 并转到页面,但随后我尝试执行 _request(),请求调用不再有 session ?
它执行以下操作并清楚地记录下来但随后丢失 session ?:
$I->amOnPage('/');
$I->login();
$I->amOnPage('/user/questions');
$I->see('No active questions');
$I->sendPOST('/chat/upload', $params, $files);
sendPOST 是这样的:
$this->getModule('ZF1')->_loadPage('POST', $uri, $params, $files);
已经发布了一个类似的问题,但它与我的问题不同 ( Codeception: Keep a logged in state )
如果我用 amOnPage 替换 sendPost,它会保持 session (wtf???)
最佳答案
显然,定义一个名为 sendPOST 的辅助方法与 REST 模块中的 sendPOST 冲突,因此它实际上并未执行我的代码。我重命名它然后它开始工作。并且,向 codeception 报告:https://github.com/Codeception/Codeception/issues/2627
I had both the ZF1 and REST module enabled in my functional.suite.yml.
In my FunctionalTester helper class in \Helper\Functional.php I had a method called sendPOST. When calling sendPOST from my *Cest class it would execute the sendPOST in the REST module. After renaming sendPOST to something else it worked.
It's true I probably don't need to load both the ZF1/REST module at the same time (still new to this codeception thing), but even so it never triggered an error that there was a duplicate sendPOST method and led to confusion. Ideally it'll throw an exception about the duplicate methods.
关于php - 在 codeception 中丢失 session /cookies - 我怎样才能保持登录到我的网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196135/