Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
39a3130
feat(tests): add InteractWithDatabase trait and corresponding unit tests
barbosa89 Oct 8, 2025
af3fa96
refactor(InteractWithDatabase): simplify record count retrieval and u…
barbosa89 Oct 8, 2025
b11471d
refactor(InteractWithDatabase): update method signatures to accept Cl…
barbosa89 Oct 8, 2025
6e6a412
feat(tests): add RefreshDatabase trait and corresponding unit tests
barbosa89 Oct 8, 2025
f372ef4
fix(config): update application port from 1337 to 1338
barbosa89 Oct 8, 2025
3b8ec17
style: php cs
barbosa89 Oct 8, 2025
2b9e2d3
fix(TestCase): ensure refreshDatabase method exists before calling
barbosa89 Oct 8, 2025
4c8a791
refactor(RefreshDatabase): streamline truncateDatabase and improve dr…
barbosa89 Oct 8, 2025
728e93d
feat(tests): add PostgresqlConnectionPool mock and enhance RefreshDat…
barbosa89 Oct 9, 2025
25fdbe7
feat(tests): add tests for null value and boolean criteria normalizat…
barbosa89 Oct 9, 2025
44d3ce0
feat(EventEmitter): add logging and faking capabilities for dispatche…
barbosa89 Oct 9, 2025
27b2a99
feat(EventEmitterTest): add support for closure predicates in event a…
barbosa89 Oct 9, 2025
c44e4dd
feat(Mail): enhance expect method to accept mailable and improve test…
barbosa89 Oct 9, 2025
bca1e2d
feat(TestEvents): replace expect assertions with PHPUnit Assert metho…
barbosa89 Oct 9, 2025
423c561
feat(TestResponse): replace expect assertions with PHPUnit Assert met…
barbosa89 Oct 9, 2025
1c3c106
chore: rename TestEvents to TestEvent
barbosa89 Oct 9, 2025
9392288
feat(Queue): add logging and faking capabilities for task pushes; int…
barbosa89 Oct 9, 2025
f3254e5
feat(EventEmitter, Queue): enhance fake methods to support specific e…
barbosa89 Oct 10, 2025
627a213
feat(EventEmitter, EventFacade): enhance fake method to support event…
barbosa89 Oct 11, 2025
b316753
feat(EventEmitter): add support for conditional closure based faking …
barbosa89 Oct 15, 2025
57110af
feat(EventEmitter, EventFacade): update fake method to accept Closure…
barbosa89 Oct 15, 2025
c4ea6d6
feat(EventEmitter): refactor normalizeFakeEvents method to improve ha…
barbosa89 Oct 15, 2025
8b532bf
feat(EventEmitter, EventFacade): add resetEventLog method to clear ev…
barbosa89 Oct 15, 2025
5409f96
refactor(EventEmitter): remove redundant comment about one-time liste…
barbosa89 Oct 15, 2025
03f383d
feat(Queue, QueueManager): enhance fake method to support times param…
barbosa89 Oct 15, 2025
22d167a
fix(EventEmitter): correct condition check for remaining fake events …
barbosa89 Oct 15, 2025
a429f25
style: php cs
barbosa89 Oct 15, 2025
29d962f
feat(EventEmitter): split class to CaptureEvents trait
barbosa89 Oct 16, 2025
40a6baa
refactor(CaptureEvents): simplify normalizeFakeEvents method for bett…
barbosa89 Oct 16, 2025
f528b8b
refactor(CaptureEvents): streamline shouldFakeEvent method for improv…
barbosa89 Oct 16, 2025
64dbe00
style: php cs
barbosa89 Oct 16, 2025
f9da064
feat(QueueManager): split class to CaptureTasks trait for task loggin…
barbosa89 Oct 16, 2025
fcb39e7
refactor(CaptureEvents): enhance normalizeFakeEvents method for impro…
barbosa89 Oct 17, 2025
2adbbda
refactor(CaptureEvents): improve normalizeSingleEvent method for bett…
barbosa89 Oct 17, 2025
4873877
style: php cs
barbosa89 Oct 17, 2025
7257741
test(EventEmitter): add tests for event logging and faking in product…
barbosa89 Oct 17, 2025
ceb0816
test(EventEmitter): add test for correct behavior of async event faking
barbosa89 Oct 17, 2025
c270301
refactor(CaptureTasks): remove production check from resetQueueLog me…
barbosa89 Oct 17, 2025
b180e88
test(ParallelQueue): add tests for logging and faking behavior in pro…
barbosa89 Oct 17, 2025
9b835d6
test(ParallelQueue): enhance assertion for tasks not pushed to includ…
barbosa89 Oct 17, 2025
317ed8b
refactor(EventEmitter): remove unused logging and event management me…
barbosa89 Oct 21, 2025
47a7c20
fix(EventEmitter): ensure event dispatching records are created for f…
barbosa89 Oct 21, 2025
7fad2ba
refactor(CaptureEvents): streamline faking logic and enhance event lo…
barbosa89 Oct 21, 2025
0ef464b
refactor(Queue): enhance faking methods and improve logging structure
barbosa89 Oct 22, 2025
8c5c0c1
refactor(Mail): rename log method to fake for clarity and consistency
barbosa89 Oct 22, 2025
1f39dd1
refactor(TestEvent, TestQueue): simplify filtering logic using arrow …
barbosa89 Oct 22, 2025
135b1c5
refactor(Collection): enhance collection methods with filter, map, wh…
barbosa89 Oct 22, 2025
e290d7b
refactor(DatabaseQueryBuilder, QueryBuilder): update collection type …
barbosa89 Oct 22, 2025
5fb592c
refactor(CaptureEvents, CaptureTasks): update timestamp type annotati…
barbosa89 Oct 22, 2025
89a5596
refactor(TestQueue): update timestamp type annotations to use float i…
barbosa89 Oct 22, 2025
9e886fe
refactor(Collection): simplify fromArray method and enhance getDataTy…
barbosa89 Oct 22, 2025
8a29e9a
tests: add comprehensive tests for filter, map, where, sort, diff, in…
barbosa89 Oct 22, 2025
4b85057
tests: add logging verification for pushed tasks in the parallel queue
barbosa89 Oct 22, 2025
064547f
tests: add fake queue expectations for BasicQueuableTask and BadTask
barbosa89 Oct 22, 2025
da7fcc2
tests: enhance task faking methods in ParallelQueueTest for improved …
barbosa89 Oct 23, 2025
3015ffa
tests: update logging expectation for pushed tasks in ParallelQueueTest
barbosa89 Oct 23, 2025
a21ef2d
feat: add new except mode
barbosa89 Oct 23, 2025
82cfcff
style: php cs
barbosa89 Oct 23, 2025
8ba2b6e
feat: add fake except mode for events
barbosa89 Oct 23, 2025
30b4212
refactor: improve code to solve sonar issues
barbosa89 Oct 23, 2025
5c3cfc5
feat: add tests for EventFacade fake methods and their behavior
barbosa89 Oct 23, 2025
352f348
feat: add negative assertions for event dispatching and email sending
barbosa89 Oct 23, 2025
192ed50
fix: update TestMail class for improved mailable handling
barbosa89 Oct 23, 2025
f9ff453
fix: remove unnecessary event emission in closure predicate test
barbosa89 Oct 23, 2025
02f9035
test: update DelayableTask duration in processing skip test
barbosa89 Oct 23, 2025
e1caad8
test: add fake queue behavior for BasicQueuableTask in local environment
barbosa89 Oct 23, 2025
fd4facd
test: add tests for parallel queue processing and task management
barbosa89 Oct 23, 2025
2abdf51
fix: reset queue faking in tearDown method [skip ci]
barbosa89 Oct 24, 2025
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
230 changes: 223 additions & 7 deletions src/Data/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,35 @@

