PageRenderTime 48ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Codeception/Lib/Friend.php

https://github.com/pmcjury/Codeception
PHP | 79 lines | 76 code | 3 blank | 0 comment | 1 complexity | 2f9925d2d64e8337e9404d22318eed21 MD5 | raw file
  1. <?php
  2. namespace Codeception\Lib;
  3. use Codeception\Actor;
  4. use Codeception\Exception\TestRuntime;
  5. use Codeception\SuiteManager;
  6. use Codeception\Lib\Interfaces\MultiSession;
  7. class Friend {
  8. protected $name;
  9. protected $actor;
  10. protected $data = [];
  11. protected $multiSessionModules = [];
  12. public function __construct($name, Actor $guy)
  13. {
  14. $this->name = $name;
  15. $this->actor = $guy;
  16. $this->multiSessionModules = array_filter(SuiteManager::$modules, function($m) {
  17. return $m instanceof Interfaces\MultiSession;
  18. });
  19. if (empty($this->multiSessionModules)) {
  20. throw new TestRuntime("No multisession modules used. Can't instantiate friend");
  21. }
  22. }
  23. public function does($closure)
  24. {
  25. $currentUserData = [];
  26. foreach ($this->multiSessionModules as $module) {
  27. $name = $module->_getName();
  28. $currentUserData[$name] = $module->_backupSessionData();
  29. if (empty($this->data)) {
  30. $module->_initializeSession();
  31. $this->data[$name] = $module->_backupSessionData();
  32. continue;
  33. }
  34. $module->_loadSessionData($this->data[$name]);
  35. };
  36. $this->actor->comment(strtoupper("<info>{$this->name} does</info>:"));
  37. $ret = $closure($this->actor);
  38. $this->actor->comment(strtoupper("<info>{$this->name} finished</info>"));
  39. foreach ($this->multiSessionModules as $module) {
  40. $name = $module->_getName();
  41. $this->data[$name] = $module->_backupSessionData();
  42. $module->_loadSessionData($currentUserData[$name]);
  43. };
  44. return $ret;
  45. }
  46. public function isGoingTo($argumentation)
  47. {
  48. $this->actor->amGoingTo($argumentation);
  49. }
  50. public function expects($prediction)
  51. {
  52. $this->actor->expect($prediction);
  53. }
  54. public function expectsTo($prediction)
  55. {
  56. $this->actor->expectTo($prediction);
  57. }
  58. public function __destruct()
  59. {
  60. foreach ($this->multiSessionModules as $module) {
  61. if (isset($this->data[$module->_getName()])) {
  62. $module->_closeSession($this->data[$module->_getName()]);
  63. }
  64. }
  65. }
  66. }