Skip to content

Commit

Permalink
Use attributes instead of annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromegamez committed Mar 17, 2024
1 parent dad8bca commit e5ce896
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 60 deletions.
14 changes: 7 additions & 7 deletions tests/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@

use DateTime;
use DateTimeInterface;
use Gamez\Illuminate\Support\Tests\ArrayableItem;
use Gamez\Illuminate\Support\TypedCollection;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class HelpersTest extends TestCase
{
/** @test */
public function it_cannot_be_created_with_an_unsupported_type_of_item()
#[Test]
public function it_cannot_be_created_with_an_unsupported_type_of_item(): void
{
$this->expectException(InvalidArgumentException::class);
typedCollect([new DateTime(), 'string', new DateTime()], DateTimeInterface::class);
}

/** @test */
public function it_can_be_created_with_supported_types()
#[Test]
public function it_can_be_created_with_supported_types(): void
{
$typedCollection = typedCollect([new DateTime(), new DateTime()], DateTimeInterface::class);
$this->assertInstanceOf(TypedCollection::class, $typedCollection);
$this->addToAssertionCount(1);
}

/** @test */
public function it_can_accept_an_array_of_types()
#[Test]
public function it_can_accept_an_array_of_types(): void
{
$typedCollection = typedCollect([new DateTime(), new ArrayableItem()], [DateTimeInterface::class, ArrayableItem::class]);
$this->assertInstanceOf(TypedCollection::class, $typedCollection);
Expand Down
35 changes: 18 additions & 17 deletions tests/LazyTypedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace Gamez\Illuminate\Support\Tests;

use DateTime;
use DateTimeImmutable;
use Gamez\Illuminate\Support\LazyTypedCollection;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\LazyCollection;

Expand All @@ -23,22 +24,22 @@ protected function setUp(): void
$this->collection = new LazyDateTimeCollection();
}

/** @test */
public function it_cannot_be_created_with_an_unsupported_type_of_item()
#[Test]
public function it_cannot_be_created_with_an_unsupported_type_of_item(): void
{
$this->expectException(InvalidArgumentException::class);
new LazyDateTimeCollection([new DateTime(), 'string', new DateTime()]);
}

/** @test */
public function it_can_be_created_with_supported_types()
#[Test]
public function it_can_be_created_with_supported_types(): void
{
new LazyDateTimeCollection([new DateTime(), new DateTime(), new DateTime()]);
$this->addToAssertionCount(1);
}

/** @test */
public function it_can_be_untyped()
#[Test]
public function it_can_be_untyped(): void
{
$untyped = $this->collection->untype();

Expand All @@ -47,29 +48,29 @@ public function it_can_be_untyped()
$this->assertNotInstanceOf(LazyTypedCollection::class, $untyped);
}

/** @test */
public function it_can_be_converted_to_an_array()
#[Test]
public function it_can_be_converted_to_an_array(): void
{
$collection = new LazyDateTimeCollection([new DateTime(), new DateTime()]);

$this->assertCount(2, $collection->toArray());
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/2
*/
public function it_works_with_items_that_themselves_are_arrayable()
#[Test]
public function it_works_with_items_that_themselves_are_arrayable(): void
{
$collection = new LazyArrayableItemCollection([new ArrayableItem()]);
$this->assertCount(1, $collection->toArray());
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/4
*/
public function items_can_be_plucked()
#[Test]
public function items_can_be_plucked(): void
{
$collection = new LazyArrayableItemCollection([
new ArrayableItem(1, 'a'),
Expand All @@ -82,16 +83,16 @@ public function items_can_be_plucked()
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/11
*/
public function items_are_untyped_when_mapped()
#[Test]
public function items_are_untyped_when_mapped(): void
{
$source = new LazyDateTimeCollection([
new \DateTimeImmutable('2022-04-25'),
new DateTimeImmutable('2022-04-25'),
]);

$mapped = $source->map(fn($item) => $item->format('Y-m-d'));
$mapped = $source->map(static fn($item) => $item->format('Y-m-d'));

$this->assertEquals(['2022-04-25'], $mapped->toArray());
}
Expand Down
74 changes: 38 additions & 36 deletions tests/TypedCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Gamez\Illuminate\Support\Tests;

use DateTime;
use DateTimeImmutable;
use Gamez\Illuminate\Support\TypedCollection;
use Illuminate\Support\Collection;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

class TypedCollectionTest extends TestCase
Expand All @@ -18,78 +20,78 @@ protected function setUp(): void
$this->collection = new DateTimeCollection();
}

/** @test */
public function it_cannot_be_created_with_an_unsupported_type_of_item()
#[Test]
public function it_cannot_be_created_with_an_unsupported_type_of_item(): void
{
$this->expectException(InvalidArgumentException::class);
new DateTimeCollection([new DateTime(), 'string', new DateTime()]);
}

/** @test */
public function it_can_be_created_with_supported_types()
#[Test]
public function it_can_be_created_with_supported_types(): void
{
new DateTimeCollection([new DateTime(), new DateTime(), new DateTime()]);
$this->addToAssertionCount(1);
}

/** @test */
public function a_supported_value_can_be_added()
#[Test]
public function a_supported_value_can_be_added(): void
{
$this->collection->add(new DateTime());
$this->addToAssertionCount(1);
}

/** @test */
public function an_unsupported_value_can_not_be_added()
#[Test]
public function an_unsupported_value_can_not_be_added(): void
{
$this->expectException(InvalidArgumentException::class);
$this->collection->add('string');
}

/** @test */
public function a_supported_value_can_be_prepended()
#[Test]
public function a_supported_value_can_be_prepended(): void
{
$this->collection->prepend(new DateTime());
$this->addToAssertionCount(1);
}

/** @test */
public function an_unsupported_value_can_not_be_prepended()
#[Test]
public function an_unsupported_value_can_not_be_prepended(): void
{
$this->expectException(InvalidArgumentException::class);
$this->collection->prepend('string');
}

/** @test */
public function a_supported_value_can_be_pushed()
#[Test]
public function a_supported_value_can_be_pushed(): void
{
$this->collection->push(new DateTime());
$this->addToAssertionCount(1);
}

/** @test */
public function an_unsupported_value_can_not_be_pushed()
#[Test]
public function an_unsupported_value_can_not_be_pushed(): void
{
$this->expectException(InvalidArgumentException::class);
$this->collection->push('string');
}

/** @test */
public function a_supported_value_can_be_put()
#[Test]
public function a_supported_value_can_be_put(): void
{
$this->collection->put('key', new DateTime());
$this->addToAssertionCount(1);
}

/** @test */
public function an_unsupported_value_can_not_be_put()
#[Test]
public function an_unsupported_value_can_not_be_put(): void
{
$this->expectException(InvalidArgumentException::class);
$this->collection->put('key', 'string');
}

/** @test */
public function it_can_be_untyped()
#[Test]
public function it_can_be_untyped(): void
{
$untyped = $this->collection->untype();

Expand All @@ -98,8 +100,8 @@ public function it_can_be_untyped()
$this->assertNotInstanceOf(TypedCollection::class, $untyped);
}

/** @test */
public function it_can_be_converted_to_an_array()
#[Test]
public function it_can_be_converted_to_an_array(): void
{
$this->collection->push(new DateTime());
$this->collection->push(new DateTime());
Expand All @@ -108,21 +110,21 @@ public function it_can_be_converted_to_an_array()
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/2
*/
public function it_works_with_items_that_themselves_are_arrayable()
#[Test]
public function it_works_with_items_that_themselves_are_arrayable(): void
{
$collection = new ArrayableItemCollection();
$collection->push(new ArrayableItem());
$this->assertCount(1, $collection->toArray());
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/7
*/
public function it_accepts_items_to_be_pushed()
#[Test]
public function it_accepts_items_to_be_pushed(): void
{
$collection = new ArrayableItemCollection();

Expand All @@ -134,10 +136,10 @@ public function it_accepts_items_to_be_pushed()
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/4
*/
public function items_can_be_plucked()
#[Test]
public function items_can_be_plucked(): void
{
$collection = new ArrayableItemCollection([
new ArrayableItem(1, 'a'),
Expand All @@ -149,8 +151,8 @@ public function items_can_be_plucked()
$this->assertEquals([1, 2, 3], $collection->pluck('id')->toArray());
}

/** @test */
public function it_returns_keys()
#[Test]
public function it_returns_keys(): void
{
$collection = new ArrayableItemCollection();
$collection->put('a' ,new ArrayableItem());
Expand All @@ -160,16 +162,16 @@ public function it_returns_keys()
}

/**
* @test
* @see https://github.com/jeromegamez/typed-collection/issues/11
*/
public function items_are_untyped_when_mapped()
#[Test]
public function items_are_untyped_when_mapped(): void
{
$source = new DateTimeCollection([
new \DateTimeImmutable('2022-04-25'),
new DateTimeImmutable('2022-04-25'),
]);

$mapped = $source->map(fn($item) => $item->format('Y-m-d'));
$mapped = $source->map(static fn($item) => $item->format('Y-m-d'));

$this->assertEquals(['2022-04-25'], $mapped->toArray());
}
Expand Down

0 comments on commit e5ce896

Please sign in to comment.