/** * 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()); }
/** * @return string */ private function getEscapedTableName() { if (empty($this->escaped_table_name)) { $this->escaped_table_name = $this->pool->getTypeTable($this->type, true); } return $this->escaped_table_name; }
/** * 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']); }
public function testUpdate() { $stats_snapshot = $this->pool->produce($this->stats_snapshot_class_name, ['day' => new DateValue('2016-11-28'), 'stats' => ['plan_name' => 'MEGA', 'users' => ['num_active' => 123], 'is_used_on_day' => 1]]); $this->assertInstanceOf($this->stats_snapshot_class_name, $stats_snapshot); $this->assertSame('MEGA', $stats_snapshot->getPlanName()); $this->assertSame(123, $stats_snapshot->getNumberOfActiveUsers()); $this->assertTrue($stats_snapshot->isUsedOnDay()); $stats_snapshot->setStats(['plan_name' => 'Lerge', 'users' => ['num_active' => 321], 'is_used_on_day' => 0])->save(); $this->assertSame('Lerge', $stats_snapshot->getPlanName()); $this->assertSame(321, $stats_snapshot->getNumberOfActiveUsers()); $this->assertFalse($stats_snapshot->isUsedOnDay()); }
/** * 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 testNewRecordsGoToTail() { /** @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(2, $entry2->getPosition()); $this->assertEquals(3, $entry3->getPosition()); }
/** * @param ConnectionInterface $connection * @param PoolInterface $pool * @param LoggerInterface|null $log */ public function __construct(ConnectionInterface $connection, PoolInterface $pool, LoggerInterface &$log = null) { $this->connection = $connection; $this->pool = $pool; $this->log = $log; if ($traits = $pool->getTraitNamesByType(get_class($this))) { foreach ($traits as $trait) { $trait_constructor = str_replace('\\', '', $trait); if (method_exists($this, $trait_constructor)) { $this->{$trait_constructor}(); } } } $this->configure(); }