Skip to content

Commit 8f6f317

Browse files
committed
merged master
2 parents 11cb98b + cf80fd8 commit 8f6f317

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

src/Codeception/Module/Sequence.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
*
2222
* ``` php
2323
* <?php
24-
* 'post'.sq(1); // post_521fbc63021eb
25-
* 'post'.sq(2); // post_521fbc6302266
26-
* 'post'.sq(1); // post_521fbc63021eb
27-
* ?>
24+
* sq('post1'); // post1_521fbc63021eb
25+
* sq('post2'); // post2_521fbc6302266
26+
* sq('post1'); // post1_521fbc63021eb
2827
* ```
2928
*
3029
* Example:
@@ -33,11 +32,10 @@
3332
* <?php
3433
* $I->wantTo('create article');
3534
* $I->click('New Article');
36-
* $I->fillField('Title', 'Article'.sq('name'));
35+
* $I->fillField('Title', sq('Article'));
3736
* $I->fillField('Body', 'Demo article with Lorem Ipsum');
3837
* $I->click('save');
39-
* $I->see('Article'.sq('name') ,'#articles')
40-
* ?>
38+
* $I->see(sq('Article') ,'#articles')
4139
* ```
4240
*
4341
* Populating Database:
@@ -46,7 +44,7 @@
4644
* <?php
4745
*
4846
* for ($i = 0; $i<10; $i++) {
49-
* $I->haveInDatabase('users', array('login' => 'user'.sq($i), 'email' => 'user'.sq($i).'@email.com');
47+
* $I->haveInDatabase('users', array('login' => sq("user$i"), 'email' => sq("user$i").'@email.com');
5048
* }
5149
* ?>
5250
* ```
@@ -59,26 +57,58 @@
5957
* {
6058
* public function createUser(AcceptanceTester $I)
6159
* {
62-
* $I->createUser('email' . sqs('user') . '@mailserver.com', sqs('login'), sqs('pwd'));
60+
* $I->createUser(sqs('user') . '@mailserver.com', sqs('login'), sqs('pwd'));
6361
* }
6462
*
6563
* public function checkEmail(AcceptanceTester $I)
6664
* {
67-
* $I->seeInEmailTo('email' . sqs('user') . '@mailserver.com', sqs('login'));
65+
* $I->seeInEmailTo(sqs('user') . '@mailserver.com', sqs('login'));
6866
* }
6967
*
7068
* public function removeUser(AcceptanceTester $I)
7169
* {
72-
* $I->removeUser('email' . sqs('user') . '@mailserver.com');
70+
* $I->removeUser(sqs('user') . '@mailserver.com');
7371
* }
7472
* }
7573
* ?>
7674
* ```
75+
*
76+
* ### Config
77+
*
78+
* By default produces unique string with param as a prefix:
79+
*
80+
* ```
81+
* sq('user') => 'user_876asd8as87a'
82+
* ```
83+
*
84+
* This behavior can be configured using `prefix` config param.
85+
*
86+
* Old style sequences:
87+
*
88+
* ```yaml
89+
* Sequence:
90+
* prefix: '_'
91+
* ```
92+
*
93+
* Using id param inside prefix:
94+
*
95+
* ```yaml
96+
* Sequence:
97+
* prefix: '{id}.'
98+
* ```
7799
*/
78100
class Sequence extends CodeceptionModule
79101
{
80102
public static $hash = [];
81103
public static $suiteHash = [];
104+
public static $prefix = '';
105+
106+
protected $config = ['prefix' => '{id}_'];
107+
108+
public function _initialize()
109+
{
110+
static::$prefix = $this->config['prefix'];
111+
}
82112

83113
public function _after(TestInterface $t)
84114
{
@@ -93,7 +123,6 @@ public function _afterSuite()
93123

94124
if (!function_exists('sq') && !function_exists('sqs')) {
95125
require_once __DIR__ . '/../Util/sq.php';
96-
require_once __DIR__ . '/../Util/sqs.php';
97126
} else {
98127
throw new ModuleException('Codeception\Module\Sequence', "function 'sq' and 'sqs' already defined");
99128
}

src/Codeception/Util/sq.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,26 @@ function sq($id = null)
77
if ($id and isset(Sequence::$hash[$id])) {
88
return Sequence::$hash[$id];
99
}
10-
$sequence = '_' . uniqid();
10+
$prefix = str_replace('{id}', $id, Sequence::$prefix);
11+
$sequence = $prefix . uniqid($id);
1112
if ($id) {
1213
Sequence::$hash[$id] = $sequence;
1314
}
1415
return $sequence;
1516
}
1617
}
18+
19+
if (!function_exists('sqs')) {
20+
function sqs($id = null)
21+
{
22+
if ($id and isset(Sequence::$suiteHash[$id])) {
23+
return Sequence::$suiteHash[$id];
24+
}
25+
$prefix = str_replace('{id}', $id, Sequence::$prefix);
26+
$sequence = $prefix . uniqid($id);
27+
if ($id) {
28+
Sequence::$suiteHash[$id] = $sequence;
29+
}
30+
return $sequence;
31+
}
32+
}

0 commit comments

Comments
 (0)