Skip to content

Commit 8af76a5

Browse files
committed
KnowledgeEquityResponse Model and Migration
Adds a Model, Migration and some happy path tests. n.b. this uses the laravel Blueprint[1] rather than manually defining tables and columns. [1] https://laravel.com/docs/10.x/migrations Bug: T419209
1 parent a89378e commit 8af76a5

3 files changed

Lines changed: 114 additions & 0 deletions

File tree

app/KnowledgeEquityResponse.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
8+
9+
class KnowledgeEquityResponse extends Model {
10+
use HasFactory;
11+
12+
protected $fillable = [
13+
'wiki_id',
14+
'selectedOption',
15+
'freeTextResponse',
16+
];
17+
18+
public function wiki(): BelongsTo {
19+
return $this->belongsTo(Wiki::class);
20+
}
21+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration {
8+
/**
9+
* Run the migrations.
10+
*/
11+
public function up(): void {
12+
Schema::create('knowledge_equity_responses', function (Blueprint $table) {
13+
$table->id();
14+
$table->timestamps();
15+
$table->unsignedInteger('wiki_id');
16+
$table->foreign('wiki_id')->references('id')->on('wikis');
17+
$table->enum('selectedOption', ['yes', 'no', 'unsure', 'unsaid']);
18+
$table->string('freeTextResponse', 3000)->nullable();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void {
26+
Schema::dropIfExists('knowledge_equity_responses');
27+
}
28+
};
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use App\KnowledgeEquityResponse;
6+
use App\Wiki;
7+
use Illuminate\Foundation\Testing\RefreshDatabase;
8+
use Str;
9+
10+
class KnowledgeEquityResponseTest extends TestCase {
11+
use RefreshDatabase;
12+
13+
protected Wiki $wiki;
14+
15+
protected function setUp(): void {
16+
parent::setUp();
17+
$this->wiki = Wiki::factory()->create();
18+
}
19+
20+
public function testCreateValidKnowledgeEquityResponse(): void {
21+
$knowledgeEquityResponse = new KnowledgeEquityResponse([
22+
'wiki_id' => $this->wiki->id,
23+
'selectedOption' => 'yes',
24+
'freeTextResponse' => 'Because it just does',
25+
]);
26+
27+
$knowledgeEquityResponse->save();
28+
29+
$this->assertDatabaseHas('knowledge_equity_responses', [
30+
'wiki_id' => $this->wiki->id,
31+
'selectedOption' => 'yes',
32+
'freeTextResponse' => 'Because it just does',
33+
]);
34+
}
35+
36+
public function testCreateValidKnowledgeEquityResponseNoFreeText(): void {
37+
$knowledgeEquityResponse = new KnowledgeEquityResponse([
38+
'wiki_id' => $this->wiki->id,
39+
'selectedOption' => 'yes',
40+
]);
41+
42+
$knowledgeEquityResponse->save();
43+
44+
$this->assertDatabaseHas('knowledge_equity_responses', [
45+
'wiki_id' => $this->wiki->id,
46+
'selectedOption' => 'yes',
47+
]);
48+
}
49+
50+
public function testCreateValidKnowledgeEquityResponse3000CharFreeText(): void {
51+
$longFreeText = Str::random(3000);
52+
$knowledgeEquityResponse = new KnowledgeEquityResponse([
53+
'wiki_id' => $this->wiki->id,
54+
'selectedOption' => 'yes',
55+
'freeTextResponse' => $longFreeText,
56+
]);
57+
58+
$knowledgeEquityResponse->save();
59+
60+
$this->assertDatabaseHas('knowledge_equity_responses', [
61+
'wiki_id' => $this->wiki->id,
62+
'selectedOption' => 'yes',
63+
]);
64+
}
65+
}

0 commit comments

Comments
 (0)