Skip to content

Commit 2696cbb

Browse files
author
Robert Pustułka
committed
Added support for chunked finder.
1 parent cd37e7d commit 2696cbb

File tree

5 files changed

+218
-1
lines changed

5 files changed

+218
-1
lines changed

src/Model/Behavior/ChunkBehavior.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use Cake\ORM\Behavior;
2828
use Cake\ORM\Query;
29+
use Robotusers\Chunk\ORM\Query as ChunkedQuery;
2930
use Robotusers\Chunk\ORM\ResultSet;
3031

3132
class ChunkBehavior extends Behavior
@@ -42,4 +43,19 @@ public function chunk(Query $query, array $config = [])
4243
{
4344
return new ResultSet($query, $config);
4445
}
46+
47+
/**
48+
* Chunked result finder.
49+
*
50+
* @param Query $query Query instance.
51+
* @param array $config Config.
52+
* @return ChunkedQuery
53+
*/
54+
public function findChunked(Query $query, array $config = [])
55+
{
56+
$chunkedQuery = new ChunkedQuery($query);
57+
$chunkedQuery->applyOptions($config);
58+
59+
return $chunkedQuery;
60+
}
4561
}

src/ORM/Query.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/*
3+
* The MIT License
4+
*
5+
* Copyright 2017 Robert Pustułka <[email protected]>.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
namespace Robotusers\Chunk\ORM;
27+
28+
use Cake\ORM\Query as BaseQuery;
29+
30+
/**
31+
* Description of Query
32+
*
33+
* @author Robert Pustułka <[email protected]>
34+
*/
35+
class Query extends BaseQuery
36+
{
37+
38+
/**
39+
* Constructor. Takes a query and feeds this object with its properties.
40+
*
41+
* @param BaseQuery $query Query instance.
42+
*/
43+
public function __construct(BaseQuery $query)
44+
{
45+
parent::__construct($query->getConnection(), $query->repository());
46+
47+
$this->load($query);
48+
}
49+
50+
/**
51+
* {@inheritDoc}
52+
*/
53+
public function applyOptions(array $options)
54+
{
55+
if (isset($options['chunkSize'])) {
56+
$this->_options['chunkSize'] = $options['chunkSize'];
57+
}
58+
59+
return parent::applyOptions($options);
60+
}
61+
62+
/**
63+
* Returns chunked reuslt set.
64+
*
65+
* @return \Robotusers\Chunk\ORM\ResultSet
66+
*/
67+
public function all()
68+
{
69+
$config = [];
70+
if (isset($this->_options['chunkSize'])) {
71+
$config['size'] = $this->_options['chunkSize'];
72+
}
73+
74+
return new ResultSet($this, $config);
75+
}
76+
77+
/**
78+
* Returns default ResultSet.
79+
*
80+
* {@inheritDoc}
81+
*/
82+
public function defaultAll()
83+
{
84+
return parent::all();
85+
}
86+
87+
/**
88+
* Feeds this object with query properties.
89+
*
90+
* @param BaseQuery $query Query instance.
91+
* @return void
92+
*/
93+
protected function load(BaseQuery $query)
94+
{
95+
$vars = get_object_vars($query);
96+
foreach ($vars as $key => $value) {
97+
$this->$key = $value;
98+
}
99+
}
100+
}

tests/TestCase/Model/Behavior/ChunkBehaviorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Cake\ORM\TableRegistry;
2828
use Cake\TestSuite\TestCase;
2929
use Robotusers\Chunk\Model\Behavior\ChunkBehavior;
30+
use Robotusers\Chunk\ORM\Query;
3031
use Robotusers\Chunk\ORM\ResultSet;
3132

3233
/**
@@ -53,4 +54,16 @@ public function testChunk()
5354
$this->assertInstanceOf(ResultSet::class, $chunk);
5455
$this->assertEquals(100, $chunk->getConfig('size'));
5556
}
57+
58+
public function testFinder()
59+
{
60+
$table = TableRegistry::get('Authors');
61+
$table->addBehavior('Robotusers/Chunk.Chunk');
62+
$query = $table->find('chunked', [
63+
'chunkSize' => 1
64+
]);
65+
66+
$this->assertInstanceOf(Query::class, $query);
67+
$this->assertEquals(1, $query->getOptions()['chunkSize']);
68+
}
5669
}

tests/TestCase/ORM/QueryTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/*
3+
* The MIT License
4+
*
5+
* Copyright 2017 Robert Pustułka <[email protected]>.
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
26+
namespace Robotusers\Chunk\Test\TestCase\ORM;
27+
28+
use Cake\ORM\ResultSet as CoreResultSet;
29+
use Cake\ORM\TableRegistry;
30+
use Cake\TestSuite\TestCase;
31+
use Robotusers\Chunk\ORM\Query;
32+
use Robotusers\Chunk\ORM\ResultSet;
33+
34+
/**
35+
* Description of QueryTest
36+
*
37+
* @author Robert Pustułka <[email protected]>
38+
*/
39+
class QueryTest extends TestCase
40+
{
41+
public $fixtures = [
42+
'core.authors'
43+
];
44+
45+
public function testLoad()
46+
{
47+
$table = TableRegistry::get('Authors');
48+
$query = $table->find();
49+
50+
$chunkedQuery = new Query($query);
51+
52+
$this->assertSame($query->repository(), $chunkedQuery->repository());
53+
$this->assertSame($query->getConnection(), $chunkedQuery->getConnection());
54+
$this->assertSame($query->getOptions(), $chunkedQuery->getOptions());
55+
}
56+
57+
public function testApplyOptions()
58+
{
59+
$table = TableRegistry::get('Authors');
60+
$query = $table->find();
61+
62+
$chunkedQuery = new Query($query);
63+
$chunkedQuery->applyOptions(['chunkSize' => 100]);
64+
$this->assertEquals(100, $chunkedQuery->getOptions()['chunkSize']);
65+
}
66+
67+
public function testAll()
68+
{
69+
$table = TableRegistry::get('Authors');
70+
$query = $table->find();
71+
72+
$chunkedQuery = new Query($query);
73+
$chunkedQuery->applyOptions(['chunkSize' => 100]);
74+
$results = $chunkedQuery->all();
75+
$this->assertInstanceOf(ResultSet::class, $results);
76+
$this->assertEquals(100, $results->getConfig('size'));
77+
}
78+
79+
public function testDefaultAll()
80+
{
81+
$table = TableRegistry::get('Authors');
82+
$query = $table->find();
83+
84+
$chunkedQuery = new Query($query);
85+
$results = $chunkedQuery->defaultAll();
86+
$this->assertInstanceOf(CoreResultSet::class, $results);
87+
}
88+
}

tests/TestCase/ORM/ResultSetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* THE SOFTWARE.
2424
*/
2525

26-
namespace Robotusers\Chunk\Test\TestCase\Model;
26+
namespace Robotusers\Chunk\Test\TestCase\ORM;
2727

2828
use Cake\ORM\TableRegistry;
2929
use Cake\TestSuite\TestCase;

0 commit comments

Comments
 (0)