ModelTestCase.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Tests;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  6. use Illuminate\Database\Eloquent\Relations\HasMany;
  7. // https://github.com/framgia/laravel-test-examples/blob/master/tests/ModelTestCase.php
  8. abstract class ModelTestCase extends TestCase
  9. {
  10. /**
  11. * @param array $fillable
  12. * @param array $guarded
  13. * @param array $hidden
  14. * @param array $visible
  15. * @param array $casts
  16. * @param array $dates
  17. * @param string $collectionClass
  18. * @param null $table
  19. * @param string $primaryKey
  20. * @param bool $incrementing
  21. *
  22. * - `$fillable` -> `getFillable()`
  23. * - `$guarded` -> `getGuarded()`
  24. * - `$table` -> `getTable()`
  25. * - `$primaryKey` -> `getKeyName()`
  26. * - `$hidden` -> `getHidden()`
  27. * - `$visible` -> `getVisible()`
  28. * - `$casts` -> `getCasts()`: note that method appends incrementing key.
  29. * - `$dates` -> `getDates()`: note that method appends `[static::CREATED_AT, static::UPDATED_AT]`.
  30. * - `newCollection()`: assert collection is exact type. Use `assertEquals` on `get_class()` result, but not `assertInstanceOf`.
  31. */
  32. protected function runConfigurationAssertions(
  33. Model $model,
  34. $fillable = [],
  35. $hidden = [],
  36. $guarded = ['*'],
  37. $visible = [],
  38. $casts = ['id' => 'int'],
  39. $dispatchesEvents = [],
  40. $dates = ['created_at', 'updated_at'],
  41. $collectionClass = Collection::class,
  42. $table = null,
  43. $primaryKey = 'id',
  44. $incrementing = true)
  45. {
  46. $this->assertEquals($fillable, $model->getFillable());
  47. $this->assertEquals($guarded, $model->getGuarded());
  48. $this->assertEquals($hidden, $model->getHidden());
  49. $this->assertEquals($visible, $model->getVisible());
  50. $this->assertEquals($casts, $model->getCasts());
  51. $this->assertEquals($dates, $model->getDates());
  52. $this->assertEquals($primaryKey, $model->getKeyName());
  53. $this->assertEquals($incrementing, $model->getIncrementing());
  54. $eventDispatcher = $model->getEventDispatcher();
  55. foreach ($dispatchesEvents as $eventName => $eventclass) {
  56. $this->assertTrue($eventDispatcher->hasListeners($eventclass));
  57. }
  58. $c = $model->newCollection();
  59. $this->assertEquals($collectionClass, get_class($c));
  60. $this->assertInstanceOf(Collection::class, $c);
  61. if ($table !== null) {
  62. $this->assertEquals($table, $model->getTable());
  63. }
  64. }
  65. /**
  66. * @param HasMany $relation
  67. * @param string $key
  68. * @param string $parent
  69. * @param \Closure $queryCheck
  70. *
  71. * - `getQuery()`: assert query has not been modified or modified properly.
  72. * - `getForeignKey()`: any `HasOneOrMany` or `BelongsTo` relation, but key type differs (see documentaiton).
  73. * - `getQualifiedParentKeyName()`: in case of `HasOneOrMany` relation, there is no `getLocalKey()` method, so this one should be asserted.
  74. */
  75. protected function assertHasManyRelation($relation, Model $model, Model $related, $key = null, $parent = null, \Closure $queryCheck = null)
  76. {
  77. $this->assertInstanceOf(HasMany::class, $relation);
  78. if (! is_null($queryCheck)) {
  79. $queryCheck->bindTo($this);
  80. $queryCheck($relation->getQuery(), $model, $relation);
  81. }
  82. if (is_null($key)) {
  83. $key = $model->getForeignKey();
  84. }
  85. $this->assertEquals($key, $relation->getForeignKeyName());
  86. if (is_null($parent)) {
  87. $parent = $model->getKeyName();
  88. }
  89. $this->assertEquals($model->getTable() . '.' . $parent, $relation->getQualifiedParentKeyName());
  90. }
  91. /**
  92. * @param BelongsTo $relation
  93. * @param string $key
  94. * @param string $owner
  95. * @param \Closure $queryCheck
  96. *
  97. * - `getQuery()`: assert query has not been modified or modified properly.
  98. * - `getForeignKey()`: any `HasOneOrMany` or `BelongsTo` relation, but key type differs (see documentaiton).
  99. * - `getOwnerKey()`: `BelongsTo` relation and its extendings.
  100. */
  101. protected function assertBelongsToRelation($relation, Model $model, Model $related, $key, $owner = null, \Closure $queryCheck = null)
  102. {
  103. $this->assertInstanceOf(BelongsTo::class, $relation);
  104. if (! is_null($queryCheck)) {
  105. $queryCheck->bindTo($this);
  106. $queryCheck($relation->getQuery(), $model, $relation);
  107. }
  108. $this->assertEquals($key, $relation->getForeignKey());
  109. if (is_null($owner)) {
  110. $owner = $related->getKeyName();
  111. }
  112. $this->assertEquals($owner, $relation->getOwnerKey());
  113. }
  114. }