Cada aserción es una Constraint de PHPUnit. Los métodos assert* solo la crean y llaman a assertThat():
public static function assertRouteRegistered(string $name, string $message = ''): void
{
static::assertThat($name, new RouteIsRegistered, $message);
}
Puedes usar las constraints directamente:
use Wikigods\LaravelAssertions\Constraints\Classes\UsesTrait;
use Wikigods\LaravelAssertions\Constraints\Models\HasRouteKeyName;
$this->assertThat(Post::class, new UsesTrait(SoftDeletes::class));
$this->assertThat(Post::class, new HasRouteKeyName('slug'));
| Constraint | Valor que recibe assertThat() |
|---|---|
Routes\RouteIsRegistered |
nombre de la ruta |
Classes\UsesTrait($trait) |
clase u objeto |
Models\HasRouteKeyName($expected) |
modelo o clase |
Models\HasCasts($expected) |
modelo o clase |
Models\HasAppends($expected) |
modelo o clase |
Models\HasUuidKey |
modelo |
Relationships\IsRelationship($relation, $type, $related, $keys, $through) |
modelo o clase |
Broadcasting\DoesNotBroadcastToCurrentUser($socketId) |
evento |
Broadcasting\BroadcastsOnChannelType($type) |
evento |
Broadcasting\BroadcastsOnChannelName($name) |
evento |
Views\ViewExists |
nombre de la vista |
Todas están en el namespace Wikigods\LaravelAssertions\Constraints.
Constraints\Not niega cualquier constraint del paquete y mantiene sus mensajes:
use Wikigods\LaravelAssertions\Constraints\Not;
$this->assertThat('posts.destroy', new Not(new RouteIsRegistered));
// The [posts.destroy] route should not be registered.
También funcionan con LogicalNot de PHPUnit. En ese caso el mensaje lleva el prefijo de PHPUnit:
$this->assertThat('posts.index', $this->logicalNot(new RouteIsRegistered));
// Failed asserting that The [posts.index] route should not be registered.
Con logicalAnd() y logicalOr() de PHPUnit:
$this->assertThat(Post::class, $this->logicalAnd(
new UsesTrait(SoftDeletes::class),
new HasRouteKeyName('slug'),
));
Es la constraint de todas las aserciones de relaciones. Las claves que se pueden comprobar:
| Clave | Método de la relación |
|---|---|
foreign_key |
getForeignKeyName() |
owner_key |
getOwnerKeyName() |
local_key |
getLocalKeyName() |
table |
getTable() |
foreign_pivot_key |
getForeignPivotKeyName() |
related_pivot_key |
getRelatedPivotKeyName() |
first_key |
getFirstKeyName() |
second_key |
getForeignKeyName() |
second_local_key |
getSecondLocalKeyName() |
Sirve para relaciones que no tienen aserción propia:
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Wikigods\LaravelAssertions\Constraints\Relationships\IsRelationship;
$this->assertThat(Post::class, new IsRelationship('comments', MorphMany::class, Comment::class));
Extiende Wikigods\LaravelAssertions\Constraints\Constraint. Implementa matches() y message(), y negatedMessage() si se va a negar:
use Wikigods\LaravelAssertions\Constraints\Constraint;
final class IsFillable extends Constraint
{
public function __construct(private readonly string $attribute) {}
protected function matches(mixed $other): bool
{
return (new $other)->isFillable($this->attribute);
}
protected function message(mixed $other): string
{
return "{$this->attribute} should be fillable in ".class_basename($other).'.';
}
protected function negatedMessage(mixed $other): string
{
return "{$this->attribute} should not be fillable in ".class_basename($other).'.';
}
}