Skip to content

Commit 92b7de2

Browse files
authored
Merge pull request #32
1 parent 3994967 commit 92b7de2

10 files changed

+72
-63
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
}
2020
],
2121
"require": {
22-
"php": "^7.4|^8.0",
23-
"illuminate/support": "^8.0"
22+
"php": "^8.0",
23+
"illuminate/support": "^9.0"
2424
},
2525
"require-dev": {
26-
"orchestra/testbench": "^6.0",
26+
"orchestra/testbench": "^7.0",
2727
"phpunit/phpunit": "^9.0",
28-
"friendsofphp/php-cs-fixer": "^2.16"
28+
"friendsofphp/php-cs-fixer": "^3.6"
2929
},
3030
"autoload": {
3131
"psr-4": {

database/migrations/create_custom_fields_tables.php.stub

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema;
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Database\Migrations\Migration;
66

7-
class CreateCustomFieldsTables extends Migration
7+
return new class extends Migration
88
{
99
/**
1010
* Run the migrations.
@@ -52,4 +52,4 @@ class CreateCustomFieldsTables extends Migration
5252
Schema::dropIfExists(config('custom-fields.tables.fields', 'custom_fields'));
5353
Schema::dropIfExists(config('custom-fields.tables.field_responses', 'custom_field_responses'));
5454
}
55-
}
55+
};

phpunit.xml.dist

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="Givebutter Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false">
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">src/</directory>
17+
</include>
18+
</coverage>
19+
<testsuites>
20+
<testsuite name="Givebutter Test Suite">
21+
<directory>tests</directory>
22+
</testsuite>
23+
</testsuites>
2224
</phpunit>

src/Models/CustomField.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Givebutter\LaravelCustomFields\Models;
44

5-
use Carbon\Carbon;
5+
use Illuminate\Database\Eloquent\Casts\Attribute;
66
use Illuminate\Database\Eloquent\Factories\HasFactory;
77
use Illuminate\Database\Eloquent\Model;
88
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -155,15 +155,16 @@ public function unarchive()
155155
/**
156156
* Get the validation rules attribute.
157157
*
158-
* @return mixed
158+
* @return Attribute
159159
*/
160-
public function getValidationRulesAttribute()
160+
public function validationRules(): Attribute
161161
{
162-
$typeRules = $this->getFieldValidationRules($this->required)[$this->type];
163-
164-
array_unshift($typeRules, $this->required ? 'required' : 'nullable');
165-
166-
return $typeRules;
162+
return new Attribute(
163+
get: fn ($value, $attributes) => [
164+
$attributes['required'] ? 'required' : 'nullable',
165+
...$this->getFieldValidationRules($attributes['required'])[$attributes['type']]
166+
],
167+
);
167168
}
168169

169170
/**

src/Models/CustomFieldResponse.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,6 @@ public function getValueAttribute()
119119
);
120120
}
121121

122-
/**
123-
* @return mixed|string
124-
*/
125-
public function getValueFriendlyAttribute()
126-
{
127-
if ($this->field->type === 'checkbox') {
128-
return $this->value ? 'Checked' : 'Unchecked';
129-
}
130-
131-
return $this->value;
132-
}
133-
134122
/**
135123
* @param $value
136124
*/
@@ -144,6 +132,22 @@ public function setValueAttribute($value)
144132
$this->attributes[$this->valueField()] = $this->formatValue($value);
145133
}
146134

135+
/**
136+
* Get the `value_friendly` attribute.
137+
*
138+
* @param $query
139+
* @param $value
140+
* @return mixed|string
141+
*/
142+
public function getValueFriendlyAttribute()
143+
{
144+
if ($this->field->type === 'checkbox') {
145+
return $this->value ? 'Checked' : 'Unchecked';
146+
}
147+
148+
return $this->value;
149+
}
150+
147151
/**
148152
* @return string
149153
*/

src/Traits/HasCustomFieldResponses.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ public function saveCustomFields($fields)
3131
continue;
3232
}
3333

34-
CustomFieldResponse::create([
34+
$customFieldResponse = new CustomFieldResponse([
3535
'value' => $value,
3636
'model_id' => $this->id,
3737
'field_id' => $customField->id,
3838
'model_type' => get_class($this),
3939
]);
40+
41+
$customFieldResponse->save();
4042
}
4143
}
4244

tests/Feature/CustomFieldControllerTest.php

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

33
namespace Givebutter\Tests\Feature;
44

5-
use Givebutter\LaravelCustomFields\Models\CustomField;
5+
use Illuminate\Http\Request;
6+
use Givebutter\Tests\TestCase;
67
use Givebutter\Tests\Support\Survey;
8+
use Illuminate\Support\Facades\Route;
79
use Givebutter\Tests\Support\SurveyResponse;
8-
use Givebutter\Tests\TestCase;
910
use Illuminate\Foundation\Testing\RefreshDatabase;
10-
use Illuminate\Http\Request;
11-
use Illuminate\Support\Facades\Route;
11+
use Givebutter\LaravelCustomFields\Models\CustomField;
1212

1313
class CustomFieldControllerTest extends TestCase
1414
{
@@ -124,7 +124,7 @@ public function checkbox_can_pass_validation()
124124
return ['errors' => $validator->errors()];
125125
}
126126

127-
$surveyResponse->saveCustomfields($request->get('custom_fields'));
127+
$surveyResponse->saveCustomFields($request->get('custom_fields'));
128128

129129
return response('All good', 200);
130130
});
@@ -155,7 +155,7 @@ public function fields_can_be_saved_from_request_with_convenience_method()
155155
);
156156

