Failed Conditions
Pull Request — master (#6575)
by Luís
14:40
created

UnitOfWorkComputeChangesBench   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B init() 0 40 4
A benchChangeSetComputation() 0 4 1
1
<?php
2
3
namespace Doctrine\Performance\ChangeSet;
4
5
use Doctrine\ORM\Query;
6
use Doctrine\ORM\UnitOfWork;
7
use Doctrine\Performance\EntityManagerFactory;
8
use Doctrine\Tests\Mocks\HydratorMockStatement;
9
use Doctrine\Tests\Models\CMS\CmsUser;
10
use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
11
12
/**
13
 * @BeforeMethods({"init"})
14
 */
15
final class UnitOfWorkComputeChangesBench
16
{
17
    /**
18
     * @var CmsUser[]
19
     */
20
    private $users;
21
22
    /**
23
     * @var UnitOfWork
24
     */
25
    private $unitOfWork;
26
27
    public function init()
28
    {
29
        $this->unitOfWork = EntityManagerFactory::getEntityManager([])->getUnitOfWork();
30
31
        for ($i = 1; $i <= 100; ++$i) {
32
            $user           = new CmsUser;
33
            $user->id       = $i;
34
            $user->status   = 'user';
35
            $user->username = 'user' . $i;
36
            $user->name     = 'Mr.Smith-' . $i;
37
            $this->users[]  = $user;
38
39
            $this->unitOfWork->registerManaged(
40
                $user,
41
                [
42
                    'id' => $i,
43
                ],
44
                [
45
                    'id'       => $user->id,
46
                    'status'   => $user->status,
47
                    'username' => $user->username,
48
                    'name'     => $user->name,
49
                    'address'  => $user->address,
50
                    'email'    => $user->email,
51
                ]
52
            );
53
        }
54
55
        $this->unitOfWork->computeChangeSets();
56
57
        if ($this->unitOfWork->getScheduledEntityUpdates()) {
58
            throw new \LogicException('Unit of work should be clean at this stage');
59
        }
60
61
        foreach ($this->users AS $user) {
62
            $user->status    = 'other';
63
            $user->username .= '++';
64
            $user->name      = str_replace('Mr.', 'Mrs.', $user->name);
65
        }
66
    }
67
68
    public function benchChangeSetComputation()
69
    {
70
        $this->unitOfWork->computeChangeSets();
71
    }
72
}
73
74