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

testThrowsExceptionIfIdNotAssigned()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 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