157157
Route::post("/surveys/{$survey->id}/responses", function () use ($surveyResponse) {
158-
$surveyResponse->saveCustomfields(request('custom_fields'));
158+
$surveyResponse->saveCustomFields(request('custom_fields'));
159159

160160
return response('All good', 200);
161161
});

tests/Feature/HasCustomFieldResponsesTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function custom_fields_responses_can_be_created_and_accessed_on_models_wi
2626

2727
$customFieldModel->customFields()->save($customField);
2828

29-
$customFieldResponse = CustomFieldResponse::make([
29+
$customFieldResponse = new CustomFieldResponse([
3030
'model_id' => $customFieldResponseModel->id,
3131
'model_type' => get_class($customFieldResponseModel),
3232
'field_id' => $customField->fresh()->id,
@@ -51,14 +51,14 @@ public function whereField_method_allows_filtering_responses()
5151
'model_type' => get_class($customFieldModel),
5252
]);
5353

54-
$firstResponse = CustomFieldResponse::create([
54+
$firstResponse = new CustomFieldResponse([
5555
'model_id' => $firstResponseModel->id,
5656
'model_type' => get_class($firstResponseModel),
5757
'field_id' => $firstField->id,
5858
'value_str' => 'Hit Em Up',
5959
]);
6060

61-
$secondResponse = CustomFieldResponse::create([
61+
$secondResponse = new CustomFieldResponse([
6262
'model_id' => $secondResponseModel->id,
6363
'model_type' => get_class($secondResponseModel),
6464
'field_id' => $firstField->id,
@@ -90,7 +90,7 @@ public function value_getter_and_setter_work_fine()
9090

9191
$customFieldModel->customFields()->save($customField);
9292

93-
$customFieldResponse = CustomFieldResponse::make([
93+
$customFieldResponse = new CustomFieldResponse([
9494
'model_id' => $customFieldResponseModel->id,
9595
'model_type' => get_class($customFieldResponseModel),
9696
'field_id' => $customField->fresh()->id,

tests/Support/Migrations/create_surveys_and_survey_responses_tables.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use Illuminate\Database\Schema\Blueprint;
55
use Illuminate\Support\Facades\Schema;
66

7-
class CreateSurveysAndSurveyResponsesTables extends Migration
7+
return new class extends Migration
88
{
99
public function up()
1010
{
@@ -24,4 +24,4 @@ public function down()
2424
Schema::dropIfExists('surveys');
2525
Schema::dropIfExists('survey_responses');
2626
}
27-
}
27+
};

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ protected function setUpDatabase($app)
5757

5858
protected function runMigrationStub()
5959
{
60-
include_once __DIR__ . '/../database/migrations/create_custom_fields_tables.php.stub';
61-
(new \CreateCustomFieldsTables())->up();
60+
(include __DIR__ . '/../database/migrations/create_custom_fields_tables.php.stub')
61+
->up();
6262
}
6363

6464
protected function prepareDatabaseForHasCustomFieldsModel()
6565
{
66-
include_once __DIR__ . '/../tests/support/migrations/create_surveys_and_survey_responses_tables.php';
67-
(new \CreateSurveysAndSurveyResponsesTables())->up();
66+
(include __DIR__ . '/../tests/support/migrations/create_surveys_and_survey_responses_tables.php')
67+
->up();
6868
}
6969

7070
protected function resetDatabase()

0 commit comments

Comments
 (0)