-
Notifications
You must be signed in to change notification settings - Fork 4
/
GeneratedValueTest.php
61 lines (55 loc) · 1.8 KB
/
GeneratedValueTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
class GeneratedValueTest extends PHPUnit_Framework_TestCase
{
/**
* Since these values are deterministic and sequential,
* we need to keep track of the state of the current test.
*/
private $nextCode = 1;
public function testDistinctGeneratedValue()
{
$customerCode = $this->generateNewCustomerCode();
$otherCustomerCode = $this->generateNewCustomerCode();
$this->assertNotEquals($customerCode, $otherCustomerCode);
}
/**
* Returns the current next value, then increments it.
* @return int
*/
private function generateNewCustomerCode()
{
return $this->nextCode++;
}
public function testRandomGeneratedValue()
{
$customerCode = $this->generateNewRandomCustomerCode();
$otherCustomerCode = $this->generateNewRandomCustomerCode();
$this->assertNotEquals($customerCode, $otherCustomerCode);
}
/**
* Randomly generated values may be very large, so it's better to limit
* the range.
* @return int
*/
private function generateNewRandomCustomerCode()
{
return rand(1, 10000);
}
public function testRelatedGeneratedValue()
{
$customerCode = $this->generateNewRelatedCustomerCode();
$otherCustomerCode = $this->generateNewRelatedCustomerCode();
$this->assertNotEquals($customerCode, $otherCustomerCode);
}
/**
* @return string something like customer_4de7541c5be0c
* The second version (with rand()) will produce a wide variety of values,
* while uniqid() results are guaranteed to be different but not by much
* (2-3 characters).
*/
private function generateNewRelatedCustomerCode()
{
return uniqid("customer_", true);
// return "customer_" . rand(1, 10000);
}
}