Passed
Push — master ( 5f43e1...ab6e06 )
by Valentin
02:56
created

SnapshotSourceTest::testFindWithLastByPK()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Spiral\Tests\Snapshotter\AggregationHandler\Sources;
4
5
use Spiral\Snapshotter\AggregationHandler\Database\Sources\SnapshotSource;
6
use Spiral\Snapshotter\AggregationHandler\Services\SnapshotService;
7
use Spiral\Tests\BaseTest;
8
9
class SnapshotSourceTest extends BaseTest
10
{
11
    /**
12
     * Find snapshot record by hash.
13
     */
14
    public function testFindByHash()
15
    {
16
        $snapshot = $this->makeSnapshot('custom error', 777);
17
        $this->handleSnapshot($snapshot, true);
18
19
        $hash = SnapshotService::makeHash($snapshot);
20
        $hash2 = 'some second random hash';
21
22
        /** @var SnapshotSource $source */
23
        $source = $this->container->get(SnapshotSource::class);
24
        $this->assertNotEmpty($source->findByHash($hash));
25
        $this->assertEmpty($source->findByHash($hash2));
26
27
        $record = $source->findByHash($hash);
28
        $record->exception_hash = $hash2;
0 ignored issues
show
Bug introduced by
Accessing exception_hash on the interface Spiral\ORM\RecordInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
29
        $record->save();
30
31
        $this->assertEmpty($source->findByHash($hash));
32
        $this->assertNotEmpty($source->findByHash($hash2));
33
    }
34
35
    public function testFindLast()
36
    {
37
        /** @var SnapshotSource $source */
38
        $source = $this->container->get(SnapshotSource::class);
39
40
        $this->assertEmpty($source->findLast());
41
42
        $snapshot1 = $this->makeSnapshot('custom error1', 777);
43
        $this->handleSnapshot($snapshot1, true);
44
        $last1 = $source->findLast();
45
        $this->assertNotEmpty($last1);
46
47
        sleep(1);
48
        $snapshot2 = $this->makeSnapshot('custom error2', 777);
49
        $this->handleSnapshot($snapshot2, true);
50
        $last2 = $source->findLast();
51
        $this->assertNotEmpty($last2);
52
        $this->assertNotSame($last1->primaryKey(), $last2->primaryKey());
53
54
        sleep(1);
55
        $snapshot3 = $this->makeSnapshot('custom error2', 777);
56
        $this->handleSnapshot($snapshot3, true);
57
        $last3 = $source->findLast();
58
        $this->assertNotEmpty($last3);
59
        $this->assertSame($last2->primaryKey(), $last3->primaryKey());
60
    }
61
62
    public function testFindWithLastByPK()
63
    {
64
        /** @var SnapshotSource $source */
65
        $source = $this->container->get(SnapshotSource::class);
66
67
        $record = $source->create();
68
        $record->save();
0 ignored issues
show
Bug introduced by
The method save does only exist in Spiral\ORM\Record, but not in Spiral\Models\EntityInterface.

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...
69
70
        $this->assertEmpty($source->findWithLastByPK($record->primaryKey()));
0 ignored issues
show
Bug introduced by
The method primaryKey does only exist in Spiral\ORM\Record, but not in Spiral\Models\EntityInterface.

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...
71
72
        $snapshot = $this->makeSnapshot('custom error', 777);
73
        $last = $this->handleSnapshot($snapshot, true);
74
75
        $this->assertNotEmpty($last);
76
        $this->assertNotSame($last->primaryKey(), $record->primaryKey());
77
        $this->assertNotEmpty($source->findWithLastByPK($last->primaryKey()));
78
    }
79
}