Completed
Pull Request — master (#138)
by
unknown
02:54
created

CouchDBFunctionalTestCase::isVersion2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
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()
0 ignored issues
show
Coding Style introduced by
getHttpClient uses the super-global variable $GLOBALS which is generally not recommended.

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:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
$resp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
$reader is of type object<Doctrine\Common\A...SimpleAnnotationReader>, but the function expects a object<Doctrine\Common\A...tions\AnnotationReader>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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