Completed
Push — master ( d9ab08...4ec3c1 )
by compolom
05:33
created

Test::test_delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace DrMVC\Orm;
4
5
use PDO;
6
use PHPUnit\Framework\TestCase;
7
8
class Test extends TestCase
9
{
10
11
    private $pdo;
12
13
    private $orm;
14
15
    /**
16
     * Test constructor.
17
     * @param string|null $name
18
     * @param array $data
19
     * @param string $dataName
20
     */
21
    public function __construct(string $name = null, array $data = [], string $dataName = '')
22
    {
23
        parent::__construct($name, $data, $dataName);
24
        $sql = 'CREATE TABLE IF NOT EXISTS `test` (
25
          `id` INTEGER PRIMARY KEY,
26
          `name` varchar(255),
27
          `email` varchar(255),
28
          `password` varchar(32)
29
        )';
30
        $this->getPDO()->exec($sql);
31
        $this->orm = new Orm('test', $this->getPDO());
32
        $this->assertInstanceOf(Orm::class, $this->orm);
33
    }
34
35
    public function getPDO(): PDO
36
    {
37
        if (!$this->pdo) {
38
            $pdo = new PDO('sqlite:' . __DIR__ . '/test.db');
39
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
40
            $this->pdo = $pdo;
41
        }
42
        return $this->pdo;
43
    }
44
45
    public function test_insert()
46
    {
47
        $entity = new Entity();
48
49
        $entity->id = null;
50
        $entity->setName('Kolya');
0 ignored issues
show
Documentation Bug introduced by
The method setName does not exist on object<DrMVC\Orm\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
51
        $entity->email = 'qweqwe';
0 ignored issues
show
Documentation introduced by
The property email does not exist on object<DrMVC\Orm\Entity>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
52
        $entity->setPassword('qwerty');
0 ignored issues
show
Documentation Bug introduced by
The method setPassword does not exist on object<DrMVC\Orm\Entity>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
53
54
        $count = $this->orm->saveEntity($entity);
55
        $this->assertEquals(1, $count->rowCount());
56
    }
57
58
    public function test_getById()
59
    {
60
        $entity = $this->orm->findById(1);
61
        $this->assertInstanceOf(Entity::class, $entity);
62
    }
63
64
    public function test_findAll()
65
    {
66
        $entity = $this->orm->findAll();
67
        $this->assertInternalType('array', $entity);
68
        $this->assertInstanceOf(Entity::class, $entity[0]);
69
    }
70
71
    public function test_delete()
72
    {
73
        $entity = $this->orm->findById(1);
74
        $this->assertEquals(1, $this->orm->deleteEntity($entity));
0 ignored issues
show
Bug introduced by
It seems like $entity defined by $this->orm->findById(1) on line 73 can be null; however, DrMVC\Orm\Orm::deleteEntity() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
75
    }
76
}
77