Passed
Branch merging-leagues-tournaments (a75688)
by Benedikt
07:29
created

IdGeneratorTest::testCreateIdFromMixByExtremes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Created by PhpStorm.
5
 * User: benedikt
6
 * Date: 10/1/17
7
 * Time: 12:52 PM
8
 */
9
10
namespace Tfboe\FmLib\Tests\Unit\Entity\Helpers;
11
12
use Doctrine\ORM\EntityManager;
13
use Iterator;
14
use Tfboe\FmLib\Entity\Helpers\BaseEntity;
15
use Tfboe\FmLib\Entity\Helpers\IdentifiableInterface;
16
use Tfboe\FmLib\Entity\Helpers\IdGenerator;
17
use Tfboe\FmLib\Entity\Helpers\UUIDEntityInterface;
18
use Tfboe\FmLib\Helpers\Random;
19
use Tfboe\FmLib\Tests\Helpers\UnitTestCase;
20
21
/**
22
 * Class IdGeneratorTest
23
 * @package Tfboe\FmLib\Tests\Unit\Entity\Helpers
24
 */
25
class IdGeneratorTest extends UnitTestCase
26
{
27
//<editor-fold desc="Public Methods">
28
  /**
29
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFor
30
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
31
   * @uses   \Tfboe\FmLib\Helpers\Random::stringToRandom
32
   * @uses   \Tfboe\FmLib\Exceptions\Internal::assert
33
   * @uses   \Tfboe\FmLib\Helpers\Random::__construct
34
   * @uses   \Tfboe\FmLib\Helpers\Random::extractEntropyByBits
35
   */
36
  public function testCreateIdFor()
37
  {
38
    $e1 = $this->getMockBuilder(Iterator::class)
39
      ->setMockClassName("CreateIdForA")
40
      ->getMock();
41
    $e2 = $this->getMockBuilder(Iterator::class)
42
      ->setMockClassName("CreateIdForA")
43
      ->getMock();
44
    $e3 = $this->getMockBuilder(Iterator::class)
45
      ->setMockClassName("CreateIdForB")
46
      ->getMock();
47
    srand(10);
48
    $id1 = IdGenerator::createIdFor($e1);
49
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id1);
50
51
    $id2 = IdGenerator::createIdFor($e1);
52
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id2);
53
    self::assertNotEquals($id1, $id2);
54
55
    $id3 = IdGenerator::createIdFor($e2);
56
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id3);
57
    self::assertNotEquals($id1, $id3);
58
    self::assertNotEquals($id2, $id3);
59
60
    srand(10);
61
    $id2 = IdGenerator::createIdFor($e2);
62
    self::assertEquals($id1, $id2);
63
64
    srand(10);
65
    $id3 = IdGenerator::createIdFor($e3);
66
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id3);
67
    self::assertNotEquals($id1, $id3);
68
  }
69
70
  /**
71
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFor
72
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
73
   * @uses   \Tfboe\FmLib\Helpers\Random::stringToRandom
74
   * @uses   \Tfboe\FmLib\Exceptions\Internal::assert
75
   * @uses   \Tfboe\FmLib\Helpers\Random::__construct
76
   * @uses   \Tfboe\FmLib\Helpers\Random::extractEntropyByBits
77
   */
78
  public function testCreateIdForIdAble()
79
  {
80
    $e1 = $this->getMockBuilder(IdentifiableInterface::class)
81
      ->setMockClassName("CreateIdForIdAbleA")
82
      ->getMock();
83
    $e1->method("getIdentifiableId")->willReturn("A");
84
85
    $e2 = $this->getMockBuilder(IdentifiableInterface::class)
86
      ->setMockClassName("CreateIdForIdAbleA")
87
      ->getMock();
88
    $e2->method("getIdentifiableId")->willReturn("A");
89
90
    $e3 = $this->getMockBuilder(IdentifiableInterface::class)
91
      ->setMockClassName("CreateIdForIdAbleB")
92
      ->getMock();
93
    $e3->method("getIdentifiableId")->willReturn("A");
94
95
    $e4 = $this->getMockBuilder(IdentifiableInterface::class)
96
      ->setMockClassName("CreateIdForIdAbleA")
97
      ->getMock();
98
    $e4->method("getIdentifiableId")->willReturn("B");
99
100
    srand(10);
101
    $id1 = IdGenerator::createIdFor($e1);
102
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id1);
103
104
    $id2 = IdGenerator::createIdFor($e1);
105
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id2);
106
    self::assertNotEquals($id1, $id2);
107
108
    $id3 = IdGenerator::createIdFor($e2);
109
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id3);
110
    self::assertNotEquals($id1, $id3);
