Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add docblocks & minor cleanup
  • Loading branch information
claudiodekker committed Mar 4, 2021
commit 91bd93f92c26cc58c25849f1a1ac6d7ee6d55411
58 changes: 54 additions & 4 deletions src/Illuminate/Testing/Fluent/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,39 @@ class Assert implements Arrayable
Macroable,
Tappable;

/** @var array */
/**
* The properties in the current scope.
*
* @var array
*/
private $props;

/** @var string */
/**
* The "dot" path to the current scope.
*
* @var string|null
*/
private $path;

/**
* Create a new Assert instance.
*
* @param array $props
* @param string|null $path
*/
protected function __construct(array $props, string $path = null)
{
$this->path = $path;
$this->props = $props;
}

protected function dotPath($key): string
/**
* Compose the absolute "dot" path to the given key.
*
* @param string $key
* @return string
*/
protected function dotPath(string $key): string
{
if (is_null($this->path)) {
return $key;
Expand All @@ -40,12 +60,25 @@ protected function dotPath($key): string
return implode('.', [$this->path, $key]);
}

/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
protected function prop(string $key = null)
{
return Arr::get($this->props, $key);
}

protected function scope($key, Closure $callback): self
/**
* Instantiate a new "scope" at the path of the given key.
*
* @param string $key
* @param Closure $callback
* @return $this
*/
protected function scope(string $key, Closure $callback): self
{
$props = $this->prop($key);
$path = $this->dotPath($key);
Expand All @@ -59,16 +92,33 @@ protected function scope($key, Closure $callback): self
return $this;
}

/**
* Create a new instance from an array.
*
* @param array $data
* @return static
*/
public static function fromArray(array $data): self
{
return new self($data);
}

/**
* Create a new instance from a AssertableJsonString.
*
* @param AssertableJsonString $json
* @return static
*/
public static function fromAssertableJsonString(AssertableJsonString $json): self
{
return self::fromArray($json->json());
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return $this->props;
Expand Down
18 changes: 18 additions & 0 deletions src/Illuminate/Testing/Fluent/Concerns/Debugging.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@

trait Debugging
{
/**
* Dumps the given props.
*
* @param string|null $prop
* @return $this
*/
public function dump(string $prop = null): self
{
dump($this->prop($prop));

return $this;
}

/**
* Dumps the given props and exits.
*
* @param string|null $prop
* @return void
*/
public function dd(string $prop = null): void
{
dd($this->prop($prop));
}

/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
abstract protected function prop(string $key = null);
}
100 changes: 79 additions & 21 deletions src/Illuminate/Testing/Fluent/Concerns/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

trait Has
{
protected function count(string $key, $length): self
/**
* Assert that the prop is of the expected size.
*
* @param string $key
* @param int $length
* @return $this
*/
protected function count(string $key, int $length): self
{
PHPUnit::assertCount(
$length,
Expand All @@ -19,21 +26,14 @@ protected function count(string $key, $length): self
return $this;
}

public function hasAll($key): self
{
$keys = is_array($key) ? $key : func_get_args();

foreach ($keys as $prop => $count) {
if (is_int($prop)) {
$this->has($count);
} else {
$this->has($prop, $count);
}
}

return $this;
}

/**
* Ensure that the given prop exists.
*
* @param string $key
* @param null $value
* @param Closure|null $scope
* @return $this
*/
public function has(string $key, $value = null, Closure $scope = null): self
{
$prop = $this->prop();
Expand Down Expand Up @@ -69,6 +69,33 @@ public function has(string $key, $value = null, Closure $scope = null): self
return $this;
}

/**
* Assert that all of the given props exist.
*
* @param array|string $key
* @return $this
*/
public function hasAll($key): self
{
$keys = is_array($key) ? $key : func_get_args();

foreach ($keys as $prop => $count) {
if (is_int($prop)) {
$this->has($count);
} else {
$this->has($prop, $count);
}
}

return $this;
}

/**
* Assert that none of the given props exist.
*
* @param array|string $key
* @return $this
*/
public function missingAll($key): self
{
$keys = is_array($key) ? $key : func_get_args();
Expand All @@ -80,6 +107,12 @@ public function missingAll($key): self
return $this;
}

/**
* Assert that the given prop does not exist.
*
* @param string $key
* @return $this
*/
public function missing(string $key): self
{
PHPUnit::assertNotTrue(
Expand All @@ -90,11 +123,36 @@ public function missing(string $key): self
return $this;
}

abstract protected function prop(string $key = null);

abstract protected function dotPath($key): string;

/**
* Compose the absolute "dot" path to the given key.
*
* @param string $key
* @return string
*/
abstract protected function dotPath(string $key): string;

/**
* Marks the property as interacted.
*
* @param string $key
* @return void
*/
abstract protected function interactsWith(string $key): void;

abstract protected function scope($key, Closure $callback);
/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
abstract protected function prop(string $key = null);

/**
* Instantiate a new "scope" at the path of the given key.
*
* @param string $key
* @param Closure $callback
* @return $this
*/
abstract protected function scope(string $key, Closure $callback);
}
28 changes: 27 additions & 1 deletion src/Illuminate/Testing/Fluent/Concerns/Interaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,19 @@

trait Interaction
{
/** @var array */
/**
* The list of interacted properties.
*
* @var array
*/
protected $interacted = [];

/**
* Marks the property as interacted.
*
* @param string $key
* @return void
*/
protected function interactsWith(string $key): void
{
$prop = Str::before($key, '.');
Expand All @@ -19,6 +29,11 @@ protected function interactsWith(string $key): void
}
}

/**
* Asserts that all properties have been interacted with.
*
* @return void
*/
public function interacted(): void
{
PHPUnit::assertSame(
Expand All @@ -30,12 +45,23 @@ public function interacted(): void
);
}

/**
* Disables the interaction check.
*
* @return $this
*/
public function etc(): self
{
$this->interacted = array_keys($this->prop());

return $this;
}

/**
* Retrieve a prop within the current scope using "dot" notation.
*
* @param string|null $key
* @return mixed
*/
abstract protected function prop(string $key = null);
}
Loading