Passed
Push — master ( a535f0...f38dcf )
by Jean Paul
08:30
created

SourceWatcherTest::testSaveWithoutName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 40
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
nc 1
nop 0
dl 0
loc 40
rs 9.44
c 1
b 0
f 0
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 PHPUnit\Framework\TestCase;
16
17
/**
18
 * Class SourceWatcherTest
19
 *
20
 * @package Coco\SourceWatcher\Tests\Core
21
 */
22
class SourceWatcherTest extends TestCase
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( "admin" );
42
        $this->mysqlConnector->setPassword( "secret" );
43
        $this->mysqlConnector->setHost( "localhost" );
44
        $this->mysqlConnector->setPort( 3306 );
45
        $this->mysqlConnector->setDbName( "people" );
46
    }
47
48
    protected function tearDown () : void
49
    {
50
        unset( $this->sourceWatcher );
51
    }
52
53
    public function testGetStepLoaderAndPipeline () : void
54
    {
55
        $stepLoader = $this->sourceWatcher->getStepLoader();
56
        $this->assertNotNull( $stepLoader );
57
        $this->assertInstanceOf( StepLoader::class, $stepLoader );
58
59
        $pipeline = $this->sourceWatcher->getPipeline();
60
        $this->assertNotNull( $stepLoader );
61
        $this->assertInstanceOf( Pipeline::class, $pipeline );
62
    }
63
64
    /**
65
     * @throws SourceWatcherException
66
     */
67
    public function testExtract () : void
68
    {
69
        $sourceWatcherInstance = $this->sourceWatcher->extract( "Csv",
70
            new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
71
            [ $this->columnsIndex => [ "name", "email" ] ] ); // #NOSONAR
72
        $this->assertNotNull( $sourceWatcherInstance );
73
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
74
    }
75
76
    /**
77
     * @throws SourceWatcherException
78
     */
79
    public function testExtractException () : void
80
    {
81
        $this->expectException( SourceWatcherException::class );
82
        $this->expectExceptionMessage( "The extractor NonExistent can't be found." );
83
84
        $this->sourceWatcher->extract( $this->nonExistentArtifactName, $this->createMock( FileInput::class ), [] );
85
    }
86
87
    /**
88
     * @throws SourceWatcherException
89
     */
90
    public function testTransform () : void
91
    {
92
        $sourceWatcherInstance = $this->sourceWatcher->transform( "RenameColumns",
93
            [ $this->columnsIndex => [ "email" => "email_address" ] ] );
94
        $this->assertNotNull( $sourceWatcherInstance );
95
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
96
    }
97
98
    /**
99
     * @throws SourceWatcherException
100
     */
101
    public function testTransformException () : void
102
    {
103
        $this->expectException( SourceWatcherException::class );
104
        $this->expectExceptionMessage( "The transformer NonExistent can't be found." );
105
106
        $this->sourceWatcher->transform( $this->nonExistentArtifactName, [] );
107
    }
108
109
    /**
110
     * @throws SourceWatcherException
111
     */
112
    public function testLoad () : void
113
    {
114
        $sourceWatcherInstance = $this->sourceWatcher->load( "Database",
115
            new DatabaseOutput( $this->createMock( Connector::class ) ) );
116
        $this->assertNotNull( $sourceWatcherInstance );
117
        $this->assertInstanceOf( SourceWatcher::class, $sourceWatcherInstance );
118
    }
119
120
    /**
121
     * @throws SourceWatcherException
122
     */
123
    public function testLoadException () : void
124
    {
125
        $this->expectException( SourceWatcherException::class );
126
        $this->expectExceptionMessage( "The loader NonExistent can't be found." );
127
128
        $this->sourceWatcher->load( $this->nonExistentArtifactName, $this->createMock( DatabaseOutput::class ) );
129
    }
130
131
    /**
132
     * @throws SourceWatcherException
133
     */
134
    public function testRun () : void
135
    {
136
        $this->assertNull( $this->sourceWatcher
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->sourceWatcher->ex...qliteConnector))->run() targeting Coco\SourceWatcher\Core\SourceWatcher::run() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
137
            ->extract( "Csv", new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
138
                [ $this->columnsIndex => [ "name", "email" ] ] )
139
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
140
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) )
141
            ->run()
142
        );
143
    }
144
145
    /**
146
     * @throws SourceWatcherException
147
     */
148
    public function testSaveWithoutName () : void
149
    {
150
        $this->sourceWatcher
151
            ->extract( "Csv", new FileInput( __DIR__ . "/../../samples/data/csv/csv1.csv" ),
152
                [ $this->columnsIndex => [ "name", "email" ] ] )
153
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
154
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
155
156
        $transformationFile = $this->sourceWatcher->save();
157
        $this->assertNotNull( $transformationFile );
158
        $this->assertFileExists( $transformationFile );
159
        $this->assertStringNotEqualsFile( $transformationFile, "" );
160
161
        $this->sourceWatcher->flush();
162
163
        $this->sourceWatcher
164
            ->extract( "Json", new FileInput( __DIR__ . "/../../samples/data/json/colors.json" ),
165
                [ $this->columnsIndex => [ "color" => "colors.*.color" ] ] )
166
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "color" => "colour" ] ] )
167
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
168
169
        $transformationFile = $this->sourceWatcher->save();
170
        $this->assertNotNull( $transformationFile );
171
        $this->assertFileExists( $transformationFile );
172
        $this->assertStringNotEqualsFile( $transformationFile, "" );
173
174
        $this->sourceWatcher->flush();
175
176
        $this->sourceWatcher
177
            ->extract( "Database", new DatabaseInput( $this->mysqlConnector ),
178
                [ "query" => "SELECT * FROM people ORDER BY last_name, first_name" ] )
179
            ->transform( "RenameColumns", [ $this->columnsIndex => [ "email" => "email_address" ] ] )
180
            ->load( "Database", new DatabaseOutput( $this->sqliteConnector ) );
181
182
        $transformationFile = $this->sourceWatcher->save();
183
        $this->assertNotNull( $transformationFile );
184
        $this->assertFileExists( $transformationFile );
185
        $this->assertStringNotEqualsFile( $transformationFile, "" );
186
187
        $this->sourceWatcher->flush();
188
    }
189
}
190