Object Assertions
Object assertions validate objects, classes, interfaces, and their properties/methods.
Available Assertions
Section titled “Available Assertions”isInstanceOf()
Section titled “isInstanceOf()”Assert that a value is an instance of a given class.
use Cline\Assert\Assertions\Assertion;
Assertion::isInstanceOf($user, User::class);Assertion::isInstanceOf($model, Model::class, 'Expected Model instance');notIsInstanceOf()
Section titled “notIsInstanceOf()”Assert that a value is NOT an instance of a given class.
Assertion::notIsInstanceOf($value, LegacyUser::class);classExists()
Section titled “classExists()”Assert that a class exists.
Assertion::classExists(User::class);Assertion::classExists($className, 'Class does not exist');interfaceExists()
Section titled “interfaceExists()”Assert that an interface exists.
Assertion::interfaceExists(UserInterface::class);subclassOf()
Section titled “subclassOf()”Assert that a class is a subclass of another.
Assertion::subclassOf(AdminUser::class, User::class);implementsInterface()
Section titled “implementsInterface()”Assert that a class implements an interface.
Assertion::implementsInterface(User::class, UserInterface::class);methodExists()
Section titled “methodExists()”Assert that a method exists on an object.
Assertion::methodExists('save', $model);Assertion::methodExists('handle', $handler, 'Handler must have handle method');propertyExists()
Section titled “propertyExists()”Assert that a property exists on an object or class.
Assertion::propertyExists($user, 'email');propertiesExist()
Section titled “propertiesExist()”Assert that multiple properties exist.
$required = ['id', 'name', 'email'];Assertion::propertiesExist($user, $required);Chaining Object Assertions
Section titled “Chaining Object Assertions”use Cline\Assert\Assert;
Assert::that($user) ->isObject() ->isInstanceOf(User::class);
Assert::that(User::class) ->classExists() ->implementsInterface(UserInterface::class);Common Patterns
Section titled “Common Patterns”Dependency Injection Validation
Section titled “Dependency Injection Validation”Assert::that($logger) ->isObject() ->implementsInterface(LoggerInterface::class);Model Validation
Section titled “Model Validation”Assert::that($model) ->isObject() ->isInstanceOf(Model::class) ->propertyExists('id') ->propertyExists('created_at');Plugin Validation
Section titled “Plugin Validation”Assert::that($plugin) ->isObject() ->implementsInterface(PluginInterface::class) ->methodExists('register') ->methodExists('boot');Factory Pattern Validation
Section titled “Factory Pattern Validation”public function make(string $class){ Assertion::classExists($class); Assertion::subclassOf($class, BaseService::class);
return new $class();}Working with Interfaces
Section titled “Working with Interfaces”Assertion::implementsInterface(JsonSerializer::class, Serializer::class);
Assert::that($serializer) ->isObject() ->isInstanceOf(Serializer::class);Best Practices
Section titled “Best Practices”Interface Over Implementation
Section titled “Interface Over Implementation”// Tightly coupledAssert::that($logger)->isInstanceOf(MonologLogger::class);
// Depends on interfaceAssert::that($logger)->isInstanceOf(LoggerInterface::class);Combine with Method Checks
Section titled “Combine with Method Checks”Assert::that($handler) ->isObject() ->isInstanceOf(HandlerInterface::class) ->methodExists('handle');Next Steps
Section titled “Next Steps”- Type Assertions - Basic type checking
- Custom Assertions - Custom object validation
- Lazy Assertions - Validate multiple properties