Add assertions for multiple content parts
This commit is contained in:
parent
5826c455a7
commit
2ffab167ea
102
src/Document.php
102
src/Document.php
|
@ -2,7 +2,12 @@
|
|||
|
||||
namespace Zoomyboy\Tex;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\View\InvokableComponentVariable;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use ReflectionProperty;
|
||||
|
||||
abstract class Document
|
||||
{
|
||||
|
@ -17,7 +22,7 @@ abstract class Document
|
|||
public function renderBody(): string
|
||||
{
|
||||
return view()
|
||||
->make($this->view(), get_object_vars($this))
|
||||
->make($this->view(), $this->data())
|
||||
->render();
|
||||
}
|
||||
|
||||
|
@ -35,6 +40,34 @@ abstract class Document
|
|||
return str_contains($this->renderBody(), $content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $content
|
||||
*/
|
||||
public function hasAllContent(array $content): bool
|
||||
{
|
||||
foreach ($content as $check) {
|
||||
if (!$this->hasContent($check)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, string> $content
|
||||
*/
|
||||
public function missesAllContent(array $content): bool
|
||||
{
|
||||
foreach ($content as $check) {
|
||||
if ($this->hasContent($check)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function filename(): string
|
||||
{
|
||||
return $this->basename().'.tex';
|
||||
|
@ -44,4 +77,71 @@ abstract class Document
|
|||
{
|
||||
return $this->basename().'.pdf';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function data(): array
|
||||
{
|
||||
return array_merge($this->extractPublicProperties(), $this->extractPublicMethods());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
protected function extractPublicProperties(): array
|
||||
{
|
||||
$class = get_class($this);
|
||||
|
||||
$reflection = new ReflectionClass($this);
|
||||
|
||||
$properties = collect($reflection->getProperties(ReflectionProperty::IS_PUBLIC))
|
||||
->filter(fn (ReflectionProperty $property) => !$property->isStatic());
|
||||
|
||||
$values = [];
|
||||
|
||||
foreach ($properties as $property) {
|
||||
$values[$property->getName()] = $this->{$property->getName()};
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the public methods for the component.
|
||||
*
|
||||
* @return array<string, Closure|InvokableComponentVariable>
|
||||
*/
|
||||
protected function extractPublicMethods()
|
||||
{
|
||||
$class = get_class($this);
|
||||
|
||||
$reflection = new ReflectionClass($this);
|
||||
|
||||
/* @var Collection<string, string> */
|
||||
$methods = collect($reflection->getMethods(ReflectionMethod::IS_PUBLIC))
|
||||
->all();
|
||||
|
||||
$values = [];
|
||||
|
||||
foreach ($methods as $method) {
|
||||
$values[$method->getName()] = $this->createVariableFromMethod(new ReflectionMethod($this, $method->getName()));
|
||||
}
|
||||
|
||||
return $values;
|
||||
}
|
||||
|
||||
protected function createVariableFromMethod(ReflectionMethod $method): Closure|InvokableComponentVariable
|
||||
{
|
||||
return 0 === $method->getNumberOfParameters()
|
||||
? $this->createInvokableVariable($method->getName())
|
||||
: Closure::fromCallable([$this, $method->getName()]);
|
||||
}
|
||||
|
||||
protected function createInvokableVariable(string $method): InvokableComponentVariable
|
||||
{
|
||||
return new InvokableComponentVariable(function () use ($method) {
|
||||
return $this->{$method}();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue