Skip to content

Commit

Permalink
Merge pull request #89 from heimrichhannot/test/static-class-util
Browse files Browse the repository at this point in the history
Add Unit Tests for StaticClassUtil
  • Loading branch information
ericges authored Nov 27, 2024
2 parents 58a52e7 + b2e04eb commit 2d64083
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/StaticUtil/StaticClassUtilTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace StaticUtil;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\UtilsBundle\StaticUtil\StaticClassUtil;
use HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets\ExampleClass;
use HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets\ExampleClassUsingTrait;
use HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets\ExampleTrait;

class StaticClassUtilTest extends ContaoTestCase
{
public function testHasTrait()
{
$this->assertFalse(StaticClassUtil::hasTrait(ExampleClass::class, ExampleTrait::class));
$this->assertTrue(StaticClassUtil::hasTrait(ExampleClassUsingTrait::class, ExampleTrait::class));

$entityWithoutTrait = new ExampleClass();
$this->assertEquals('no-trait', $entityWithoutTrait->exampleClassMethod('no-trait'));
$this->assertFalse(StaticClassUtil::hasTrait($entityWithoutTrait, ExampleTrait::class));

$entityWithTrait = new ExampleClassUsingTrait();
$this->assertEquals('using-trait', $entityWithTrait->exampleClassUsingTraitMethod('using-trait'));
$this->assertEquals('from-trait', $entityWithTrait->exampleTraitMethod('from-trait'));
$this->assertTrue(StaticClassUtil::hasTrait($entityWithTrait, ExampleTrait::class));

$classWithoutTrait = new class {
public function example(): string
{
return 'test-without-trait';
}
};
$this->assertEquals('test-without-trait', $classWithoutTrait->example());
$this->assertFalse(StaticClassUtil::hasTrait($classWithoutTrait, ExampleTrait::class));

$classUsingTrait = new class {
use ExampleTrait;

public function example(): string
{
return 'test-with-trait';
}
};
$this->assertEquals('test-with-trait', $classUsingTrait->example());
$this->assertEquals('test-from-trait', $classUsingTrait->exampleTraitMethod('test-from-trait'));
$this->assertTrue(StaticClassUtil::hasTrait($classUsingTrait, ExampleTrait::class));
}
}
11 changes: 11 additions & 0 deletions tests/StaticUtil/StaticClassUtilTestAssets/ExampleClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets;

class ExampleClass
{
public function exampleClassMethod(mixed $return = 'example'): mixed
{
return $return;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets;

class ExampleClassUsingTrait
{
use ExampleTrait;

public function exampleClassUsingTraitMethod(mixed $return = 'example'): mixed
{
return $return;
}
}
11 changes: 11 additions & 0 deletions tests/StaticUtil/StaticClassUtilTestAssets/ExampleTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace HeimrichHannot\UtilsBundle\Tests\StaticUtil\StaticClassUtilTestAssets;

trait ExampleTrait
{
public function exampleTraitMethod(mixed $return = 'example'): mixed
{
return $return;
}
}

0 comments on commit 2d64083

Please sign in to comment.