Sequence.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Codeception\Module;
  3. use Codeception\Module as CodeceptionModule;
  4. use Codeception\Exception\ModuleException;
  5. use Codeception\TestInterface;
  6. /**
  7. * Sequence solves data cleanup issue in alternative way.
  8. * Instead cleaning up the database between tests,
  9. * you can use generated unique names, that should not conflict.
  10. * When you create article on a site, for instance, you can assign it a unique name and then check it.
  11. *
  12. * This module has no actions, but introduces a function `sq` for generating unique sequences within test and
  13. * `sqs` for generating unique sequences across suite.
  14. *
  15. * ### Usage
  16. *
  17. * Function `sq` generates sequence, the only parameter it takes, is id.
  18. * You can get back to previously generated sequence using that id:
  19. *
  20. * ``` php
  21. * <?php
  22. * sq('post1'); // post1_521fbc63021eb
  23. * sq('post2'); // post2_521fbc6302266
  24. * sq('post1'); // post1_521fbc63021eb
  25. * ```
  26. *
  27. * Example:
  28. *
  29. * ``` php
  30. * <?php
  31. * $I->wantTo('create article');
  32. * $I->click('New Article');
  33. * $I->fillField('Title', sq('Article'));
  34. * $I->fillField('Body', 'Demo article with Lorem Ipsum');
  35. * $I->click('save');
  36. * $I->see(sq('Article') ,'#articles')
  37. * ```
  38. *
  39. * Populating Database:
  40. *
  41. * ``` php
  42. * <?php
  43. *
  44. * for ($i = 0; $i<10; $i++) {
  45. * $I->haveInDatabase('users', array('login' => sq("user$i"), 'email' => sq("user$i").'@email.com');
  46. * }
  47. * ?>
  48. * ```
  49. *
  50. * Cest Suite tests:
  51. *
  52. * ``` php
  53. * <?php
  54. * class UserTest
  55. * {
  56. * public function createUser(AcceptanceTester $I)
  57. * {
  58. * $I->createUser(sqs('user') . '@mailserver.com', sqs('login'), sqs('pwd'));
  59. * }
  60. *
  61. * public function checkEmail(AcceptanceTester $I)
  62. * {
  63. * $I->seeInEmailTo(sqs('user') . '@mailserver.com', sqs('login'));
  64. * }
  65. *
  66. * public function removeUser(AcceptanceTester $I)
  67. * {
  68. * $I->removeUser(sqs('user') . '@mailserver.com');
  69. * }
  70. * }
  71. * ?>
  72. * ```
  73. *
  74. * ### Config
  75. *
  76. * By default produces unique string with param as a prefix:
  77. *
  78. * ```
  79. * sq('user') => 'user_876asd8as87a'
  80. * ```
  81. *
  82. * This behavior can be configured using `prefix` config param.
  83. *
  84. * Old style sequences:
  85. *
  86. * ```yaml
  87. * Sequence:
  88. * prefix: '_'
  89. * ```
  90. *
  91. * Using id param inside prefix:
  92. *
  93. * ```yaml
  94. * Sequence:
  95. * prefix: '{id}.'
  96. * ```
  97. */
  98. class Sequence extends CodeceptionModule
  99. {
  100. public static $hash = [];
  101. public static $suiteHash = [];
  102. public static $prefix = '';
  103. protected $config = ['prefix' => '{id}_'];
  104. public function _initialize()
  105. {
  106. static::$prefix = $this->config['prefix'];
  107. }
  108. public function _after(TestInterface $t)
  109. {
  110. self::$hash = [];
  111. }
  112. public function _afterSuite()
  113. {
  114. self::$suiteHash = [];
  115. }
  116. }
  117. if (!function_exists('sq') && !function_exists('sqs')) {
  118. require_once __DIR__ . '/../Util/sq.php';
  119. } else {
  120. throw new ModuleException('Codeception\Module\Sequence', "function 'sq' and 'sqs' already defined");
  121. }