/** * JsonableBehaviorTest::testDecodeParams() * * @return void */ public function testDecodeParams() { $this->Comments->removeBehavior('Jsonable'); $this->Comments->addBehavior('Tools.Jsonable', ['output' => 'array', 'fields' => ['details'], 'decodeParams' => ['assoc' => false]]); $data = ['comment' => 'blabla', 'url' => 'www.dereuromark.de', 'title' => 'param', 'details' => ['x' => ['y' => 'z']]]; $entity = $this->Comments->newEntity($data); $this->Comments->save($entity); // Test decode with default params $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); $obj = new stdClass(); $obj->x = new stdClass(); $obj->x->y = 'z'; $expected = $obj; $this->assertEquals($expected, $res['details']); // Test decode with assoc = true $this->Comments->removeBehavior('Jsonable'); $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true]]); $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); $expected = ['x' => ['y' => 'z']]; $this->assertEquals($expected, $res['details']); // Test decode with assoc = true and depth = 2 $this->Comments->removeBehavior('Jsonable'); $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 2]]); $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); $expected = []; $this->assertEquals($expected, $res['details']); // Test decode with assoc = true and depth = 3 $this->Comments->removeBehavior('Jsonable'); $this->Comments->addBehavior('Tools.Jsonable', ['fields' => ['details'], 'decodeParams' => ['assoc' => true, 'depth' => 3]]); $res = $this->Comments->find('all', ['conditions' => ['title' => 'param']])->first(); $expected = ['x' => ['y' => 'z']]; $this->assertEquals($expected, $res['details']); }
/** * ResetBehaviorTest::testResetWithCallbackAndFieldsAutoAdded() * * @return void */ public function testResetWithCallbackAndFieldsAutoAdded() { $this->Table->removeBehavior('Reset'); $this->Table->addBehavior('Tools.Reset', ['fields' => ['id'], 'updateFields' => ['id'], 'callback' => 'TestApp\\Model\\Table\\ResetCommentsTable::fieldsCallbackAuto']); $x = $this->Table->find('first', ['conditions' => ['id' => 6]]); $this->assertEquals('Second Comment for Second Article', $x['comment']); $result = $this->Table->resetRecords(); $this->assertTrue((bool) $result); $x = $this->Table->find('first', ['conditions' => ['id' => 6]]); $expected = 'bar'; $this->assertEquals($expected, $x['comment']); }
/** * BitmaskedBehaviorTest::testNotContains() * * @return void */ public function testNotContains() { $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED); $expected = ['(BitmaskedComments.status & 2 != 2)']; $this->assertEquals($expected, $res); $conditions = $res; $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray(); $this->assertTrue(!empty($res) && count($res) === 4); // multiple (AND) $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]); $expected = ['(BitmaskedComments.status & 3 != 3)']; $this->assertEquals($expected, $res); $conditions = $res; $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray(); $this->assertTrue(!empty($res) && count($res) === 5); }
/** * @return void */ public function testNotContains() { $config = $this->Comments->connection()->config(); $isPostgres = strpos($config['driver'], 'Postgres') !== false; $res = $this->Comments->containsNotBit(BitmaskedComment::STATUS_PUBLISHED); $expected = ['(BitmaskedComments.status & 2 != 2)']; if ($isPostgres) { $expected = ['("BitmaskedComments"."status" & 2 != 2)']; } $this->assertEquals($expected, $res); $conditions = $res; $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray(); $this->assertTrue(!empty($res) && count($res) === 4); // multiple (AND) $res = $this->Comments->containsNotBit([BitmaskedComment::STATUS_PUBLISHED, BitmaskedComment::STATUS_ACTIVE]); $expected = ['(BitmaskedComments.status & 3 != 3)']; if ($isPostgres) { $expected = ['("BitmaskedComments"."status" & 3 != 3)']; } $this->assertEquals($expected, $res); $conditions = $res; $res = $this->Comments->find('all', ['conditions' => $conditions])->toArray(); $this->assertTrue(!empty($res) && count($res) === 5); }