Skip to content

Commit

Permalink
Updates test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sikhlana committed Sep 15, 2024
1 parent 157916d commit f724e1c
Showing 1 changed file with 46 additions and 9 deletions.
55 changes: 46 additions & 9 deletions tests/SingletonResolveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ protected function getPackageProviders($app)
];
}

public function testResolvingShouldIncrementCount()
protected function setUp(): void
{
IncrementsCount::$count = 0;
parent::setUp();

IncrementsCount::reset();
}

public function testResolvingShouldIncrementCount()
{
for ($i = 1; $i <= 10; $i++) {
$this->app->make(IncrementsCount::class);
$this->assertEquals($i, IncrementsCount::$count);
Expand All @@ -27,32 +32,64 @@ public function testResolvingShouldIncrementCount()

public function testResolvingShouldNotIncrementCount()
{
IncrementsCountOnlyOnce::$count = 0;

for ($i = 0; $i < 10; $i++) {
$this->app->make(IncrementsCountOnlyOnce::class);
$this->assertEquals(1, IncrementsCountOnlyOnce::$count);
$this->app->make(ParentSingleton::class);
$this->assertEquals(1, ParentSingleton::$count);
}
}

public function testResolvingShouldNotIncrementCountEvenIfBounded()
{
$this->app->bind(IncrementsCountOnlyOnce::class);
$this->app->bind(ParentSingleton::class);
$this->assertEquals(0, ParentSingleton::$count);
$this->testResolvingShouldNotIncrementCount();
}

public function testChildClassesShouldNotInterfereWithParent()
{
for ($i = 0; $i < 10; $i++) {
$this->app->make(ChildSingleton::class);
$this->assertEquals(1, ParentSingleton::$count);
}

for ($i = 0; $i < 10; $i++) {
$this->app->make(ParentSingleton::class);
$this->assertEquals(2, ParentSingleton::$count);
}

for ($i = 0; $i < 10; $i++) {
$this->app->make(AnotherChildSingleton::class);
$this->assertEquals(3, ParentSingleton::$count);
}
}
}

class IncrementsCount
{
public static $count = 0;
public static int $count = 0;

public function __construct()
{
static::$count++;
}

public static function reset(): void
{
static::$count = 0;
}
}

class ParentSingleton extends IncrementsCount implements Singleton
{
//
}

class ChildSingleton extends ParentSingleton
{
//
}

class IncrementsCountOnlyOnce extends IncrementsCount implements Singleton
class AnotherChildSingleton extends ParentSingleton
{
//
}

0 comments on commit f724e1c

Please sign in to comment.