Skip to content

Commit e05401f

Browse files
Merge pull request #27 from givebutter/get-to-green
Finish fixing factories + bug fixes in tests
2 parents 377d315 + d497600 commit e05401f

File tree

7 files changed

+32
-39
lines changed

7 files changed

+32
-39
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"autoload-dev": {
3636
"psr-4": {
37-
"Givebutter\\Tests\\": "tests/"
37+
"Givebutter\\Tests\\": "tests/",
38+
"Database\\": "database/"
3839
}
3940
},
4041
"scripts": {

tests/Feature/CustomFieldControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function valid_data_passes_controller_validation()
1919
{
2020
$survey = Survey::create();
2121
$survey->customfields()->save(
22-
factory(CustomField::class)->make([
22+
CustomField::factory()->make([
2323
'title' => 'email',
2424
'type' => 'text',
2525
])
@@ -45,15 +45,15 @@ public function invalid_data_throws_validation_exception()
4545
{
4646
$survey = Survey::create();
4747
$survey->customfields()->save(
48-
factory(CustomField::class)->make([
48+
CustomField::factory()->make([
4949
'title' => 'favorite_album',
5050
'type' => 'select',
5151
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],
5252
])
5353
);
5454

5555
Route::post("/surveys/{$survey->id}/responses", function (Request $request) use ($survey) {
56-
$validator = $survey->validateCustomFields($request);
56+
$validator = $survey->validateCustomFields($request->custom_fields);
5757

5858
if ($validator->fails()) {
5959
return ['errors' => $validator->errors()];
@@ -77,7 +77,7 @@ public function non_required_fields_can_be_left_null_for_validation()
7777
{
7878
$survey = Survey::create();
7979
$survey->customfields()->save(
80-
factory(CustomField::class)->make([
80+
CustomField::factory()->make([
8181
'title' => 'favorite_album',
8282
'type' => 'select',
8383
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],
@@ -111,7 +111,7 @@ public function checkbox_can_pass_validation()
111111
$survey = Survey::create();
112112
$surveyResponse = SurveyResponse::create();
113113
$survey->customfields()->save(
114-
factory(CustomField::class)->make([
114+
CustomField::factory()->make([
115115
'title' => 'favorite_album',
116116
'type' => 'checkbox',
117117
])
@@ -147,7 +147,7 @@ public function fields_can_be_saved_from_request_with_convenience_method()
147147
$survey = Survey::create();
148148
$surveyResponse = SurveyResponse::create();
149149
$survey->customfields()->save(
150-
factory(CustomField::class)->make([
150+
CustomField::factory()->make([
151151
'title' => 'favorite_album',
152152
'type' => 'select',
153153
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],

tests/Feature/HasCustomFieldResponsesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function custom_fields_responses_can_be_created_and_accessed_on_models_wi
1919
$customFieldModel = Survey::create();
2020
$customFieldResponseModel = SurveyResponse::create();
2121

22-
$customField = factory(CustomField::class)->make([
22+
$customField = CustomField::factory()->make([
2323
'model_id' => $customFieldModel->id,
2424
'model_type' => get_class($customFieldModel),
2525
]);
@@ -46,7 +46,7 @@ public function whereField_method_allows_filtering_responses()
4646
$firstResponseModel = SurveyResponse::create();
4747
$secondResponseModel = SurveyResponse::create();
4848

49-
$firstField = factory(CustomField::class)->create([
49+
$firstField = CustomField::factory()->create([
5050
'model_id' => $customFieldModel->id,
5151
'model_type' => get_class($customFieldModel),
5252
]);
@@ -82,7 +82,7 @@ public function value_getter_and_setter_work_fine()
8282
$customFieldModel = Survey::create();
8383
$customFieldResponseModel = SurveyResponse::create();
8484

85-
$customField = factory(CustomField::class)->make([
85+
$customField = CustomField::factory()->make([
8686
'model_id' => $customFieldModel->id,
8787
'model_type' => get_class($customFieldModel),
8888
'type' => 'text',

tests/Feature/HasCustomFieldsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function custom_fields_can_be_created_and_accessed_on_models_with_trait()
1616
{
1717
$model = Survey::create();
1818

19-
$customField = factory(CustomField::class)->make([
19+
$customField = CustomField::factory()->make([
2020
'model_id' => $model->id,
2121
'model_type' => get_class($model),
2222
'description' => 'Lil Wayne',

tests/Feature/OrderingTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public function new_fields_are_ordered_by_default()
1818
{
1919
$survey = Survey::create();
2020
$survey->customfields()->saveMany([
21-
factory(CustomField::class)->make([
21+
CustomField::factory()->make([
2222
'title' => 'email',
2323
'type' => 'text',
2424
]),
25-
factory(CustomField::class)->make([
25+
CustomField::factory()->make([
2626
'title' => 'phone',
2727
'type' => 'text',
2828
]),
@@ -37,11 +37,11 @@ public function order_function_replaces_field_orders()
3737
{
3838
$survey = Survey::create();
3939
$survey->customfields()->saveMany([
40-
factory(CustomField::class)->make([
40+
CustomField::factory()->make([
4141
'title' => 'email',
4242
'type' => 'text',
4343
]),
44-
factory(CustomField::class)->make([
44+
CustomField::factory()->make([
4545
'title' => 'phone',
4646
'type' => 'text',
4747
]),
@@ -58,11 +58,11 @@ public function order_function_throws_exception_for_wrong_number_of_ids()
5858
{
5959
$survey = Survey::create();
6060
$survey->customfields()->saveMany([
61-
factory(CustomField::class)->make([
61+
CustomField::factory()->make([
6262
'title' => 'email',
6363
'type' => 'text',
6464
]),
65-
factory(CustomField::class)->make([
65+
CustomField::factory()->make([
6666
'title' => 'phone',
6767
'type' => 'text',
6868
]),
@@ -79,23 +79,23 @@ public function order_function_throws_exception_if_passed_fields_not_belonging_t
7979
{
8080
$survey1 = Survey::create();
8181
$survey1->customfields()->saveMany([
82-
factory(CustomField::class)->make([
82+
CustomField::factory()->make([
8383
'title' => 'email',
8484
'type' => 'text',
8585
]),
86-
factory(CustomField::class)->make([
86+
CustomField::factory()->make([
8787
'title' => 'phone',
8888
'type' => 'text',
8989
]),
9090
]);
9191

9292
$survey2 = Survey::create();
9393
$survey2->customfields()->saveMany([
94-
factory(CustomField::class)->make([
94+
CustomField::factory()->make([
9595
'title' => 'fax',
9696
'type' => 'text',
9797
]),
98-
factory(CustomField::class)->make([
98+
CustomField::factory()->make([
9999
'title' => 'telegraph',
100100
'type' => 'text',
101101
]),

tests/Support/Factories/CustomFieldFactory.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

tests/TestCase.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Givebutter\Tests;
44

55
use Givebutter\LaravelCustomFields\LaravelCustomFieldsServiceProvider;
6+
use Illuminate\Database\Eloquent\Factories\Factory;
67
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Str;
79
use Orchestra\Testbench\TestCase as OrchestraTestCase;
810

911
class TestCase extends OrchestraTestCase
@@ -12,7 +14,14 @@ public function setUp(): void
1214
{
1315
parent::setUp();
1416

15-
$this->withFactories(__DIR__ . '/Support/Factories');
17+
Factory::guessFactoryNamesUsing(function (string $modelName) {
18+
$namespace = 'Database\\Factories\\';
19+
20+
$modelName = Str::afterLast($modelName, '\\');
21+
22+
return $namespace.$modelName.'Factory';
23+
});
24+
1625
$this->setUpDatabase($this->app);
1726
$this->withoutExceptionHandling();
1827
}

0 commit comments

Comments
 (0)