EloquentMorphToTouchesTest.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database\EloquentMorphToTouchesTest;
  3. use DB;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Schema\Blueprint;
  6. use Illuminate\Support\Facades\Schema;
  7. use Illuminate\Tests\Integration\Database\DatabaseTestCase;
  8. class EloquentMorphToTouchesTest extends DatabaseTestCase
  9. {
  10. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  11. {
  12. Schema::create('posts', function (Blueprint $table) {
  13. $table->increments('id');
  14. $table->timestamps();
  15. });
  16. Schema::create('comments', function (Blueprint $table) {
  17. $table->increments('id');
  18. $table->nullableMorphs('commentable');
  19. });
  20. Post::create();
  21. }
  22. public function testNotNull()
  23. {
  24. $comment = (new Comment)->commentable()->associate(Post::first());
  25. DB::enableQueryLog();
  26. $comment->save();
  27. $this->assertCount(2, DB::getQueryLog());
  28. }
  29. public function testNull()
  30. {
  31. DB::enableQueryLog();
  32. Comment::create();
  33. $this->assertCount(1, DB::getQueryLog());
  34. }
  35. }
  36. class Comment extends Model
  37. {
  38. public $timestamps = false;
  39. protected $touches = ['commentable'];
  40. public function commentable()
  41. {
  42. return $this->morphTo(null, null, null, 'id');
  43. }
  44. }
  45. class Post extends Model
  46. {
  47. //
  48. }