/**
  * Test if all new records go to the begining of the list.
  */
 public function testHead()
 {
     /** @var ObjectInterface|PositionInterface $entry1 */
     /* @var ObjectInterface|PositionInterface $entry2 */
     /* @var ObjectInterface|PositionInterface $entry3 */
     /* @var ObjectInterface|PositionInterface $entry4 */
     $entry1 = $this->pool->produce($this->head_type_class_name, ['application_id' => 1, 'shard_id' => 1]);
     $entry2 = $this->pool->produce($this->head_type_class_name, ['application_id' => 1, 'shard_id' => 1]);
     $entry3 = $this->pool->produce($this->head_type_class_name, ['application_id' => 1, 'shard_id' => 1]);
     $entry4 = $this->pool->produce($this->head_type_class_name, ['application_id' => 1, 'shard_id' => 2]);
     $this->assertInstanceOf($this->head_type_class_name, $entry1);
     $this->assertInstanceOf($this->head_type_class_name, $entry2);
     $this->assertInstanceOf($this->head_type_class_name, $entry3);
     $this->assertInstanceOf($this->head_type_class_name, $entry4);
     /** @var ObjectInterface $v */
     foreach ([$entry1, $entry2, $entry3] as $v) {
         $this->assertEquals(1, $v->getFieldValue('application_id'));
         $this->assertEquals(1, $v->getFieldValue('shard_id'));
     }
     $this->assertEquals(1, $entry4->getFieldValue('application_id'));
     $this->assertEquals(2, $entry4->getFieldValue('shard_id'));
     $this->assertEquals(1, $entry1->getPosition());
     $this->assertEquals(1, $entry2->getPosition());
     $this->assertEquals(1, $entry3->getPosition());
     $this->assertEquals(1, $entry4->getPosition());
     $reloaded_entry_1 = $this->pool->reload($this->head_type_class_name, $entry1->getId());
     $reloaded_entry_2 = $this->pool->reload($this->head_type_class_name, $entry2->getId());
     $reloaded_entry_3 = $this->pool->reload($this->head_type_class_name, $entry3->getId());
     $reloaded_entry_4 = $this->pool->reload($this->head_type_class_name, $entry4->getId());
     $this->assertEquals(3, $reloaded_entry_1->getPosition());
     $this->assertEquals(2, $reloaded_entry_2->getPosition());
     $this->assertEquals(1, $reloaded_entry_3->getPosition());
     $this->assertEquals(1, $reloaded_entry_4->getPosition());
 }
 /**
  * Test if non-empty value can be set.
  */
 public function testValueCanBeSet()
 {
     $key_value = $this->pool->produce($this->type_class_name, ['name' => 'xyz', 'value' => 123]);
     $this->assertInstanceOf($this->type_class_name, $key_value);
     $key_value = $this->pool->reload($this->type_class_name, $key_value->getId());
     $this->assertSame(123, $key_value->getValue());
 }
 /**
  * Test if assoc array is encoded when saved to the database, and properly decoded when loaded.
  */
 public function testAssocArrayValueSerialization()
 {
     $key_value = $this->pool->produce($this->type_class_name, ['name' => 'xyz', 'value' => ['one' => 'two']]);
     $this->assertInstanceOf($this->type_class_name, $key_value);
     $this->assertSame(['one' => 'two'], $key_value->getValue());
     $key_value = $this->pool->reload($this->type_class_name, $key_value->getId());
     $this->assertSame(['one' => 'two'], $key_value->getValue());
     $row = $this->connection->executeFirstRow('SELECT * FROM `key_values` WHERE `id` = ?', $key_value->getId());
     $this->assertInternalType('array', $row);
     $this->assertEquals('xyz', $row['name']);
     $this->assertSame('{"one": "two"}', $row['value']);
 }
 /**
  * Test before update trigger.
  */
 public function testBeforeUpdateTrigger()
 {
     $entry = $this->pool->produce($this->type_class_name, ['num' => 3]);
     $this->assertInstanceOf($this->type_class_name, $entry);
     $this->assertEquals(3, $entry->getNum());
     $reloaded_entry = $this->pool->reload($this->type_class_name, $entry->getId());
     $this->assertEquals(5, $reloaded_entry->getNum());
     $reloaded_entry->setNum(12)->save();
     $this->assertEquals(12, $reloaded_entry->getNum());
     $reloaded_entry_2 = $this->pool->reload($this->type_class_name, $entry->getId());
     $this->assertEquals(15, $reloaded_entry_2->getNum());
 }
 /**
  * Test if all new records go to the begining of the list.
  */
 public function testNewRecordsGoToHead()
 {
     /** @var PositionInterface $entry1 */
     /* @var PositionInterface $entry2 */
     /* @var PositionInterface $entry3 */
     $entry1 = $this->pool->produce($this->type_class_name);
     $entry2 = $this->pool->produce($this->type_class_name);
     $entry3 = $this->pool->produce($this->type_class_name);
     $this->assertInstanceOf($this->type_class_name, $entry1);
     $this->assertInstanceOf($this->type_class_name, $entry2);
     $this->assertInstanceOf($this->type_class_name, $entry3);
     $this->assertEquals(1, $entry1->getPosition());
     $this->assertEquals(1, $entry2->getPosition());
     $this->assertEquals(1, $entry3->getPosition());
     $reloaded_entry_1 = $this->pool->reload($this->type_class_name, $entry1->getId());
     $reloaded_entry_2 = $this->pool->reload($this->type_class_name, $entry2->getId());
     $reloaded_entry_3 = $this->pool->reload($this->type_class_name, $entry3->getId());
     $this->assertEquals(3, $reloaded_entry_1->getPosition());
     $this->assertEquals(2, $reloaded_entry_2->getPosition());
     $this->assertEquals(1, $reloaded_entry_3->getPosition());
 }