Skip to content

Commit 073f0c7

Browse files
author
davert
committed
new module Sequence added
0 parents  commit 073f0c7

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace Codeception\Module;
3+
use Codeception\Exception\Module;
4+
use Codeception\TestCase;
5+
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.
13+
*
14+
* ### Usage
15+
*
16+
* Function `sq` generates sequence, the only parameter it takes, is id.
17+
* You can get back to previously generated sequence using that id:
18+
*
19+
* ``` php
20+
* <?php
21+
* 'post'.sq(1); // post_521fbc63021eb
22+
* 'post'.sq(2); // post_521fbc6302266
23+
* 'post'.sq(1); // post_521fbc63021eb
24+
* ?>
25+
* ```
26+
*
27+
* Example:
28+
*
29+
* ``` php
30+
* <?php
31+
* $I->wantTo('create article');
32+
* $I->click('New Article');
33+
* $I->fillField('Title', 'Article'.sq('name'));
34+
* $I->fillField('Body', 'Demo article with Lorem Ipsum');
35+
* $I->click('save');
36+
* $I->see('Article'.sq('name') ,'#articles')
37+
* ?>
38+
* ```
39+
*
40+
* Populating Database:
41+
*
42+
* ``` php
43+
* <?php
44+
*
45+
* for ($i = 0; $i<10; $i++) {
46+
* $I->haveInDatabase('users', array('login' => 'user'.sq($i), 'email' => 'user'.sq($i).'@email.com');
47+
* }
48+
* ?>
49+
* ```
50+
*
51+
*/
52+
class Sequence {
53+
54+
static $hash = array();
55+
56+
public function _after(TestCase $t)
57+
{
58+
self::$hash = array();
59+
}
60+
}
61+
62+
if (!function_exists('sq')) {
63+
require_once __DIR__.'/../Util/sq.php';
64+
} else {
65+
throw new Module('Codeception\Module\Sequence', "function 'sq' already defiend");
66+
}

src/Codeception/Util/sq.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
use \Codeception\Module\Sequence;
3+
4+
function sq($id = null)
5+
{
6+
if ($id and isset(Sequence::$hash[$id])) return Sequence::$hash[$id];
7+
$sequence = '_'.uniqid();
8+
if ($id) Sequence::$hash[$id] = $sequence;
9+
return $sequence;
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
class SequenceTest extends Codeception\TestCase\Test
4+
{
5+
// tests
6+
public function testSequences()
7+
{
8+
$module = new \Codeception\Module\Sequence();
9+
$this->assertNotEquals(sq(), sq());
10+
$this->assertNotEquals(sq(1), sq(2));
11+
$this->assertEquals(sq(1), sq(1));
12+
$old = sq(1);
13+
$module->_after($this);
14+
$this->assertNotEquals($old, sq(1));
15+
}
16+
17+
}

0 commit comments

Comments
 (0)