Passed
Push — master ( f38dcf...954e17 )
by Jean Paul
01:37
created

SourceWatcherTest::testMultipleExtractors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php declare( strict_types=1 );
2
3
namespace Coco\SourceWatcher\Tests\Core;
4
5
use Coco\SourceWatcher\Core\Database\Connections\Connector;
6
use Coco\SourceWatcher\Core\Database\Connections\MySqlConnector;
7
use Coco\SourceWatcher\Core\Database\Connections\SqliteConnector;
8
use Coco\SourceWatcher\Core\IO\Inputs\DatabaseInput;
9
use Coco\SourceWatcher\Core\IO\Inputs\FileInput;
10
use Coco\SourceWatcher\Core\IO\Outputs\DatabaseOutput;
11
use Coco\SourceWatcher\Core\Pipeline;
12
use Coco\SourceWatcher\Core\SourceWatcher;
13
use Coco\SourceWatcher\Core\SourceWatcherException;
14
use Coco\SourceWatcher\Core\StepLoader;
15
use Coco\SourceWatcher\Tests\Common\ParentTest;
16
17
/**
18
 * Class SourceWatcherTest
19
 *
20
 * @package Coco\SourceWatcher\Tests\Core
21
 */
22
class SourceWatcherTest extends ParentTest
23
{
24
    private string $columnsIndex;
25
    private SourceWatcher $sourceWatcher;
26
    private string $nonExistentArtifactName;
27
    private SqliteConnector $sqliteConnector;
28
    private MySqlConnector $mysqlConnector;
29
30
    protected function setUp () : void
31
    {
32
        $this->columnsIndex = "columns";
33
        $this->sourceWatcher = new SourceWatcher();
34
        $this->nonExistentArtifactName = "NonExistent";
35
36
        $this->sqliteConnector = new SqliteConnector();
37
        $this->sqliteConnector->setPath( __DIR__ . "/../../samples/data/sqlite/people-db.sqlite" );
38
        $this->sqliteConnector->setTableName( "people" );
39
40
        $this->mysqlConnector = new MySqlConnector();
41
        $this->mysqlConnector->setUser( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_USERNAME", null ) );
42
        $this->mysqlConnector->setPassword( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_PASSWORD", null ) );
43
        $this->mysqlConnector->setHost( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_HOST", null ) );
44
        $this->mysqlConnector->setPort( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_PORT", 5432, "intval" ) );
45
        $this->mysqlConnector->setDbName( $this->getEnvironmentVariable( "UNIT_TEST_MYSQL_DATABASE", null ) );
46
        $this->mysqlConnector->setTableName( "people" );
47
    }
48
49
    protected function tearDown () : void
50
    {
51
        unset( $this->sourceWatcher );
52
    }
53
54
    public function testGetStepLoaderAndPipeline () : void
55
    {
56
        $stepLoader = $this->sourceWatcher->getStepLoader();
57
        $this->assertNotNull( $stepLoader );
58
        $this->assertInstanceOf( StepLoader::class, $stepLoader );
59
60
        $pipeline = $this->sourceWatcher->getPipeline();
61
        $this->assertNotNull( $stepLoader );
62
        $this->assertInstanceOf( Pipeline::class, $pipeline );
63
    }
64
65
    /**
66
     * @throws SourceWatcherException
67
     */
68
    public function testExtract () : void
69
    {
70
        $sourceWatcherInstance = $this->sourceWatcher->extract( "Csv",
71
            new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
72
            [ $this->columnsIndex => [ "name", "email" ] ] ); // #NOSONAR
73
        $this->assertNotNull( $sourceWatcherInstance );
74
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
75
    }
76
77
    /**
78
     * @throws SourceWatcherException
79
     */
80
    public function testExtractException () : void
81
    {
82
        $this->expectException( SourceWatcherException::class );
83
        $this->expectExceptionMessage( "The extractor NonExistent can't be found." );
84
85
        $this->sourceWatcher->extract( $this->nonExistentArtifactName, $this->createMock( FileInput::class ), [] );
86
    }
87
88
    /**
89
     * @throws SourceWatcherException
90
     */
91
    public function testTransform () : void
92
    {
93
        $sourceWatcherInstance = $this->sourceWatcher->transform( "RenameColumns",
94
            [ $this->columnsIndex => [ "email" => "email_address" ] ] );
95
        $this->assertNotNull( $sourceWatcherInstance );
96
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
97
    }
98
99
    /**
100
     * @throws SourceWatcherException
101
     */
102
    public function testTransformException () : void
103
    {
104
        $this->expectException( SourceWatcherException::class );
105
        $this->expectExceptionMessage( "The transformer NonExistent can't be found." );
106
107
        $this->sourceWatcher->transform( $this->nonExistentArtifactName, [] );
108
    }
109
110
    /**
111
     * @throws SourceWatcherException
112
     */
113
    public function testLoad () : void
114
    {
115
        $sourceWatcherInstance = $this->sourceWatcher->load( "Database",
116
            new DatabaseOutput( $this->createMock( Connector::class ) ) );
117
        $this->assertNotNull( $sourceWatcherInstance );
118
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
119
    }
120
121
    /**
122
     * @throws SourceWatcherException
123
     */
124
    public function testLoadException () : void
125
    {
126
        $this->expectException( SourceWatcherException::class );
127
        $this->expectExceptionMessage( "The loader NonExistent can't be found." );
128
129
        $this->sourceWatcher->load( $this->nonExistentArtifactName, $this->createMock( DatabaseOutput::class ) );
130
    }
131
132
    /**
133
     * Integration test
134
     *
135
     * @throws SourceWatcherException
136
     */
137
    public function testRun () : void
138
    {
139
        $this->sourceWatcher
140
            ->extract( "Csv", new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
141
                [ $this->columnsIndex => [ "name", "email" ] ] )
142
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
143
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) )
144
            ->run();
145
146
        $this->assertNotNull( $this->sourceWatcher->getPipeline()->getResults() );
147
        $this->assertNotEmpty( $this->sourceWatcher->getPipeline()->getResults() );
148
    }
149
150
    /**
151
     * Unit test creating several swt files
152
     *
153
     * @throws SourceWatcherException
154
     */
155
    public function testSaveWithoutName () : void
156
    {
157
        $this->sourceWatcher
158
            ->extract( "Csv", new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
159
                [ $this->columnsIndex => [ "name", "email" ] ] )
160
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
161
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
162
163
        $transformationFile = $this->sourceWatcher->save();
164
        $this->assertNotNull( $transformationFile );
165
        $this->assertFileExists( $transformationFile );
166
        $this->assertStringNotEqualsFile( $transformationFile, "" );
167
168
        $this->sourceWatcher->flush();
169
170
        $this->sourceWatcher
171
            ->extract( "Json", new FileInput( __DIR__ . "/../../samples/data/json/colors.json" ),
172
                [ $this->columnsIndex => [ "color" => "colors.*.color" ] ] )
173
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "color" => "colour" ] ] )
174
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
175
176
        $transformationFile = $this->sourceWatcher->save();
177
        $this->assertNotNull( $transformationFile );
178
        $this->assertFileExists( $transformationFile );
179
        $this->assertStringNotEqualsFile( $transformationFile, "" );
180
181
        $this->sourceWatcher->flush();
182
183
        $this->sourceWatcher
184
            ->extract( "Database", new DatabaseInput( $this->mysqlConnector ),
185
                [ "query" => "SELECT * FROM people ORDER BY name" ] )
186
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
187
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
188
189
        $transformationFile = $this->sourceWatcher->save();
190
        $this->assertNotNull( $transformationFile );
191
        $this->assertFileExists( $transformationFile );
192
        $this->assertStringNotEqualsFile( $transformationFile, "" );
193
194
        $this->sourceWatcher->flush();
195
    }
196
197
    /**
198
     * Acting as an integration test to verify how two extractors work together.
199
     *
200
     * @throws SourceWatcherException
201
     */
202
    public function testMultipleExtractors () : void
203
    {
204
        $this->sourceWatcher
205
            ->extract( "Database", new DatabaseInput( $this->mysqlConnector ),
206
                [ "query" => "SELECT * FROM people ORDER BY name" ] )
207
            ->extract( "FindMissingFromSequence", null, [ "filterField" => "id" ] )
208
            ->run();
209
210
        $this->assertNotNull( $this->sourceWatcher->getPipeline()->getResults() );
211
        $this->assertNotEmpty( $this->sourceWatcher->getPipeline()->getResults() );
212
    }
213
}
214