Completed
Push — develop ( c0c47b...cc4ea9 )
by Siad
04:17
created

CouchbaseTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 66
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
D setUp() 0 39 10
A testLibOptionsSet() 0 14 1
A tearDown() 0 7 2
1
<?php
2
/**
3
 * zf-couchbase2
4
 *
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
11
 * SOFTWARE.
12
 *
13
 * @copyright 2015 MehrAlsNix (http://www.mehralsnix.de)
14
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
15
 * @link      http://github.com/MehrAlsNix/zf-couchbase2
16
 */
17
18
namespace MehrAlsNix\Test\ZF\Cache\Storage\Adapter;
19
20
use MehrAlsNix\ZF\Cache\Storage\Adapter\Couchbase;
21
use MehrAlsNix\ZF\Cache\Storage\Adapter\CouchbaseOptions;
22
use Zend\Cache\Exception\ExtensionNotLoadedException;
23
24
/**
25
 * Class CouchbaseTest
26
 * @package MehrAlsNix\Test\ZF\Cache\Storage\Adapter
27
 */
28
class CouchbaseTest extends CommonAdapterTest
29
{
30
    public function setUp()
31
    {
32
        if (!getenv('TESTS_ZEND_CACHE_COUCHBASE_ENABLED')) {
33
            $this->markTestSkipped('Enable TESTS_ZEND_CACHE_COUCHBASE_ENABLED to run this test');
34
        }
35
        if (version_compare('2.0.0', phpversion('couchbase')) > 0) {
36
            try {
37
                new Couchbase();
38
                $this->fail("Expected exception Zend\Cache\Exception\ExtensionNotLoadedException");
39
            } catch (ExtensionNotLoadedException $e) {
40
                $this->markTestSkipped("Missing ext/couchbase version >= 2.0.0");
41
            }
42
        }
43
        $this->_options = new CouchbaseOptions([
44
            'resource_id' => __CLASS__
45
        ]);
46
        if (getenv('TESTS_ZEND_CACHE_COUCHBASE_HOST') && getenv('TESTS_ZEND_CACHE_COUCHBASE_PORT')) {
47
            $this->_options->getResourceManager()->setServer(__CLASS__, [
0 ignored issues
show
Documentation introduced by
array(array(getenv('TEST...ACHE_COUCHBASE_PORT'))) is of type array<integer,array<inte...",\"1\":\"string\"}>"}>, but the function expects a string.

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...
48
                [getenv('TESTS_ZEND_CACHE_COUCHBASE_HOST'), getenv('TESTS_ZEND_CACHE_COUCHBASE_PORT')]
49
            ]);
50
        } elseif (getenv('TESTS_ZEND_CACHE_COUCHBASE_HOST')) {
51
            $this->_options->getResourceManager()->setServer(__CLASS__, [
0 ignored issues
show
Documentation introduced by
array(array(getenv('TEST...ACHE_COUCHBASE_HOST'))) is of type array<integer,array<inte...,{\"0\":\"string\"}>"}>, but the function expects a string.

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...
52
                [getenv('TESTS_ZEND_CACHE_COUCHBASE_HOST')]
53
            ]);
54
        }
55
        if (getenv('TESTS_ZEND_CACHE_COUCHBASE_USERNAME')) {
56
            $this->_options->getResourceManager()->setUsername(__CLASS__, getenv('TESTS_ZEND_CACHE_COUCHBASE_USERNAME'));
57
        }
58
        if (getenv('TESTS_ZEND_CACHE_COUCHBASE_PASSWORD')) {
59
            $this->_options->getResourceManager()->setPassword(__CLASS__, getenv('TESTS_ZEND_CACHE_COUCHBASE_PASSWORD'));
60
        }
61
        if (getenv('TESTS_ZEND_CACHE_COUCHBASE_BUCKET')) {
62
            $this->_options->getResourceManager()->setBucket(__CLASS__, getenv('TESTS_ZEND_CACHE_COUCHBASE_BUCKET'));
63
        }
64
        $this->_storage = new Couchbase();
65
        $this->_storage->setOptions($this->_options);
66
        $this->_storage->flush();
67
        parent::setUp();
68
    }
69
70
    public function testLibOptionsSet()
71
    {
72
        $options = new CouchbaseOptions();
73
        $options->setLibOptions([
74
            'SERTYPE_IGBINARY' => false
75
        ]);
76
        $this->assertEquals($options->getResourceManager()->getLibOptions(
77
            $options->getResourceId()
78
        ), false);
79
        $couchbase = new Couchbase($options);
80
        $this->assertEquals($couchbase->getOptions()->getLibOptions(), [
81
            \COUCHBASE_SERTYPE_IGBINARY => false
82
        ]);
83
    }
84
85
86
    public function tearDown()
87
    {
88
        if ($this->_storage) {
89
            $this->_storage->flush();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Zend\Cache\Storage\StorageInterface as the method flush() does only exist in the following implementations of said interface: MehrAlsNix\ZF\Cache\Storage\Adapter\Couchbase, Zend\Cache\Storage\Adapter\Apc, Zend\Cache\Storage\Adapter\BlackHole, Zend\Cache\Storage\Adapter\Dba, Zend\Cache\Storage\Adapter\Filesystem, Zend\Cache\Storage\Adapter\Memcache, Zend\Cache\Storage\Adapter\Memcached, Zend\Cache\Storage\Adapter\Memory, Zend\Cache\Storage\Adapter\MongoDb, Zend\Cache\Storage\Adapter\Redis, Zend\Cache\Storage\Adapter\Session, Zend\Cache\Storage\Adapter\WinCache, Zend\Cache\Storage\Adapter\XCache, Zend\Cache\Storage\Adapter\ZendServerDisk, Zend\Cache\Storage\Adapter\ZendServerShm.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90
        }
91
        parent::tearDown();
92
    }
93
}
94