Skip to content

Commit 623608a

Browse files
authored
Merge pull request #1 from nicklayb/filterTables
Filter tables
2 parents 7c69896 + 7b58837 commit 623608a

File tree

4 files changed

+293
-10
lines changed

4 files changed

+293
-10
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,31 @@ class ProdImport extends Import
6767
}
6868
```
6969

70+
#### Table filter
71+
72+
Sometimes you may need to filter tables to get only certain items like, for instance, only the last 6 months of work. This may be achieved by adding table filters.
73+
74+
Let say I have a table called `orders` where I only need the last 6 months items.
75+
76+
```
77+
namespace Foo\Console\Commands;
78+
79+
use Nicklayb\LaravelDbImport\Import;
80+
81+
class ProdImport extends Import
82+
{
83+
protected $sourceConnection = 'source';
84+
protected $destinationConnection = 'mysql';
85+
86+
public function filterUsers($query)
87+
{
88+
return $query->where('created_at', '>', Carbon::now()->subMonths(6));
89+
}
90+
}
91+
```
92+
93+
You will receive the base query in parameter and you should return the modified query.
94+
7095
### Registering import
7196

7297
Since you published the vendor's file, you'll notice that you have a brand new `dbimport.php` in your config file. In this file you will register all of you import classes you want to use.

phpunit.xml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
bootstrap="./vendor/autoload.php"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false"
12+
>
13+
<testsuites>
14+
<testsuite name="Package Test Suite">
15+
<directory suffix=".php">./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

src/Import.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,48 @@ public function countImportTasks()
166166
*/
167167
public function getSourceRows($table)
168168
{
169-
return DB::connection($this->sourceConnection)
169+
$query = DB::connection($this->sourceConnection)
170170
->table($table)
171-
->select($this->getSelects($table))
172-
->get();
171+
->select($this->getSelects($table));
172+
return $this->queryTable($query)->get();
173+
}
174+
175+
/**
176+
* Filter query with specific table filters
177+
*
178+
* @param QueryBuilder $query
179+
* @param String $table
180+
* @return QueryBuilder
181+
*/
182+
public function queryTable($query, $table)
183+
{
184+
if ($this->hasTableFilter($table)) {
185+
$filterName = $this->getFilterName($table);
186+
return $this->{$filterName}($query);
187+
}
188+
return $query;
189+
}
190+
191+
/**
192+
* Validates if a specific table has a custom filter
193+
*
194+
* @param String $table
195+
* @return boolean
196+
*/
197+
public function hasTableFilter($table)
198+
{
199+
return method_exists($this, $this->getFilterName($table));
200+
}
201+
202+
/**
203+
* Returns the qualified method name for a table filter
204+
*
205+
* @param String $table
206+
* @return String
207+
*/
208+
public function getFilterName($table)
209+
{
210+
return 'filter'.studly_case($table);
173211
}
174212

175213
/**
@@ -310,7 +348,7 @@ public function hasLastTables()
310348
*/
311349
public function getManipulationName($table)
312350
{
313-
return 'manipulate'.camel_case($table);
351+
return 'manipulate'.studly_case($table);
314352
}
315353

316354
/**

tests/ImportTest.php

Lines changed: 208 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,217 @@
22

33
use PHPUnit\Framework\TestCase;
44

5+
require __DIR__.'/../src/Import.php';
6+
7+
class BasicImport extends Nicklayb\LaravelDbImport\Import
8+
{
9+
}
10+
11+
class ExtendedImport extends Nicklayb\LaravelDbImport\Import
12+
{
13+
protected $ignoreTables = [ 'migrations' ];
14+
protected $lastTables = [ 'relation_one', 'relation_two' ];
15+
protected $selects = [
16+
'users' => [
17+
'id', 'firstname', 'lastname'
18+
]
19+
];
20+
protected $resetPassword = [
21+
'users' => 'test',
22+
'users:super_secret_password' => 'new secret'
23+
];
24+
25+
public function filterUsers($query)
26+
{
27+
return $query;
28+
}
29+
30+
public function manipulateUsers($user)
31+
{
32+
$user['key'] = 'value';
33+
return $user;
34+
}
35+
36+
public function preImport()
37+
{
38+
return [
39+
'pre_task' => function () {
40+
//
41+
}
42+
];
43+
}
44+
45+
public function postImport()
46+
{
47+
return [
48+
'post_task' => function () {
49+
//
50+
}
51+
];
52+
}
53+
}
54+
555
class ImportTest extends TestCase
656
{
7-
public function testImport()
57+
protected $basicImport;
58+
protected $extendedImport;
59+
60+
public function __construct()
861
{
9-
$stub = $this->getMockForAbstractClass('Nicklayb\LaravelDbImport\Import');
10-
$stub->expects($this->any())
11-
->method('countImportTasks')
12-
->will($this->returnValue(0));
62+
parent::__construct();
63+
$this->basicImport = new BasicImport;
64+
$this->extendedImport = new ExtendedImport;
65+
}
1366

14-
$this->assertTrue($stub->concreteMethod());
67+
public function testHasIgnoreTable()
68+
{
69+
$this->assertTrue($this->extendedImport->hasIgnoreTable('migrations'));
70+
}
71+
72+
public function testHasIgnoreTableInexistant()
73+
{
74+
$this->assertFalse($this->extendedImport->hasIgnoreTable('products'));
75+
}
76+
77+
public function testHasLastTable()
78+
{
79+
$this->assertTrue($this->extendedImport->hasLastTable('relation_one'));
80+
}
81+
82+
public function testHasLastTableInexistant()
83+
{
84+
$this->assertFalse($this->extendedImport->hasLastTable('products'));
85+
}
86+
87+
public function testHasPasswordReset()
88+
{
89+
$this->assertTrue($this->extendedImport->hasPasswordResets());
90+
}
91+
92+
public function testHasPasswordResetInexistant()
93+
{
94+
$this->assertFalse($this->basicImport->hasPasswordResets());
95+
}
96+
97+
public function testQualifiedManipulationName()
98+
{
99+
$expected = 'manipulateUsers';
100+
$this->assertEquals($expected, $this->extendedImport->getManipulationName('users'));
101+
}
102+
103+
public function testGetPasswordResetValues()
104+
{
105+
$expected = [
106+
'super_secret_password' => 'new secret',
107+
'password' => 'test'
108+
];
109+
$this->assertEquals($expected, $this->extendedImport->getPasswordResetValues('users'));
110+
}
111+
112+
public function testHasSelects()
113+
{
114+
$this->assertTrue($this->extendedImport->hasSelects('users'));
115+
}
116+
117+
public function testHasSelectsInexistant()
118+
{
119+
$this->assertFalse($this->extendedImport->hasSelects('products'));
120+
}
121+
122+
/**
123+
* @depends testHasSelects
124+
* @depends testHasSelectsInexistant
125+
*/
126+
public function testGetSelects()
127+
{
128+
$expected = [
129+
'id', 'firstname', 'lastname'
130+
];
131+
$this->assertEquals($expected, $this->extendedImport->getSelects('users'));
132+
}
133+
134+
/**
135+
* @depends testHasSelects
136+
* @depends testHasSelectsInexistant
137+
*/
138+
public function testGetSelectsInexistant()
139+
{
140+
$expected = [ '*' ];
141+
$this->assertEquals($expected, $this->extendedImport->getSelects('products'));
142+
}
143+
144+
/**
145+
* @depends testQualifiedManipulationName
146+
*/
147+
public function testHasTableManipulation()
148+
{
149+
$this->assertTrue($this->extendedImport->hasManipulation('users'));
150+
}
151+
152+
/**
153+
* @depends testQualifiedManipulationName
154+
*/
155+
public function testHasInexistantTableManipulation()
156+
{
157+
$this->assertFalse($this->extendedImport->hasManipulation('products'));
158+
}
159+
160+
public function testQualifiedFilterName()
161+
{
162+
$expected = 'filterUsers';
163+
$this->assertEquals($expected, $this->extendedImport->getFilterName('users'));
164+
}
165+
166+
/**
167+
* @depends testQualifiedFilterName
168+
*/
169+
public function testHasTableFilter()
170+
{
171+
$this->assertTrue($this->extendedImport->hasTableFilter('users'));
172+
}
173+
174+
/**
175+
* @depends testQualifiedFilterName
176+
*/
177+
public function testHasInexistantTableFilter()
178+
{
179+
$this->assertFalse($this->extendedImport->hasTableFilter('products'));
180+
}
181+
182+
/**
183+
* @depends testQualifiedFilterName
184+
* @depends testHasTableFilter
185+
* @depends testHasInexistantTableFilter
186+
*/
187+
public function testExecuteManipulation()
188+
{
189+
$table = 'users';
190+
$base = [ 'root' => 'element' ];
191+
$expected = [
192+
'root' => 'element',
193+
'key' => 'value'
194+
];
195+
196+
$this->assertEquals($expected, $this->extendedImport->executeManipulation($table, $base));
197+
}
198+
199+
/**
200+
* @depends testQualifiedFilterName
201+
* @depends testHasTableFilter
202+
* @depends testHasInexistantTableFilter
203+
*/
204+
public function testExecuteInexistantManipulation()
205+
{
206+
$table = 'products';
207+
$base = [ 'root' => 'element' ];
208+
$expected = [ 'root' => 'element' ];
209+
210+
$this->assertEquals($expected, $this->extendedImport->executeManipulation($table, $base));
211+
}
212+
213+
public function testCountImportTasks()
214+
{
215+
$expected = 2;
216+
$this->assertEquals($expected, $this->extendedImport->countImportTasks());
15217
}
16218
}

0 commit comments

Comments
 (0)