Skip to content

Commit

Permalink
inshance test to integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
abdalqader committed Mar 4, 2025
1 parent 94f53dd commit 4dc14ec
Showing 1 changed file with 45 additions and 38 deletions.
83 changes: 45 additions & 38 deletions tests/Support/DeepCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Support;

use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use PHPUnit\Framework\TestCase;

class DeepCollectionTest extends TestCase
Expand All @@ -22,23 +24,21 @@ public function testRecursivelyConvertsNestedArraysToCollections()

$collection = deepCollect($array);

// Ensure the top-level is a Collection
// Ensure conversion
$this->assertInstanceOf(Collection::class, $collection);

// Ensure nested arrays are converted
$this->assertInstanceOf(Collection::class, $collection['nested']);
$this->assertInstanceOf(Collection::class, $collection['nested']['deepNested']);

// Ensure values remain the same
// Ensure data integrity
$this->assertEquals('value', $collection['nested']['key']);
$this->assertEquals('deepValue', $collection['nested']['deepNested']['deepKey']);
}

public function testReturnsNonArrayValuesUnchanged()
public function testDoesNotAlterNonArrayValues()
{
$this->assertEquals(42, deepCollect(42));
$this->assertEquals('Laravel', deepCollect('Laravel'));
$this->assertEquals(null, deepCollect(null));
$this->assertNull(deepCollect(null));
}

public function testConvertsEmptyArraysToEmptyCollections()
Expand All @@ -48,30 +48,7 @@ public function testConvertsEmptyArraysToEmptyCollections()
$this->assertTrue($collection->isEmpty());
}

public function testTransformsMixedNestedStructuresIntoCollections()
{
$array = [
'number' => 100,
'boolean' => false,
'nested' => [
'list' => [10, 20, 30],
'assoc' => ['key' => 'value'],
],
];

$collection = deepCollect($array);

$this->assertInstanceOf(Collection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection['nested']);
$this->assertInstanceOf(Collection::class, $collection['nested']['assoc']);

// Verify values remain unchanged
$this->assertEquals(100, $collection['number']);
$this->assertFalse($collection['boolean']);
$this->assertEquals([10, 20, 30], $collection['nested']['list']->toArray());
}

public function testHandlesDeeplyNestedArraysCorrectly()
public function testHandlesDeeplyNestedArrays()
{
$array = [
'level1' => [
Expand All @@ -87,14 +64,7 @@ public function testHandlesDeeplyNestedArraysCorrectly()

$collection = deepCollect($array);

// Assert each level is converted
$this->assertInstanceOf(Collection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection['level1']);
$this->assertInstanceOf(Collection::class, $collection['level1']['level2']);
$this->assertInstanceOf(Collection::class, $collection['level1']['level2']['level3']);
$this->assertInstanceOf(Collection::class, $collection['level1']['level2']['level3']['level4']);

// Assert value remains correct
$this->assertEquals('finalValue', $collection['level1']['level2']['level3']['level4']['key']);
}

Expand All @@ -118,9 +88,46 @@ public function testDoesNotReconvertExistingCollections()

$collection = deepCollect($original);

// Ensure the structure remains untouched
$this->assertSame($original, $collection);
$this->assertInstanceOf(Collection::class, $collection['nested']);
$this->assertEquals('value', $collection['nested']['key']);
}

public function testConvertsQueryBuilderArrayResultsToCollections()
{
// Simulating a Query Builder result
$queryResult = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
];

$collection = deepCollect($queryResult);

$this->assertInstanceOf(Collection::class, $collection);
$this->assertInstanceOf(Collection::class, $collection->first());
$this->assertEquals('Alice', $collection->first()['name']);
}

public function testHandlesEloquentRelationResults()
{
// Simulating an Eloquent relation returning an array
$model = new class extends Model {
public function comments(): HasMany
{
return $this->hasMany(Comment::class);
}
};

// Fake relationship array result (e.g., when calling $model->comments()->get()->toArray())
$commentsArray = [
['id' => 1, 'text' => 'First comment'],
['id' => 2, 'text' => 'Second comment'],
];

$commentsCollection = deepCollect($commentsArray);

$this->assertInstanceOf(Collection::class, $commentsCollection);
$this->assertInstanceOf(Collection::class, $commentsCollection->first());
$this->assertEquals('First comment', $commentsCollection->first()['text']);
}
}

0 comments on commit 4dc14ec

Please sign in to comment.