Completed
Pull Request — develop (#3453)
by Evgeniy
63:46
created

ObjectTest::insert()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 13
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Functional\Types;
6
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Types\Type;
9
use Doctrine\Tests\DBAL\TestAsset\SimpleClass;
10
use Doctrine\Tests\DbalFunctionalTestCase;
11
12
class ObjectTest extends DbalFunctionalTestCase
13
{
14
    protected function setUp() : void
15
    {
16
        parent::setUp();
17
18
        $table = new Table('object_table');
19
        $table->addColumn('id', 'integer');
20
        $table->addColumn('value', 'object');
21
        $table->setPrimaryKey(['id']);
22
23
        $sm = $this->connection->getSchemaManager();
24
        $sm->dropAndCreateTable($table);
25
    }
26
27
    public function testInsertAndSelect() : void
28
    {
29
        $object1 = new SimpleClass();
30
        $object1->setData('Test');
31
32
        $this->insert(1, $object1);
33
        $resultObject1 = $this->select(1);
34
35
        $this->assertInstanceOf(SimpleClass::class, $resultObject1);
36
        $this->assertSame('Test', $resultObject1->getData());
37
    }
38
39
    private function insert(int $id, object $object) : void
40
    {
41
        $value = Type::getType('object')->convertToDatabaseValue($object, $this->connection->getDatabasePlatform());
42
43
        $result = $this->connection->insert('object_table', [
44
            'id'    => $id,
45
            'value' => $value,
46
        ], [
47
            Type::getType('integer')->getBindingType(),
48
            Type::getType('object')->getBindingType(),
49
        ]);
50
51
        $this->assertSame(1, $result);
52
    }
53
54
    private function select(int $id)
55
    {
56
        $value = $this->connection->fetchColumn('SELECT value FROM object_table WHERE id = ?', [$id]);
57
58
        return Type::getType('object')->convertToPHPValue($value, $this->connection->getDatabasePlatform());
59
    }
60
}
61