|
|
@@ -3301,13 +3301,81 @@ class TableTest extends \Cake\TestSuite\TestCase {
|
|
|
$table->find();
|
|
|
}
|
|
|
|
|
|
+ public function providerForTestGet() {
|
|
|
+ return [
|
|
|
+ [ ['fields' => ['id']] ],
|
|
|
+ [ ['fields' => ['id'], 'cache' => false] ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* Test that get() will use the primary key for searching and return the first
|
|
|
* entity found
|
|
|
*
|
|
|
+ * @dataProvider providerForTestGet
|
|
|
+ * @param array $options
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testGet($options) {
|
|
|
+ $table = $this->getMock(
|
|
|
+ '\Cake\ORM\Table',
|
|
|
+ ['callFinder', 'query'],
|
|
|
+ [[
|
|
|
+ 'connection' => $this->connection,
|
|
|
+ 'schema' => [
|
|
|
+ 'id' => ['type' => 'integer'],
|
|
|
+ 'bar' => ['type' => 'integer'],
|
|
|
+ '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['bar']]]
|
|
|
+ ]
|
|
|
+ ]]
|
|
|
+ );
|
|
|
+
|
|
|
+ $query = $this->getMock(
|
|
|
+ '\Cake\ORM\Query',
|
|
|
+ ['addDefaultTypes', 'first', 'where', 'cache'],
|
|
|
+ [$this->connection, $table]
|
|
|
+ );
|
|
|
+
|
|
|
+ $entity = new \Cake\ORM\Entity;
|
|
|
+ $table->expects($this->once())->method('query')
|
|
|
+ ->will($this->returnValue($query));
|
|
|
+ $table->expects($this->once())->method('callFinder')
|
|
|
+ ->with('all', $query, ['fields' => ['id']])
|
|
|
+ ->will($this->returnValue($query));
|
|
|
+
|
|
|
+ $query->expects($this->once())->method('where')
|
|
|
+ ->with([$table->alias() . '.bar' => 10])
|
|
|
+ ->will($this->returnSelf());
|
|
|
+ $query->expects($this->never())->method('cache');
|
|
|
+ $query->expects($this->once())->method('first')
|
|
|
+ ->will($this->returnValue($entity));
|
|
|
+ $result = $table->get(10, $options);
|
|
|
+ $this->assertSame($entity, $result);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function providerForTestGetWithCache() {
|
|
|
+ return [
|
|
|
+ [
|
|
|
+ ['fields' => ['id'], 'cache' => 'default'],
|
|
|
+ 'get:test.table_name[10]', 'default'
|
|
|
+ ],
|
|
|
+ [
|
|
|
+ ['fields' => ['id'], 'cache' => 'default', 'key' => 'custom_key'],
|
|
|
+ 'custom_key', 'default'
|
|
|
+ ]
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+/**
|
|
|
+ * Test that get() will use the cache.
|
|
|
+ *
|
|
|
+ * @dataProvider providerForTestGetWithCache
|
|
|
+ * @param array $options
|
|
|
+ * @param string $cacheKey
|
|
|
+ * @param string $cacheConfig
|
|
|
* @return void
|
|
|
*/
|
|
|
- public function testGet() {
|
|
|
+ public function testGetWithCache($options, $cacheKey, $cacheConfig) {
|
|
|
$table = $this->getMock(
|
|
|
'\Cake\ORM\Table',
|
|
|
['callFinder', 'query'],
|
|
|
@@ -3320,10 +3388,11 @@ class TableTest extends \Cake\TestSuite\TestCase {
|
|
|
]
|
|
|
]]
|
|
|
);
|
|
|
+ $table->table('table_name');
|
|
|
|
|
|
$query = $this->getMock(
|
|
|
'\Cake\ORM\Query',
|
|
|
- ['addDefaultTypes', 'first', 'where'],
|
|
|
+ ['addDefaultTypes', 'first', 'where', 'cache'],
|
|
|
[$this->connection, $table]
|
|
|
);
|
|
|
|
|
|
@@ -3337,9 +3406,12 @@ class TableTest extends \Cake\TestSuite\TestCase {
|
|
|
$query->expects($this->once())->method('where')
|
|
|
->with([$table->alias() . '.bar' => 10])
|
|
|
->will($this->returnSelf());
|
|
|
+ $query->expects($this->once())->method('cache')
|
|
|
+ ->with($cacheKey, $cacheConfig)
|
|
|
+ ->will($this->returnSelf());
|
|
|
$query->expects($this->once())->method('first')
|
|
|
->will($this->returnValue($entity));
|
|
|
- $result = $table->get(10, ['fields' => ['id']]);
|
|
|
+ $result = $table->get(10, $options);
|
|
|
$this->assertSame($entity, $result);
|
|
|
}
|
|
|
|