Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Introduce helpers for static constructors #158

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@
"autoload": {
"psr-4": {
"loophp\\collection\\": "./src/"
}
},
"files": [
"src/Utils/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
15 changes: 15 additions & 0 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use PhpSpec\ObjectBehavior;
use stdClass;
use function gettype;
use function loophp\collection\Utils\collectIterable;
use const INF;
use const PHP_EOL;
use const PHP_VERSION_ID;
Expand All @@ -37,6 +38,13 @@ class CollectionSpec extends ObjectBehavior
{
private const PHP_8 = 80_000;

public function getMatchers(): array
{
return [
'beSameAs' => static fn (CollectionInterface $subject, CollectionInterface $expected): bool => $subject->same($expected),
];
}
Comment on lines +41 to +46
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding a custom matcher based on: https://www.phpspec.net/en/stable/cookbook/matchers.html#inline-matcher

Couldn't find a better way to test the function, let me know if you can think of any

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good way indeed! And I don't see any other option at the moment.


public function it_can_all(): void
{
$this::fromIterable([1, 2, 3])
Expand Down Expand Up @@ -438,6 +446,13 @@ public function it_can_be_constructed_with_an_arrayObject(): void
$this->shouldImplement(Collection::class);
}

public function it_can_be_constructed_with_collectIterable(): void
{
$input = [1, 2, 3];

$this::fromIterable($input)->shouldBeSameAs(collectIterable($input));
}

public function it_can_be_instantiated_with_withClosure(): void
{
$fibonacci = static function ($start, $inc) {
Expand Down
32 changes: 32 additions & 0 deletions src/Utils/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/

declare(strict_types=1);

namespace loophp\collection\Utils;

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;

use function function_exists;

if (!function_exists('collectIterable')) {
/**
* @pure
*
* @template TKey
* @template T
*
* @param iterable<TKey, T> $iterable
*
* @return CollectionInterface<TKey, T>
*/
function collectIterable(iterable $iterable): CollectionInterface
{
return Collection::fromIterable($iterable);
}
}