DoctrineTest::testImportExport()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 9.1563
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace Mathielen\ImportEngine;
3
4
use Doctrine\ORM\EntityManager;
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Tools\SchemaTool;
7
use Doctrine\ORM\Tools\Setup;
8
use Mathielen\ImportEngine\Import\Import;
9
use Mathielen\ImportEngine\Import\Run\ImportRunner;
10
use Mathielen\ImportEngine\Import\Workflow\DefaultWorkflowFactory;
11
use Mathielen\ImportEngine\Importer\Importer;
12
use Mathielen\ImportEngine\Storage\DoctrineStorage;
13
use Mathielen\ImportEngine\Storage\Format\CsvFormat;
14
use Mathielen\ImportEngine\Storage\LocalFileStorage;
15
use Mathielen\ImportEngine\Storage\StorageInfo;
16
use Mathielen\ImportEngine\ValueObject\ImportConfiguration;
17
use Mathielen\ImportEngine\ValueObject\ImportRun;
18
use Symfony\Component\EventDispatcher\EventDispatcher;
19
20
class DoctrineTest extends \PHPUnit_Framework_TestCase
21
{
22
23
    /**
24
     * @var EntityManagerInterface
25
     */
26
    protected static $em = null;
27
28
    public static function setUpBeforeClass()
29
    {
30
        $isDevMode = true;
31
        $config = Setup::createAnnotationMetadataConfiguration(array(__DIR__."/../../../metadata/TestEntities"), $isDevMode, null, null, false);
32
33
        $connectionOptions = array('driver' => 'pdo_sqlite', 'memory' => true);
34
35
        // obtaining the entity manager
36
        self::$em =  EntityManager::create($connectionOptions, $config);
37
38
        $schemaTool = new SchemaTool(self::$em);
39
40
        $cmf = self::$em->getMetadataFactory();
41
        $classes = $cmf->getAllMetadata();
42
43
        $schemaTool->dropDatabase();
44
        $schemaTool->createSchema($classes);
45
    }
46
47
    public static function tearDownAfterClass()
48
    {
49
        self::$em = NULL;
50
    }
51
52
    /**
53
     * @medium
54
     */
55
    public function testImportExport()
56
    {
57
        $sourceStorage = new LocalFileStorage(new \SplFileInfo(__DIR__ . '/../../../metadata/testfiles/100.csv'), new CsvFormat());
58
        $targetStorage = new DoctrineStorage(self::$em, 'TestEntities\Address');
59
        $this->assertEquals(new StorageInfo(['name'=>'SELECT o FROM TestEntities\Address o', 'count'=>0, 'type'=>'DQL Query']), $targetStorage->info());
60
61
        $importer = Importer::build($targetStorage);
62
63
        $importConfiguration = new ImportConfiguration();
64
        $importRun = $importConfiguration->toRun();
65
66
        $import = Import::build($importer, $sourceStorage, $importRun);
67
68
        $eventDispatcher = new EventDispatcher();
69
        $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher));
70
71
        $importRunner->run($import);
72
73
        $entities = self::$em
74
            ->getRepository('TestEntities\Address')
75
            ->findAll();
76
77
        //import worked
78
        $this->assertEquals(100, count($entities));
79
80
        $exportFile = '/tmp/doctrine_test.csv';
81
        @unlink($exportFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
82
        $sourceStorage = new DoctrineStorage(self::$em, null, self::$em->createQuery("SELECT A FROM TestEntities\Address A WHERE A.zip LIKE '2%'"));
83
        $this->assertEquals(new StorageInfo(['name'=>"SELECT A FROM TestEntities\Address A WHERE A.zip LIKE '2%'", 'count'=>10, 'type'=>'DQL Query']), $sourceStorage->info());
84
85
        $targetStorage = new LocalFileStorage(new \SplFileInfo($exportFile), new CsvFormat());
86
        $importer = Importer::build($targetStorage);
87
88
        $importConfiguration = new ImportConfiguration();
89
        $importRun = $importConfiguration->toRun();
90
91
        $import = Import::build($importer, $sourceStorage, $importRun);
92
93
        $eventDispatcher = new EventDispatcher();
94
        $importRunner = new ImportRunner(new DefaultWorkflowFactory($eventDispatcher));
95
96
        $importRunner->run($import);
97
98
        $this->assertFileExists($exportFile);
99
        $this->assertEquals(11, count(file($exportFile))); //+header
100
        $this->assertEquals(10, $import->getRun()->toArray()['statistics']['processed']);
101
    }
102
103
}
104