namespace Phenix\Data;

use Closure;
use Phenix\Contracts\Arrayable;
use Ramsey\Collection\Collection as GenericCollection;
use SplFixedArray;
use Ramsey\Collection\CollectionInterface;
use Ramsey\Collection\Exception\CollectionMismatchException;
use Ramsey\Collection\Sort;

use function array_filter;
use function array_key_first;
use function array_map;
use function array_merge;
use function array_udiff;
use function array_uintersect;
use function is_int;
use function is_object;
use function spl_object_id;
use function sprintf;
use function usort;

/**
* @template T
* @extends GenericCollection<T>
*/
class Collection extends GenericCollection implements Arrayable
{
public static function fromArray(array $data): self
{
$data = SplFixedArray::fromArray($data);
$collection = new self('array');

foreach ($data as $value) {
$collection->add($value);
}
$collection = new self(self::getDataType($data));
$collection->data = $data;

return $collection;
}
Expand All @@ -34,4 +47,207 @@ public function first(): mixed

return $this->data[$firstIndex];
}

/**
* @param callable(T): bool $callback
*
* @return self<T>
*/
public function filter(callable $callback): self
{
$collection = clone $this;
$collection->data = array_merge([], array_filter($collection->data, $callback));

return $collection;
}

/**
* @param callable(T): TCallbackReturn $callback
*
* @return self<TCallbackReturn>
*
* @template TCallbackReturn
*/
public function map(callable $callback): self
{
return new self('mixed', array_map($callback, $this->data));
}

