Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / Functional / Locking / LockAgentWorker.php
1 <?php
2
3 namespace Doctrine\Tests\ORM\Functional\Locking;
4
5 require_once __DIR__ . "/../../../TestInit.php";
6
7 class LockAgentWorker
8 {
9     private $em;
10
11     static public function run()
12     {
13         $lockAgent = new LockAgentWorker();
14
15         $worker = new \GearmanWorker();
16         $worker->addServer();
17         $worker->addFunction("findWithLock", array($lockAgent, "findWithLock"));
18         $worker->addFunction("dqlWithLock", array($lockAgent, "dqlWithLock"));
19         $worker->addFunction('lock', array($lockAgent, 'lock'));
20
21         while($worker->work()) {
22             if ($worker->returnCode() != GEARMAN_SUCCESS) {
23                 echo "return_code: " . $worker->returnCode() . "\n";
24                 break;
25             }
26         }
27     }
28
29     protected function process($job, \Closure $do)
30     {
31         $fixture = $this->processWorkload($job);
32
33         $s = microtime(true);
34         $this->em->beginTransaction();
35         $do($fixture, $this->em);
36
37         sleep(1);
38         $this->em->rollback();
39         $this->em->clear();
40         $this->em->close();
41         $this->em->getConnection()->close();
42
43         return (microtime(true) - $s);
44     }
45
46     public function findWithLock($job)
47     {
48         return $this->process($job, function($fixture, $em) {
49             $entity = $em->find($fixture['entityName'], $fixture['entityId'], $fixture['lockMode']);
50         });
51     }
52
53     public function dqlWithLock($job)
54     {
55         return $this->process($job, function($fixture, $em) {
56             /* @var $query Doctrine\ORM\Query */
57             $query = $em->createQuery($fixture['dql']);
58             $query->setLockMode($fixture['lockMode']);
59             $query->setParameters($fixture['dqlParams']);
60             $result = $query->getResult();
61         });
62     }
63
64     public function lock($job)
65     {
66         return $this->process($job, function($fixture, $em) {
67             $entity = $em->find($fixture['entityName'], $fixture['entityId']);
68             $em->lock($entity, $fixture['lockMode']);
69         });
70     }
71
72     protected function processWorkload($job)
73     {
74         echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";
75
76         $workload = $job->workload();
77         $workload = unserialize($workload);
78
79         if (!isset($workload['conn']) || !is_array($workload['conn'])) {
80             throw new \InvalidArgumentException("Missing Database parameters");
81         }
82
83         $this->em = $this->createEntityManager($workload['conn']);
84
85         if (!isset($workload['fixture'])) {
86             throw new \InvalidArgumentException("Missing Fixture parameters");
87         }
88         return $workload['fixture'];
89     }
90
91     protected function createEntityManager($conn)
92     {
93         $config = new \Doctrine\ORM\Configuration();
94         $config->setProxyDir(__DIR__ . '/../../../Proxies');
95         $config->setProxyNamespace('MyProject\Proxies');
96         $config->setAutoGenerateProxyClasses(true);
97
98         $annotDriver = $config->newDefaultAnnotationDriver(array(__DIR__ . '/../../../Models/'), true);
99         $config->setMetadataDriverImpl($annotDriver);
100
101         $cache = new \Doctrine\Common\Cache\ArrayCache();
102         $config->setMetadataCacheImpl($cache);
103         $config->setQueryCacheImpl($cache);
104         $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
105
106         $em = \Doctrine\ORM\EntityManager::create($conn, $config);
107
108         return $em;
109     }
110 }
111
112 LockAgentWorker::run();