1+ <?php
2+
3+ /**
4+ * SPDX-FileCopyrightText: 2026 LibreCode coop and LibreCode contributors
5+ * SPDX-License-Identifier: AGPL-3.0-or-later
6+ */
7+
8+ declare (strict_types=1 );
9+
10+ namespace OCA \ProfileFields \Tests \Unit \Command \Data ;
11+
12+ use OCA \ProfileFields \Command \Data \Import ;
13+ use OCA \ProfileFields \Service \DataImportService ;
14+ use PHPUnit \Framework \MockObject \MockObject ;
15+ use PHPUnit \Framework \TestCase ;
16+ use Symfony \Component \Console \Tester \CommandTester ;
17+
18+ class ImportTest extends TestCase {
19+ private DataImportService &MockObject $ dataImportService ;
20+ private Import $ command ;
21+
22+ protected function setUp (): void {
23+ parent ::setUp ();
24+ $ this ->dataImportService = $ this ->createMock (DataImportService::class);
25+ $ this ->command = new Import ($ this ->dataImportService );
26+ }
27+
28+ public function testExecuteRunsDryRunImportAndPrintsSummary (): void {
29+ $ payloadFile = tempnam (sys_get_temp_dir (), 'profile-fields-import- ' );
30+ $ this ->assertNotFalse ($ payloadFile );
31+ file_put_contents ($ payloadFile , json_encode ([
32+ 'schema_version ' => 1 ,
33+ 'definitions ' => [],
34+ 'values ' => [],
35+ ], JSON_THROW_ON_ERROR ));
36+
37+ $ this ->dataImportService ->expects ($ this ->once ())
38+ ->method ('import ' )
39+ ->with ([
40+ 'schema_version ' => 1 ,
41+ 'definitions ' => [],
42+ 'values ' => [],
43+ ], true )
44+ ->willReturn ([
45+ 'created_definitions ' => 1 ,
46+ 'updated_definitions ' => 2 ,
47+ 'skipped_definitions ' => 3 ,
48+ 'created_values ' => 4 ,
49+ 'updated_values ' => 5 ,
50+ 'skipped_values ' => 6 ,
51+ ]);
52+
53+ $ tester = new CommandTester ($ this ->command );
54+ $ exitCode = $ tester ->execute ([
55+ '--input ' => $ payloadFile ,
56+ '--dry-run ' => true ,
57+ ]);
58+
59+ self ::assertSame (0 , $ exitCode );
60+ self ::assertStringContainsString ('Profile Fields data import dry-run completed. ' , $ tester ->getDisplay ());
61+ self ::assertStringContainsString ('Definitions: 1 created, 2 updated, 3 skipped. ' , $ tester ->getDisplay ());
62+ self ::assertStringContainsString ('Values: 4 created, 5 updated, 6 skipped. ' , $ tester ->getDisplay ());
63+
64+ @unlink ($ payloadFile );
65+ }
66+
67+ public function testExecuteFailsForInvalidJsonInput (): void {
68+ $ payloadFile = tempnam (sys_get_temp_dir (), 'profile-fields-import-invalid- ' );
69+ $ this ->assertNotFalse ($ payloadFile );
70+ file_put_contents ($ payloadFile , '{invalid-json ' );
71+
72+ $ this ->dataImportService ->expects ($ this ->never ())->method ('import ' );
73+
74+ $ tester = new CommandTester ($ this ->command );
75+ $ exitCode = $ tester ->execute ([
76+ '--input ' => $ payloadFile ,
77+ ]);
78+
79+ self ::assertSame (1 , $ exitCode );
80+ self ::assertStringContainsString ('Failed to decode import payload JSON. ' , $ tester ->getDisplay ());
81+
82+ @unlink ($ payloadFile );
83+ }
84+ }
0 commit comments