/**
* @param string|null $propertyOrMethod
* @param mixed $value
*
* @return self<T>
*/
public function where(string|null $propertyOrMethod, mixed $value): self
{
return $this->filter(
fn (mixed $item): bool => $this->extractValue($item, $propertyOrMethod) === $value,
);
}

/**
* @param string|null $propertyOrMethod
* @param Sort $order
*
* @return self<T>
*/
public function sort(string|null $propertyOrMethod = null, Sort $order = Sort::Ascending): self
{
$collection = clone $this;

usort(
$collection->data,
function (mixed $a, mixed $b) use ($propertyOrMethod, $order): int {
$aValue = $this->extractValue($a, $propertyOrMethod);
$bValue = $this->extractValue($b, $propertyOrMethod);

return ($aValue <=> $bValue) * ($order === Sort::Descending ? -1 : 1);
},
);

return $collection;
}

/**
* @param CollectionInterface<T> $other
*
* @return self<T>
*/
public function diff(CollectionInterface $other): self
{
$this->compareCollectionTypes($other);

$diffAtoB = array_udiff($this->data, $other->toArray(), $this->getComparator());
$diffBtoA = array_udiff($other->toArray(), $this->data, $this->getComparator());

$collection = clone $this;
$collection->data = array_merge($diffAtoB, $diffBtoA);

return $collection;
}

/**
* @param CollectionInterface<T> $other
*
* @return self<T>
*/
public function intersect(CollectionInterface $other): self
{
$this->compareCollectionTypes($other);

$collection = clone $this;
$collection->data = array_uintersect($this->data, $other->toArray(), $this->getComparator());

return $collection;
}

/**
* @param CollectionInterface<T> ...$collections
*
* @return self<T>
*/
public function merge(CollectionInterface ...$collections): self
{
$mergedCollection = clone $this;

foreach ($collections as $index => $collection) {
if (! $collection instanceof static) {
throw new CollectionMismatchException(
sprintf('Collection with index %d must be of type %s', $index, static::class),
);
}

if ($this->getUniformType($collection) !== $this->getUniformType($this)) {
throw new CollectionMismatchException(
sprintf(
'Collection items in collection with index %d must be of type %s',
$index,
$this->getType(),
),
);
}

foreach ($collection as $key => $value) {
if (is_int($key)) {
$mergedCollection[] = $value;
} else {
$mergedCollection[$key] = $value;
}
}
}

return $mergedCollection;
}

