1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Tests\ODM\CouchDB; |
4
|
|
|
|
5
|
|
|
use Doctrine\CouchDB\CouchDBClient; |
6
|
|
|
use Doctrine\CouchDB\HTTP\SocketClient; |
7
|
|
|
use Doctrine\ODM\CouchDB\DocumentManager; |
8
|
|
|
use Doctrine\ODM\CouchDB\Configuration; |
9
|
|
|
use Doctrine\ODM\CouchDB\Mapping\Driver\AnnotationDriver; |
10
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
11
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
12
|
|
|
|
13
|
|
|
abstract class CouchDBFunctionalTestCase extends \PHPUnit_Framework_TestCase |
14
|
|
|
{ |
15
|
|
|
private $httpClient = null; |
16
|
|
|
|
17
|
|
|
protected $logger; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @return \Doctrine\CouchDB\HTTP\Client |
21
|
|
|
*/ |
22
|
|
|
public function getHttpClient() |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
if ($this->httpClient === null) { |
25
|
|
|
if (isset($GLOBALS['DOCTRINE_COUCHDB_CLIENT'])) { |
26
|
|
|
$this->httpClient = new $GLOBALS['DOCTRINE_COUCHDB_CLIENT']; |
27
|
|
|
} else { |
28
|
|
|
$this->httpClient = new SocketClient(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->logger = new \Doctrine\CouchDB\HTTP\LoggingClient($this->httpClient); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->logger; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getTestDatabase() |
38
|
|
|
{ |
39
|
|
|
return TestUtil::getTestDatabase(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function createCouchDBClient() |
43
|
|
|
{ |
44
|
|
|
return new CouchDBClient($this->getHttpClient(), $this->getTestDatabase()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function createDocumentManager() |
48
|
|
|
{ |
49
|
|
|
$couchDBClient = $this->createCouchDBClient(); |
50
|
|
|
$httpClient = $couchDBClient->getHttpClient(); |
51
|
|
|
$database = $couchDBClient->getDatabase(); |
52
|
|
|
|
53
|
|
|
$httpClient->request('DELETE', '/' . $database); |
54
|
|
|
$resp = $httpClient->request('PUT', '/' . $database); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$reader = new \Doctrine\Common\Annotations\SimpleAnnotationReader(); |
57
|
|
|
$reader->addNamespace('Doctrine\ODM\CouchDB\Mapping\Annotations'); |
58
|
|
|
$paths = __DIR__ . "/../../Models"; |
59
|
|
|
$metaDriver = new AnnotationDriver($reader, $paths); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$config = $this->createConfiguration($metaDriver); |
62
|
|
|
|
63
|
|
|
if($this->isVersion2($couchDBClient)){ |
64
|
|
|
$config->setAllOrNothingFlush(false); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return DocumentManager::create($couchDBClient, $config); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function isVersion2(CouchDBClient $couchDBClient) |
71
|
|
|
{ |
72
|
|
|
return substr($couchDBClient->getVersion(), 0, 1)==="2"; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function createConfiguration($metaDriver) |
76
|
|
|
{ |
77
|
|
|
$config = new Configuration(); |
78
|
|
|
$config->setProxyDir(\sys_get_temp_dir()); |
79
|
|
|
$config->setAutoGenerateProxyClasses(true); |
80
|
|
|
$config->setMetadataDriverImpl($metaDriver); |
81
|
|
|
$config->setMetadataCacheImpl(new ArrayCache); |
82
|
|
|
$config->setLuceneHandlerName('_fti'); |
83
|
|
|
|
84
|
|
|
return $config; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: