Skip to content

Commit 8dd05ed

Browse files
committed
add tests 🔥
1 parent 962f2ae commit 8dd05ed

18 files changed

+424
-73
lines changed

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<testsuite name="Package Test Suite">
1414
<directory suffix=".php">./tests/</directory>
1515
<file phpVersion="8.1.0" phpVersionOperator=">=">./tests81/Unit/CrudPanel/CrudPanelUpdateTest.php</file>
16+
<file phpVersion="8.1.0" phpVersionOperator=">=">./tests81/Unit/CrudPanel/CrudPanelFieldsTest.php</file>
1617
</testsuite>
1718
</testsuites>
1819
<filter>

src/app/Library/CrudPanel/Traits/FieldsProtectedMethods.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,11 @@ protected function inferFieldTypeFromRelationType($relationType)
242242
switch ($relationType) {
243243
case 'BelongsTo':
244244
return 'select';
245-
break;
246-
247245
case 'BelongsToMany':
248246
case 'MorphToMany':
249247
return 'select_multiple';
250-
251248
default:
252249
return 'text';
253-
break;
254250
}
255251
}
256252

src/app/Library/CrudPanel/Traits/Relationships.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ public function guessIfFieldHasMultipleFromRelationType($relation_type)
209209
case 'MorphOneOrMany':
210210
case 'MorphToMany':
211211
return true;
212-
213212
default:
214213
return false;
215214
}
@@ -228,7 +227,6 @@ public function guessIfFieldHasPivotFromRelationType($relation_type)
228227
case 'HasManyThrough':
229228
case 'MorphToMany':
230229
return true;
231-
break;
232230
default:
233231
return false;
234232
}

tests/BaseTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,14 @@ protected function getPackageProviders($app)
2323
BackpackServiceProvider::class,
2424
];
2525
}
26+
27+
// allow us to run crud panel private/protected methods like `inferFieldTypeFromDbColumnType`
28+
public function invokeMethod(&$object, $methodName, array $parameters = [])
29+
{
30+
$reflection = new \ReflectionClass(get_class($object));
31+
$method = $reflection->getMethod($methodName);
32+
$method->setAccessible(true);
33+
34+
return $method->invokeArgs($object, $parameters);
35+
}
2636
}

tests/Unit/CrudPanel/CrudPanelAutoSetTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -868,14 +868,4 @@ public function testSetDoctrineTypesMapping()
868868
$type = $new_model_db_platform->getDoctrineTypeMapping('enum');
869869
$this->assertEquals('string', $type);
870870
}
871-
872-
// allow us to run crud panel private/protected methods like `inferFieldTypeFromDbColumnType`
873-
public function invokeMethod(&$object, $methodName, array $parameters = [])
874-
{
875-
$reflection = new \ReflectionClass(get_class($object));
876-
$method = $reflection->getMethod($methodName);
877-
$method->setAccessible(true);
878-
879-
return $method->invokeArgs($object, $parameters);
880-
}
881871
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
3+
4+
/**
5+
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Autofocus
6+
*/
7+
class CrudPanelAutofocusTest extends BaseCrudPanelTest
8+
{
9+
public function testItCanEnableAndDisableAutofocus()
10+
{
11+
$this->crudPanel->setOperation('create');
12+
$this->crudPanel->enableAutoFocus();
13+
$this->assertTrue($this->crudPanel->getAutoFocusOnFirstField());
14+
$this->crudPanel->disableAutofocus();
15+
$this->assertFalse($this->crudPanel->getAutoFocusOnFirstField());
16+
}
17+
}

tests/Unit/CrudPanel/CrudPanelColumnsTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
/**
88
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Columns
9+
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\ColumnsProtectedMethods
910
*/
1011
class CrudPanelColumnsTest extends BaseDBCrudPanelTest
1112
{

tests/Unit/CrudPanel/CrudPanelCreateTest.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,15 @@
99
use Backpack\CRUD\Tests\Unit\Models\PlanetNonNullable;
1010
use Backpack\CRUD\Tests\Unit\Models\Universe;
1111
use Backpack\CRUD\Tests\Unit\Models\User;
12+
use Backpack\CRUD\Tests\Unit\Models\Bill;
1213
use Faker\Factory;
1314
use Illuminate\Support\Arr;
1415

1516
/**
1617
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Create
1718
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Relationships
19+
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\FieldsProtectedMethods
20+
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Update
1821
*/
1922
class CrudPanelCreateTest extends BaseDBCrudPanelTest
2023
{
@@ -315,7 +318,6 @@ public function testGetRelationFieldsDotNotation()
315318

316319
//get all fields with a relation
317320
$relationFields = $this->crudPanel->getRelationFields();
318-
//var_dump($this->crudPanel->get('create.fields')['street']);
319321

320322
$this->assertEquals($this->crudPanel->get('create.fields')['street'], Arr::last($relationFields));
321323
}
@@ -340,6 +342,7 @@ public function testCreateHasOneRelations()
340342
],
341343
];
342344
$entry = $this->crudPanel->create($inputData);
345+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
343346
$account_details = $entry->accountDetails()->first();
344347

345348
$this->assertEquals($account_details->nickname, 'i_have_has_one');
@@ -381,7 +384,7 @@ public function testGetRelationFieldsWithPivotNoRelations()
381384

