Completed
Pull Request — master (#6500)
by Mathew
17:08
created

AssignedGeneratorTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 43
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A testThrowsExceptionIfIdNotAssigned() 0 6 1
A entitiesWithoutId() 0 7 1
A testCorrectIdGeneration() 0 13 1
1
<?php
2
3
namespace Doctrine\Tests\ORM\Id;
4
5
use Doctrine\ORM\Id\AssignedGenerator;
6
use Doctrine\ORM\ORMException;
7
use Doctrine\Tests\OrmTestCase;
8
9
/**
10
 * AssignedGeneratorTest
11
 *
12
 * @author robo
13
 */
14
class AssignedGeneratorTest extends OrmTestCase
15
{
16
    private $_em;
17
    private $_assignedGen;
18
19
    protected function setUp()
20
    {
21
        $this->_em = $this->_getTestEntityManager();
22
        $this->_assignedGen = new AssignedGenerator;
23
    }
24
25
    /**
26
     * @dataProvider entitiesWithoutId
27
     */
28
    public function testThrowsExceptionIfIdNotAssigned($entity)
29
    {
30
        $this->expectException(ORMException::class);
31
32
        $this->_assignedGen->generate($this->_em, $entity);
33
    }
34
35
    public function entitiesWithoutId(): array
36
    {
37
        return [
38
            'single'    => [new AssignedSingleIdEntity()],
39
            'composite' => [new AssignedCompositeIdEntity()],
40
        ];
41
    }
42
43
    public function testCorrectIdGeneration()
44
    {
45
        $entity = new AssignedSingleIdEntity;
46
        $entity->myId = 1;
47
        $id = $this->_assignedGen->generate($this->_em, $entity);
48
        $this->assertEquals(['myId' => 1], $id);
49
50
        $entity = new AssignedCompositeIdEntity;
51
        $entity->myId2 = 2;
52
        $entity->myId1 = 4;
53
        $id = $this->_assignedGen->generate($this->_em, $entity);
54
        $this->assertEquals(['myId1' => 4, 'myId2' => 2], $id);
55
    }
56
}
57
58
/** @Entity */
59
class AssignedSingleIdEntity {
60
    /** @Id @Column(type="integer") */
61
    public $myId;
62
}
63
64
/** @Entity */
65
class AssignedCompositeIdEntity {
66
    /** @Id @Column(type="integer") */
67
    public $myId1;
68
    /** @Id @Column(type="integer") */
69
    public $myId2;
70
}
71