/**
* @param CollectionInterface<T> $other
*
* @throws CollectionMismatchException
*/
private function compareCollectionTypes(CollectionInterface $other): void
{
if (! $other instanceof static) {
throw new CollectionMismatchException('Collection must be of type ' . static::class);
}

if ($this->getUniformType($other) !== $this->getUniformType($this)) {
throw new CollectionMismatchException('Collection items must be of type ' . $this->getType());
}
}

private function getComparator(): Closure
{
return function (mixed $a, mixed $b): int {
if (is_object($a) && is_object($b)) {
$a = spl_object_id($a);
$b = spl_object_id($b);
}

if ($a === $b) {
return 0;
}

if ($a < $b) {
return 1;
}

return -1;
};
}

/**
* @param CollectionInterface<mixed> $collection
*/
private function getUniformType(CollectionInterface $collection): string
{
return match ($collection->getType()) {
'integer' => 'int',
'boolean' => 'bool',
'double' => 'float',
default => $collection->getType(),
};
}

/**
* @param array<mixed> $data
*
* @return string
*/
private static function getDataType(array $data): string
{
if (empty($data)) {
return 'mixed';
}

$firstType = gettype(reset($data));

foreach ($data as $item) {
if (gettype($item) !== $firstType) {
return 'mixed';
}
}

return $firstType;
}
}
14 changes: 7 additions & 7 deletions src/Database/Models/QueryBuilders/DatabaseQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function with(array|string $relationships): self
}

/**
* @return Collection<int, DatabaseModel>
* @return Collection<DatabaseModel>
*/
public function get(): Collection
{
Expand Down Expand Up @@ -215,7 +215,7 @@ protected function resolveRelationships(Collection $collection): void
}

/**
* @param Collection<int, DatabaseModel> $models
* @param Collection<DatabaseModel> $models
* @param BelongsTo $relationship
* @param Closure $closure
*/
Expand All @@ -226,7 +226,7 @@ protected function resolveBelongsToRelationship(
): void {
$closure($relationship);

/** @var Collection<int, DatabaseModel> $records */
/** @var Collection<DatabaseModel> $records */
$records = $relationship->query()
->whereIn($relationship->getForeignKey()->getColumnName(), $models->modelKeys())
->get();
Expand All @@ -243,7 +243,7 @@ protected function resolveBelongsToRelationship(
}

/**
* @param Collection<int, DatabaseModel> $models
* @param Collection<DatabaseModel> $models
* @param HasMany $relationship
* @param Closure $closure
*/
Expand All @@ -254,7 +254,7 @@ protected function resolveHasManyRelationship(
): void {
$closure($relationship);

/** @var Collection<int, DatabaseModel> $children */
/** @var Collection<DatabaseModel> $children */
$children = $relationship->query()
->whereIn($relationship->getProperty()->getAttribute()->foreignKey, $models->modelKeys())
->get();
Expand Down Expand Up @@ -284,7 +284,7 @@ protected function resolveHasManyRelationship(
}

/**
* @param Collection<int, DatabaseModel> $models
* @param Collection<DatabaseModel> $models
* @param BelongsToMany $relationship
* @param Closure $closure
*/
Expand All @@ -297,7 +297,7 @@ protected function resolveBelongsToManyRelationship(

$attr = $relationship->getProperty()->getAttribute();

/** @var Collection<int, DatabaseModel> $related */
/** @var Collection<DatabaseModel> $related */
$related = $relationship->query()
->addSelect($relationship->getColumns())
->innerJoin($attr->table, function (Join $join) use ($attr): void {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function connection(SqlCommonConnectionPool|string $connection): self
}

/**
* @return Collection<int, array>
* @return Collection<array<string, mixed>>
*/
public function get(): Collection
{
Expand Down
Loading