55namespace Tests \Feature \Filament \App \Imports ;
66
77use App \Enums \CustomFields \CompanyField ;
8- use App \Filament \Resources \CompanyResource \Pages \ListCompanies ;
98use App \Models \Company ;
109use App \Models \CustomField ;
1110use App \Models \CustomFieldValue ;
1413use Filament \Facades \Filament ;
1514use Illuminate \Foundation \Testing \RefreshDatabase ;
1615use Illuminate \Support \Facades \Storage ;
17- use Livewire \Livewire ;
1816use Relaticle \CustomFields \Services \TenantContextService ;
1917use Relaticle \ImportWizard \Enums \DuplicateHandlingStrategy ;
2018use Relaticle \ImportWizard \Filament \Imports \CompanyImporter ;
21- use Relaticle \ImportWizard \Models \Import ;
2219
2320uses (RefreshDatabase::class);
2421
25- function createCompanyTestImportRecord (User $ user , Team $ team ): Import
26- {
27- return Import::create ([
28- 'user_id ' => $ user ->id ,
29- 'team_id ' => $ team ->id ,
30- 'importer ' => CompanyImporter::class,
31- 'file_name ' => 'test.csv ' ,
32- 'file_path ' => '/tmp/test.csv ' ,
33- 'total_rows ' => 1 ,
34- ]);
35- }
36-
37- function setCompanyImporterData (object $ importer , array $ data ): void
38- {
39- $ reflection = new \ReflectionClass ($ importer );
40- $ dataProperty = $ reflection ->getProperty ('data ' );
41- $ dataProperty ->setValue ($ importer , $ data );
42- }
43-
4422beforeEach (function () {
4523 Storage::fake ('local ' );
4624
@@ -53,111 +31,6 @@ function setCompanyImporterData(object $importer, array $data): void
5331 TenantContextService::setTenantId ($ this ->team ->id );
5432});
5533
56- test ('company importer has correct columns defined ' , function () {
57- $ columns = CompanyImporter::getColumns ();
58-
59- $ columnNames = collect ($ columns )->map (fn ($ column ) => $ column ->getName ())->all ();
60-
61- // Core database columns are defined explicitly
62- // Custom fields like address, country, phone are handled by CustomFields::importer()
63- expect ($ columnNames )
64- ->toContain ('name ' )
65- ->toContain ('account_owner_email ' );
66- });
67-
68- test ('company importer has required name column ' , function () {
69- $ columns = CompanyImporter::getColumns ();
70-
71- $ nameColumn = collect ($ columns )->first (fn ($ column ) => $ column ->getName () === 'name ' );
72-
73- expect ($ nameColumn )->not ->toBeNull ()
74- ->and ($ nameColumn ->isMappingRequired ())->toBeTrue ();
75- });
76-
77- test ('company importer has options form with duplicate handling ' , function () {
78- $ components = CompanyImporter::getOptionsFormComponents ();
79-
80- $ duplicateHandlingComponent = collect ($ components )->first (
81- fn ($ component ) => $ component ->getName () === 'duplicate_handling '
82- );
83-
84- expect ($ duplicateHandlingComponent )->not ->toBeNull ()
85- ->and ($ duplicateHandlingComponent ->isRequired ())->toBeTrue ();
86- });
87-
88- test ('import action exists on list companies page ' , function () {
89- Livewire::test (ListCompanies::class)
90- ->assertSuccessful ()
91- ->assertActionExists ('import ' );
92- });
93-
94- test ('company importer guesses column names correctly ' , function () {
95- $ columns = CompanyImporter::getColumns ();
96-
97- $ nameColumn = collect ($ columns )->first (fn ($ column ) => $ column ->getName () === 'name ' );
98-
99- expect ($ nameColumn ->getGuesses ())
100- ->toContain ('name ' )
101- ->toContain ('company_name ' )
102- ->toContain ('company ' );
103- });
104-
105- test ('company importer provides example values ' , function () {
106- $ columns = CompanyImporter::getColumns ();
107-
108- $ nameColumn = collect ($ columns )->first (fn ($ column ) => $ column ->getName () === 'name ' );
109-
110- expect ($ nameColumn ->getExample ())->not ->toBeNull ()
111- ->and ($ nameColumn ->getExample ())->toBe ('Acme Corporation ' );
112- });
113-
114- test ('duplicate handling strategy enum has correct values ' , function () {
115- expect (DuplicateHandlingStrategy::SKIP ->value )->toBe ('skip ' )
116- ->and (DuplicateHandlingStrategy::UPDATE ->value )->toBe ('update ' )
117- ->and (DuplicateHandlingStrategy::CREATE_NEW ->value )->toBe ('create_new ' );
118- });
119-
120- test ('duplicate handling strategy has labels ' , function () {
121- expect (DuplicateHandlingStrategy::SKIP ->getLabel ())->toBe ('Skip duplicates ' )
122- ->and (DuplicateHandlingStrategy::UPDATE ->getLabel ())->toBe ('Update existing records ' )
123- ->and (DuplicateHandlingStrategy::CREATE_NEW ->getLabel ())->toBe ('Create new records anyway ' );
124- });
125-
126- test ('company importer returns completed notification body ' , function () {
127- $ import = new Import ;
128- $ import ->successful_rows = 10 ;
129-
130- $ body = CompanyImporter::getCompletedNotificationBody ($ import );
131-
132- expect ($ body )->toContain ('10 ' )
133- ->and ($ body )->toContain ('imported ' );
134- });
135-
136- test ('company importer includes failed rows in notification ' , function () {
137- $ import = Import::create ([
138- 'team_id ' => $ this ->team ->id ,
139- 'user_id ' => $ this ->user ->id ,
140- 'successful_rows ' => 8 ,
141- 'total_rows ' => 10 ,
142- 'processed_rows ' => 10 ,
143- 'importer ' => CompanyImporter::class,
144- 'file_name ' => 'test.csv ' ,
145- 'file_path ' => 'imports/test.csv ' ,
146- ]);
147-
148- // Create some failed rows
149- $ import ->failedRows ()->createMany ([
150- ['data ' => ['name ' => 'Failed 1 ' ], 'validation_error ' => 'Invalid data ' ],
151- ['data ' => ['name ' => 'Failed 2 ' ], 'validation_error ' => 'Invalid data ' ],
152- ]);
153-
154- $ body = CompanyImporter::getCompletedNotificationBody ($ import );
155-
156- expect ($ body )->toContain ('8 ' )
157- ->and ($ body )->toContain ('2 ' )
158- ->and ($ body )->toContain ('failed ' );
159- });
160-
16134describe ('Domain-Based Duplicate Detection ' , function (): void {
16235 function createDomainsFieldForCompany (Team $ team ): CustomField
16336 {
@@ -190,14 +63,14 @@ function setCompanyDomainValue(Company $company, string $domain, CustomField $fi
19063 setCompanyDomainValue ($ existingCompany , 'acme.com ' , $ domainField );
19164
19265 $ domainsKey = 'custom_fields_ ' .CompanyField::DOMAINS ->value ;
193- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
66+ $ import = createImportRecord ($ this ->user , $ this ->team , CompanyImporter::class );
19467 $ importer = new CompanyImporter (
19568 $ import ,
19669 ['name ' => 'name ' , $ domainsKey => $ domainsKey ],
19770 ['duplicate_handling ' => DuplicateHandlingStrategy::UPDATE ]
19871 );
19972
200- setCompanyImporterData ($ importer , [
73+ setImporterData ($ importer , [
20174 'name ' => 'Different Name ' ,
20275 $ domainsKey => 'acme.com ' ,
20376 ]);
@@ -219,14 +92,14 @@ function setCompanyDomainValue(Company $company, string $domain, CustomField $fi
21992 setCompanyDomainValue ($ domainMatchCompany , 'acme.com ' , $ domainField );
22093
22194 $ domainsKey = 'custom_fields_ ' .CompanyField::DOMAINS ->value ;
222- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
95+ $ import = createImportRecord ($ this ->user , $ this ->team , CompanyImporter::class );
22396 $ importer = new CompanyImporter (
22497 $ import ,
22598 ['name ' => 'name ' , $ domainsKey => $ domainsKey ],
22699 ['duplicate_handling ' => DuplicateHandlingStrategy::UPDATE ]
227100 );
228101
229- setCompanyImporterData ($ importer , [
102+ setImporterData ($ importer , [
230103 'name ' => 'Acme Inc ' , // Matches first company by name
231104 $ domainsKey => 'acme.com ' , // Matches second company by domain
232105 ]);
@@ -237,43 +110,20 @@ function setCompanyDomainValue(Company $company, string $domain, CustomField $fi
237110 expect ($ record ->id )->toBe ($ domainMatchCompany ->id );
238111 });
239112
240- it ('falls back to name match when no domain provided ' , function (): void {
241- $ domainField = createDomainsFieldForCompany ($ this ->team );
242- $ existingCompany = Company::factory ()->for ($ this ->team , 'team ' )->create (['name ' => 'Acme Inc ' ]);
243- setCompanyDomainValue ($ existingCompany , 'acme.com ' , $ domainField );
244-
245- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
246- $ importer = new CompanyImporter (
247- $ import ,
248- ['name ' => 'name ' ],
249- ['duplicate_handling ' => DuplicateHandlingStrategy::UPDATE ]
250- );
251-
252- setCompanyImporterData ($ importer , [
253- 'name ' => 'Acme Inc ' ,
254- // No domain provided
255- ]);
256-
257- $ record = $ importer ->resolveRecord ();
258-
259- expect ($ record ->id )->toBe ($ existingCompany ->id )
260- ->and ($ record ->exists )->toBeTrue ();
261- });
262-
263113 it ('creates new company when domain does not match ' , function (): void {
264114 $ domainField = createDomainsFieldForCompany ($ this ->team );
265115 $ existingCompany = Company::factory ()->for ($ this ->team , 'team ' )->create (['name ' => 'Existing Inc ' ]);
266116 setCompanyDomainValue ($ existingCompany , 'existing.com ' , $ domainField );
267117
268118 $ domainsKey = 'custom_fields_ ' .CompanyField::DOMAINS ->value ;
269- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
119+ $ import = createImportRecord ($ this ->user , $ this ->team , CompanyImporter::class );
270120 $ importer = new CompanyImporter (
271121 $ import ,
272122 ['name ' => 'name ' , $ domainsKey => $ domainsKey ],
273123 ['duplicate_handling ' => DuplicateHandlingStrategy::UPDATE ]
274124 );
275125
276- setCompanyImporterData ($ importer , [
126+ setImporterData ($ importer , [
277127 'name ' => 'New Company ' ,
278128 $ domainsKey => 'newcompany.com ' , // Different domain
279129 ]);
@@ -289,14 +139,14 @@ function setCompanyDomainValue(Company $company, string $domain, CustomField $fi
289139 setCompanyDomainValue ($ existingCompany , 'acme.com ' , $ domainField );
290140
291141 $ domainsKey = 'custom_fields_ ' .CompanyField::DOMAINS ->value ;
292- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
142+ $ import = createImportRecord ($ this ->user , $ this ->team , CompanyImporter::class );
293143 $ importer = new CompanyImporter (
294144 $ import ,
295145 ['name ' => 'name ' , $ domainsKey => $ domainsKey ],
296146 ['duplicate_handling ' => DuplicateHandlingStrategy::UPDATE ]
297147 );
298148
299- setCompanyImporterData ($ importer , [
149+ setImporterData ($ importer , [
300150 'name ' => 'Acme Inc ' ,
301151 $ domainsKey => 'ACME.COM ' , // Uppercase
302152 ]);
@@ -312,14 +162,14 @@ function setCompanyDomainValue(Company $company, string $domain, CustomField $fi
312162 setCompanyDomainValue ($ existingCompany , 'acme.com ' , $ domainField );
313163
314164 $ domainsKey = 'custom_fields_ ' .CompanyField::DOMAINS ->value ;
315- $ import = createCompanyTestImportRecord ($ this ->user , $ this ->team );
165+ $ import = createImportRecord ($ this ->user , $ this ->team , CompanyImporter::class );
316166 $ importer = new CompanyImporter (
317167 $ import ,
318168 ['name ' => 'name ' , $ domainsKey => $ domainsKey ],
319169 ['duplicate_handling ' => DuplicateHandlingStrategy::CREATE_NEW ]
320170 );
321171
322- setCompanyImporterData ($ importer , [
172+ setImporterData ($ importer , [
323173 'name ' => 'Acme Inc ' ,
324174 $ domainsKey => 'acme.com ' ,
325175 ]);
0 commit comments