Skip to content

Commit 5633bdf

Browse files
author
Nicolas Boisvert
committed
Started test implementations
1 parent 7c69896 commit 5633bdf

File tree

3 files changed

+155
-10
lines changed

3 files changed

+155
-10
lines changed

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: 95 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,104 @@
22

33
use PHPUnit\Framework\TestCase;
44

5+
require __DIR__.'/../src/Import.php';
6+
7+
class ImportExtended extends Nicklayb\LaravelDbImport\Import
8+
{
9+
public function filterUsers($query)
10+
{
11+
return $query;
12+
}
13+
14+
public function manipulateUsers($user)
15+
{
16+
$user['key'] = 'value';
17+
return $user;
18+
}
19+
}
20+
521
class ImportTest extends TestCase
622
{
7-
public function testImport()
23+
protected $import;
24+
25+
public function __construct()
26+
{
27+
parent::__construct();
28+
$this->import = new ImportExtended;
29+
}
30+
31+
public function testQualifiedManipulationName()
32+
{
33+
$expected = 'manipulateUsers';
34+
$this->assertEquals($expected, $this->import->getManipulationName('users'));
35+
}
36+
37+
/**
38+
* @depends testQualifiedManipulationName
39+
*/
40+
public function testHasTableManipulation()
41+
{
42+
$this->assertTrue($this->import->hasManipulation('users'));
43+
}
44+
45+
/**
46+
* @depends testQualifiedManipulationName
47+
*/
48+
public function testHasInexistantTableManipulation()
49+
{
50+
$this->assertFalse($this->import->hasManipulation('products'));
51+
}
52+
53+
public function testQualifiedFilterName()
54+
{
55+
$expected = 'filterUsers';
56+
$this->assertEquals($expected, $this->import->getFilterName('users'));
57+
}
58+
59+
/**
60+
* @depends testQualifiedFilterName
61+
*/
62+
public function testHasTableFilter()
63+
{
64+
$this->assertTrue($this->import->hasTableFilter('users'));
65+
}
66+
67+
/**
68+
* @depends testQualifiedFilterName
69+
*/
70+
public function testHasInexistantTableFilter()
71+
{
72+
$this->assertFalse($this->import->hasTableFilter('products'));
73+
}
74+
75+
/**
76+
* @depends testQualifiedFilterName
77+
* @depends testHasTableFilter
78+
* @depends testHasInexistantTableFilter
79+
*/
80+
public function testExecuteManipulation()
81+
{
82+
$table = 'users';
83+
$base = [ 'root' => 'element' ];
84+
$expected = [
85+
'root' => 'element',
86+
'key' => 'value'
87+
];
88+
89+
$this->assertEquals($expected, $this->import->executeManipulation($table, $base));
90+
}
91+
92+
/**
93+
* @depends testQualifiedFilterName
94+
* @depends testHasTableFilter
95+
* @depends testHasInexistantTableFilter
96+
*/
97+
public function testExecuteInexistantManipulation()
898
{
9-
$stub = $this->getMockForAbstractClass('Nicklayb\LaravelDbImport\Import');
10-
$stub->expects($this->any())
11-
->method('countImportTasks')
12-
->will($this->returnValue(0));
99+
$table = 'products';
100+
$base = [ 'root' => 'element' ];
101+
$expected = [ 'root' => 'element' ];
13102

14-
$this->assertTrue($stub->concreteMethod());
103+
$this->assertEquals($expected, $this->import->executeManipulation($table, $base));
15104
}
16105
}

0 commit comments

Comments
 (0)