Completed
Push — master ( 6ffe9e...612694 )
by Gabriel
03:27
created

EmailsCleanupRecordsTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 7
dl 0
loc 28
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A test_delete() 0 25 1
1
<?php
2
3
namespace Nip\MailModule\Tests\Console\Commands;
4
5
use Mockery;
6
use Nip\Database\Adapters\MySQLi;
7
use Nip\Database\Connections\Connection;
8
use Nip\Database\Query\Delete;
9
use Nip\MailModule\Console\Commands\EmailsCleanupRecords;
10
use Nip\MailModule\Tests\AbstractTest;
11
use Nip\MailModule\Tests\Fixtures\Models\Emails\Emails;
12
use Nip\Records\Locator\ModelLocator;
13
14
/**
15
 * Class EmailsCleanupRecordsTest
16
 * @package Nip\MailModule\Tests\Console\Commands
17
 */
18
class EmailsCleanupRecordsTest extends AbstractTest
19
{
20
    public function test_delete()
21
    {
22
        $query = null;
23
24
        $adapter = Mockery::mock(MySQLi::class)->shouldAllowMockingProtectedMethods()->makePartial();
25
        $adapter->shouldReceive('cleanData')->andReturnArg(0);
26
27
        /** @var Connection|Mockery\Mock $database */
28
        $database = Mockery::mock(Connection::class)->shouldAllowMockingProtectedMethods()->makePartial();
29
        $database->shouldReceive('execute')->with(\Mockery::capture($query))->once();
0 ignored issues
show
Bug introduced by
The method shouldReceive does only exist in Mockery\Mock, but not in Nip\Database\Connections\Connection.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
30
        $database->setAdapter($adapter);
0 ignored issues
show
Bug introduced by
The method setAdapter does only exist in Nip\Database\Connections\Connection, but not in Mockery\Mock.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
31
32
        $emailsTable = new Emails();
33
        $emailsTable->setDB($database);
0 ignored issues
show
Bug introduced by
It seems like $database can also be of type object<Mockery\Mock>; however, Nip\Records\Traits\HasDa...seRecordsTrait::setDB() does only seem to accept object<Nip\Database\Connections\Connection>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
34
        ModelLocator::set('emails', $emailsTable);
35
36
        $command = new EmailsCleanupRecords();
37
        $command->handle();
38
39
        self::assertInstanceOf(Delete::class, $query);
40
        self::assertSame(
41
            'DELETE FROM `emails` WHERE `type` = \'donation\' AND `date_sent` <= DATE_SUB(CURRENT_DATE(), INTERVAL 365 DAY)',
42
            $query->getString()
43
        );
44
    }
45
}
46