111
    self::assertNotEquals($id2, $id3);
112
113
    srand(10);
114
    $id2 = IdGenerator::createIdFor($e2);
115
    self::assertEquals($id1, $id2);
116
117
    srand(10);
118
    $id3 = IdGenerator::createIdFor($e3);
119
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id3);
120
    self::assertNotEquals($id1, $id3);
121
122
    srand(10);
123
    $id4 = IdGenerator::createIdFor($e4);
124
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $id4);
125
    self::assertNotEquals($id1, $id4);
126
  }
127
128
  /**
129
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
130
   */
131
  public function testCreateIdFrom()
132
  {
133
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', IdGenerator::createIdFrom());
134
  }
135
136
  /**
137
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
138
   * @uses   \Tfboe\FmLib\Exceptions\Internal::assert
139
   * @uses   \Tfboe\FmLib\Helpers\Random::__construct
140
   * @uses   \Tfboe\FmLib\Helpers\Random::extractEntropyByBits
141
   */
142
  public function testCreateIdFromDeterminism()
143
  {
144
    srand(10);
145
    $id1 = IdGenerator::createIdFrom();
146
    $id2 = IdGenerator::createIdFrom(new Random("0Acb05f"));
147
    $id3 = IdGenerator::createIdFrom(new Random("Acb05f"));
148
    self::assertNotEquals($id1, $id2);
149
    self::assertNotEquals($id2, $id3);
150
    self::assertNotEquals($id1, $id3);
151
    srand(10);
152
    self::assertEquals($id1, IdGenerator::createIdFrom());
153
    self::assertEquals($id2, IdGenerator::createIdFrom(new Random("0Acb05f")));
154
    self::assertEquals($id3, IdGenerator::createIdFrom(new Random("Acb05f")));
155
    srand(10);
156
    self::assertNotEquals($id2, IdGenerator::createIdFrom(new Random("0Acb05f")));
157
    self::assertNotEquals($id3, IdGenerator::createIdFrom(new Random("Acb05f")));
158
    self::assertNotEquals($id1, IdGenerator::createIdFrom());
159
    srand(10);
160
    self::assertNotEquals($id1, IdGenerator::createIdFrom(new Random("0Acb05f")));
161
    self::assertNotEquals($id2, IdGenerator::createIdFrom(new Random("Acb05f")));
162
    self::assertNotEquals($id3, IdGenerator::createIdFrom());
163
  }
164
165
  /**
166
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFor
167
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
168
   * @uses   \Tfboe\FmLib\Helpers\Random::stringToRandom
169
   * @uses   \Tfboe\FmLib\Exceptions\Internal::assert
170
   * @uses   \Tfboe\FmLib\Helpers\Random::__construct
171
   * @uses   \Tfboe\FmLib\Helpers\Random::extractEntropyByBits
172
   */
173
  public function testCreateIdWithExistingId()
174
  {
175
    $e1 = $this->getMockBuilder(UUIDEntityInterface::class)
176
      ->setMockClassName("CreateIdWithExistingIdA")
177
      ->onlyMethods(['getId', 'hasId'])
178
      ->getMock();
179
    $e1->method("getId")->willReturn("1");
180
    $e1->method("hasId")->willReturn(true);
181
182
    self::assertEquals('1', IdGenerator::createIdFor($e1));
183
184
    $e2 = $this->getMockBuilder(UUIDEntityInterface::class)
185
      ->setMockClassName("CreateIdWithExistingIdA")
186
      ->onlyMethods(['getId', 'hasId'])
187
      ->getMock();
188
    $e2->method("hasId")->willReturn(false);
189
190
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', IdGenerator::createIdFor($e2));
191
  }
192
193
  /**
194
   * @covers \Tfboe\FmLib\Entity\Helpers\IdGenerator::generate
195
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFrom
196
   * @uses   \Tfboe\FmLib\Entity\Helpers\IdGenerator::createIdFor
197
   * @uses   \Tfboe\FmLib\Helpers\Random::stringToRandom
198
   * @uses   \Tfboe\FmLib\Exceptions\Internal::assert
199
   * @uses   \Tfboe\FmLib\Helpers\Random::__construct
200
   * @uses   \Tfboe\FmLib\Helpers\Random::extractEntropyByBits
201
   */
202
  public function testGenerate()
203
  {
204
    $generator = new IdGenerator();
205
    $entityManager = $this->createMock(EntityManager::class);
206
    $entity = $this->getStub(BaseEntity::class);
207
    /** @var EntityManager $entityManager */
208
    self::assertRegExp('/^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$/', $generator->generate($entityManager, $entity));
209
  }
210
//</editor-fold desc="Public Methods">
211
}