Skip to content

Commit 3ee5713

Browse files
authored
Merge pull request #15631 from snipe/test/importer-tests
Add importer tests
2 parents ab8a22f + 56e7ea6 commit 3ee5713

20 files changed

+3222
-7
lines changed

app/Models/Asset.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ class Asset extends Depreciable
4343

4444
/**
4545
* Run after the checkout acceptance was declined by the user
46-
*
46+
*
4747
* @param User $acceptedBy
4848
* @param string $signature
49-
*/
49+
*/
5050
public function declinedCheckout(User $declinedBy, $signature)
5151
{
5252
$this->assigned_to = null;
5353
$this->assigned_type = null;
54-
$this->accepted = null;
55-
$this->save();
54+
$this->accepted = null;
55+
$this->save();
5656
}
5757

5858
/**
@@ -368,7 +368,7 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_
368368
if ($this->save()) {
369369
if (is_int($admin)) {
370370
$checkedOutBy = User::findOrFail($admin);
371-
} elseif (get_class($admin) === \App\Models\User::class) {
371+
} elseif ($admin && get_class($admin) === \App\Models\User::class) {
372372
$checkedOutBy = $admin;
373373
} else {
374374
$checkedOutBy = auth()->user();
@@ -1705,7 +1705,7 @@ public function scopeByFilter($query, $filter)
17051705
});
17061706
});
17071707
}
1708-
1708+
17091709

17101710
/**
17111711
* THIS CLUNKY BIT IS VERY IMPORTANT
@@ -1726,7 +1726,7 @@ public function scopeByFilter($query, $filter)
17261726
* assets.location would fail, as that field doesn't exist -- plus we're already searching
17271727
* against those relationships earlier in this method.
17281728
*
1729-
* - snipe
1729+
* - snipe
17301730
*
17311731
*/
17321732

app/Models/Import.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace App\Models;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\Model;
67

78
class Import extends Model
89
{
10+
use HasFactory;
11+
912
protected $casts = [
1013
'header_row' => 'array',
1114
'first_row' => 'array',
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace Database\Factories;
4+
5+
use App\Models\Import;
6+
use Illuminate\Support\Str;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
use Tests\Support\Importing;
9+
10+
/**
11+
* @extends Factory<Import>
12+
*/
13+
class ImportFactory extends Factory
14+
{
15+
/**
16+
* @inheritdoc
17+
*/
18+
protected $model = Import::class;
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
public function definition()
24+
{
25+
return [
26+
'name' => $this->faker->company,
27+
'file_path' => Str::random().'.csv',
28+
'filesize' => $this->faker->randomDigitNotNull(),
29+
'field_map' => null,
30+
];
31+
}
32+
33+
/**
34+
* Create an accessory import type.
35+
*
36+
* @return static
37+
*/
38+
public function accessory()
39+
{
40+
return $this->state(function (array $attributes) {
41+
$fileBuilder = Importing\AccessoriesImportFileBuilder::new();
42+
43+
$attributes['name'] = "{$attributes['name']} Accessories";
44+
$attributes['import_type'] = 'accessory';
45+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
46+
$attributes['first_row'] = $fileBuilder->firstRow();
47+
48+
return $attributes;
49+
});
50+
}
51+
52+
/**
53+
* Create an asset import type.
54+
*
55+
* @return static
56+
*/
57+
public function asset()
58+
{
59+
return $this->state(function (array $attributes) {
60+
$fileBuilder = Importing\AssetsImportFileBuilder::new();
61+
62+
$attributes['name'] = "{$attributes['name']} Assets";
63+
$attributes['import_type'] = 'asset';
64+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
65+
$attributes['first_row'] = $fileBuilder->firstRow();
66+
67+
return $attributes;
68+
});
69+
}
70+
71+
/**
72+
* Create a component import type.
73+
*
74+
* @return static
75+
*/
76+
public function component()
77+
{
78+
return $this->state(function (array $attributes) {
79+
$fileBuilder = Importing\ComponentsImportFileBuilder::new();
80+
81+
$attributes['name'] = "{$attributes['name']} Components";
82+
$attributes['import_type'] = 'component';
83+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
84+
$attributes['first_row'] = $fileBuilder->firstRow();
85+
86+
return $attributes;
87+
});
88+
}
89+
90+
/**
91+
* Create a consumable import type.
92+
*
93+
* @return static
94+
*/
95+
public function consumable()
96+
{
97+
return $this->state(function (array $attributes) {
98+
$fileBuilder = Importing\ConsumablesImportFileBuilder::new();
99+
100+
$attributes['name'] = "{$attributes['name']} Consumables";
101+
$attributes['import_type'] = 'consumable';
102+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
103+
$attributes['first_row'] = $fileBuilder->firstRow();
104+
105+
return $attributes;
106+
});
107+
}
108+
109+
/**
110+
* Create a license import type.
111+
*
112+
* @return static
113+
*/
114+
public function license()
115+
{
116+
return $this->state(function (array $attributes) {
117+
$fileBuilder = Importing\LicensesImportFileBuilder::new();
118+
119+
$attributes['name'] = "{$attributes['name']} Licenses";
120+
$attributes['import_type'] = 'license';
121+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
122+
$attributes['first_row'] = $fileBuilder->firstRow();
123+
124+
return $attributes;
125+
});
126+
}
127+
128+
/**
129+
* Create a users import type.
130+
*
131+
* @return static
132+
*/
133+
public function users()
134+
{
135+
return $this->state(function (array $attributes) {
136+
$fileBuilder = Importing\UsersImportFileBuilder::new();
137+
138+
$attributes['name'] = "{$attributes['name']} Employees";
139+
$attributes['import_type'] = 'user';
140+
$attributes['header_row'] = $fileBuilder->toCsv()[0];
141+
$attributes['first_row'] = $fileBuilder->firstRow();
142+
143+
return $attributes;
144+
});
145+
}
146+
}

database/factories/UserFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Illuminate\Database\Eloquent\Factories\Factory;
88
use \Auth;
99

10+
/**
11+
* @extends Factory<User>
12+
*/
1013
class UserFactory extends Factory
1114
{
1215
/**
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tests\Feature\Importing\Api;
4+
5+
use App\Models\User;
6+
7+
class GeneralImportTest extends ImportDataTestCase
8+
{
9+
public function testRequiresExistingImport()
10+
{
11+
$this->actingAsForApi(User::factory()->canImport()->create());
12+
13+
$this->importFileResponse(['import' => 9999, 'import-type' => 'accessory'])
14+
->assertStatusMessageIs('import-errors');
15+
}
16+
}

0 commit comments

Comments
 (0)