Test Failed
Push — main ( 99efcd...df6632 )
by Alex
14:53
created

ImportDataFromCsvCommandTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
eloc 67
c 10
b 0
f 0
dl 0
loc 171
rs 10
wmc 9
1
<?php
2
3
namespace App\Tests\Command;
4
5
use App\Tests\DatabaseHelperTrait;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\ORM\EntityManagerInterface;
8
use Doctrine\Persistence\ManagerRegistry;
9
use Symfony\Bundle\FrameworkBundle\Console\Application;
10
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Tester\CommandTester;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
14
15
class ImportDataFromCsvCommandTest extends KernelTestCase
16
{
17
    use DatabaseHelperTrait;
18
19
    /**
20
     * Bootstrap kernel and prepare database.
21
     */
22
    protected function setUp(): void
23
    {
24
        // Bootstrap the Symfony kernel
25
        self::bootKernel();
26
27
        // Get the parameter bag from the container
28
        $container = self::$kernel->getContainer();
29
        /** @var ParameterBagInterface $parameterBag */
30
        $parameterBag = $container->getParameterBag();
31
32
        // Get the database connection parameters
33
        $databaseParams = $parameterBag->get('doctrine.connections');
34
35
        var_dump($databaseParams);
36
37
        // Get the database connection name (game in this case)
38
        $connectionName = 'game';
39
40
        // Get the entity manager and connection from the container
41
        $entityManager = $this->getEntityManager($connectionName);
42
        /** @var Connection $connection */
43
        $connection = $entityManager->getConnection();
44
45
        // Query the schema manager to get the table names
46
        $schemaManager = $connection->createSchemaManager();
47
        $tables = $schemaManager->listTables();
48
49
        // Output the table names to the console
50
        echo "\nTables in the '{$connectionName}' database:\n\n";
51
        foreach ($tables as $table) {
52
            echo $table->getName() . " columns:\n";
53
            foreach ($table->getColumns() as $column) {
54
                echo ' - ' . $column->getName() . "\n";
55
            }
56
            echo "\n";
57
        }
58
59
        // Execute the query to get SQLite version
60
        $versionQuery = $connection->query('SELECT sqlite_version() as version');
61
        while (($row = $versionQuery->fetchAssociative()) !== false) {
62
            var_dump($row);
63
        }
64
        
65
        // if (getenv('APP_ENV') !== 'test') {
66
        //     $this->markTestSkipped(
67
        //         'This test can only be run on the test environment.'
68
        //     );
69
        // }
70
71
        /** @var EntityManagerInterface */
72
        $entityManager = $this->getEntityManager('game');
73
74
        // Truncate the location table before each test
75
        $this->truncateTable($entityManager, 'location');
76
    }
77
78
    /**
79
     * Test case for execute method with valid input.
80
     */
81
    public function testExecute(): void
82
    {
83
        // Get the entity manager for the 'game' connection
84
        /** @var EntityManagerInterface */
85
        $entityManager = $this->getEntityManager('game');
86
87
        // Check the contents of the 'location' table before executing the command
88
        $this->assertEmptyTable($entityManager, 'location');
89
90
        $application = new Application(self::$kernel);
91
92
        $command = $application->find('app:import-csv');
93
94
        $commandTester = new CommandTester($command);
95
        $commandTester->execute([
96
            'filename' => 'location.csv',
97
            '--manager' => 'game',
98
        ]);
99
100
        $commandTester->assertCommandIsSuccessful();
101
102
        // Check the output of the command in the console
103
        $output = $commandTester->getDisplay();
104
        $this->assertStringContainsString("Importing CSV\n=============\nFilename: location.csv", $output);
105
106
        // Check the contents of the 'location' table after executing the command
107
        $this->assertNonEmptyTable($entityManager, 'location');
108
    }
109
110
    /**
111
     * Test case for execute method with invalid file.
112
     */
113
    public function testExecuteWithNonExistentCsvFile(): void
114
    {
115
        $application = new Application(self::$kernel);
116
117
        $command = $application->find('app:import-csv');
118
119
        $commandTester = new CommandTester($command);
120
        $commandTester->execute([
121
            'filename' => 'nonexistent.csv',
122
            '--manager' => 'game',
123
        ]);
124
125
        $this->assertSame(Command::FAILURE, $commandTester->getStatusCode());
126
127
        // Expected path to the file
128
        $csvPath = self::$kernel->getProjectDir() . '/public/csv/nonexistent.csv';
129
130
        // Check the output of the command in the console
131
        $output = $commandTester->getDisplay();
132
        $this->assertStringContainsString('The CSV file "' . $csvPath . '" does not exist.', $output);
133
    }
134
135
    /**
136
     * Test case for execute method with invalid entity manager.
137
     */
138
    public function testExecuteWithNonExistentEntityManager(): void
139
    {
140
        $application = new Application(self::$kernel);
141
142
        $command = $application->find('app:import-csv');
143
144
        $commandTester = new CommandTester($command);
145
        $commandTester->execute([
146
            'filename' => 'location.csv',
147
            '--manager' => 'nonexistent',
148
        ]);
149
150
        $this->assertSame(Command::FAILURE, $commandTester->getStatusCode());
151
152
        // Check the output of the command in the console
153
        $output = $commandTester->getDisplay();
154
        $this->assertStringContainsString('The Entity Manager "nonexistent" does not exist.', $output);
155
    }
156
157
    /**
158
     * Test case for execute method with missing entity class.
159
     */
160
    public function testExecuteWithNoEntityClassFound(): void
161
    {
162
        $application = new Application(self::$kernel);
163
164
        $command = $application->find('app:import-csv');
165
166
        $commandTester = new CommandTester($command);
167
        $commandTester->execute([
168
            'filename' => 'dummy.csv',
169
            '--manager' => 'game',
170
        ]);
171
172
        $this->assertSame(Command::FAILURE, $commandTester->getStatusCode());
173
174
        // Check the output of the command in the console
175
        $output = $commandTester->getDisplay();
176
        $this->assertStringContainsString('No entity class found for the CSV file "dummy.csv".', $output);
177
    }
178
179
    /**
180
     * @return object The entity manager.
181
     */
182
    private function getEntityManager(string $name = 'default')
183
    {
184
        $container = self::$kernel->getContainer();
185
186
        /** @var ManagerRegistry $doctrine */
187
        $doctrine = $container->get('doctrine');
188
189
        $entityManager = $doctrine->getManager($name);
190
191
        return $entityManager;
192
    }
193
}
194