382385
$this->assertEmpty($relationFields);
383386
}
384-
387+
385388
public function testMorphToManySelectableRelationship()
386389
{
387390
$this->crudPanel->setModel(User::class);
@@ -398,6 +401,8 @@ public function testMorphToManySelectableRelationship()
398401
];
399402

400403
$entry = $this->crudPanel->create($inputData);
404+
405+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
401406

402407
$this->assertCount(1, $entry->bills);
403408

@@ -437,6 +442,7 @@ public function testMorphToManyCreatableRelationship()
437442
];
438443

439444
$entry = $this->crudPanel->create($inputData);
445+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
440446

441447
$this->assertCount(1, $entry->recommends);
442448

@@ -494,7 +500,8 @@ public function testBelongsToManyWithPivotDataRelationship()
494500
];
495501

496502
$entry = $this->crudPanel->create($inputData);
497-
503+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
504+
498505
$this->assertCount(1, $entry->fresh()->superArticles);
499506
$this->assertEquals('my first article note', $entry->fresh()->superArticles->first()->pivot->notes);
500507
}
@@ -580,6 +587,7 @@ public function testCreateHasOneWithNestedRelationsRepeatableInterface()
580587
];
581588

582589
$entry = $this->crudPanel->create($inputData);
590+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
583591
$account_details = $entry->accountDetails()->first();
584592

585593
$this->assertEquals($account_details->article, Article::find(1));
@@ -625,6 +633,7 @@ public function testCreateBelongsToFake()
625633
];
626634

627635
$entry = $this->crudPanel->create($inputData);
636+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
628637
$this->crudPanel->entry = $entry->withFakes();
629638
$this->assertEquals($entry->bang_relation_field, 1);
630639
}
@@ -705,6 +714,7 @@ public function testCreateHasOneWithNestedRelations()
705714
];
706715

707716
$entry = $this->crudPanel->create($inputData);
717+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
708718
$account_details = $entry->accountDetails()->first();
709719

710720
$this->assertEquals($account_details->article, Article::find(1));
@@ -773,6 +783,7 @@ public function testMorphOneRelationship()
773783
];
774784

775785
$entry = $this->crudPanel->create($inputData);
786+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
776787

777788
$this->assertEquals($inputData['comment']['text'], $entry->comment->text);
778789

@@ -815,6 +826,7 @@ public function testMorphManyCreatableRelationship()
815826
];
816827

817828
$entry = $this->crudPanel->create($inputData);
829+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
818830

819831
$this->assertCount(2, $entry->stars);
820832

@@ -866,6 +878,7 @@ public function testHasManyCreatableRelationship()
866878
];
867879

868880
$entry = $this->crudPanel->create($inputData);
881+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
869882

870883
$this->assertCount(2, $entry->universes);
871884

@@ -942,6 +955,7 @@ public function testHasManySelectableRelationshipWithoutForceDelete()
942955
];
943956

944957
$entry = $this->crudPanel->create($inputData);
958+
$updateFields = $this->crudPanel->getUpdateFields($entry->id);
945959

946960
$this->assertCount(2, $entry->planets);
947961

@@ -1384,5 +1398,7 @@ public function testBelongsToManyWithArrayedNameSubfields()
13841398
$superArticle = $entry->fresh()->superArticles->first();
13851399
$this->assertEquals($superArticle->pivot->start_date, '2021-02-26');
13861400
$this->assertEquals($superArticle->pivot->end_date, '2091-01-26');
1401+
1402+
$this->crudPanel->getUpdateFields($superArticle->id);
13871403
}
13881404
}

tests/Unit/CrudPanel/CrudPanelDeleteTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,10 @@ public function testDeleteUnknown()
3535

3636
$this->crudPanel->delete($unknownId);
3737
}
38+
39+
public function testItAddsTheBulkDeleteButton()
40+
{
41+
$this->crudPanel->addBulkDeleteButton();
42+
$this->assertCount(1, $this->crudPanel->buttons());
43+
}
3844
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
namespace Backpack\CRUD\Tests\Unit\CrudPanel;
3+
4+
/**
5+
* @covers Backpack\CRUD\app\Library\CrudPanel\Traits\Errors
6+
*/
7+
class CrudPanelErrorsTest extends BaseCrudPanelTest
8+
{
9+
public function testItCanEnableAndDisableInlineErrors()
10+
{
11+
$this->crudPanel->setOperation('create');
12+
$this->crudPanel->enableInlineErrors();
13+
$this->assertTrue($this->crudPanel->inlineErrorsEnabled());
14+
$this->crudPanel->disableInlineErrors();
15+
$this->assertFalse($this->crudPanel->inlineErrorsEnabled());
16+
}
17+
18+
public function testItCanEnableAndDisableGroupedErrors()
19+
{
20+
$this->crudPanel->setOperation('create');
21+
$this->crudPanel->enableGroupedErrors();
22+
$this->assertTrue($this->crudPanel->groupedErrorsEnabled());
23+
$this->crudPanel->disableGroupedErrors();
24+
$this->assertFalse($this->crudPanel->groupedErrorsEnabled());
25+
}
26+
}

0 commit comments

Comments
 (0)