Skip to content

Commit

Permalink
Implement ProfilerAwareTrait::class, `ProfilerAwareInterface::class…
Browse files Browse the repository at this point in the history
…`. (#54)

* Implement ProfilerAwareTrait::class, ProfilerAwareInterface::class. (#54)
  • Loading branch information
terabytesoftw committed Aug 23, 2022
1 parent 0c8c9f0 commit ccfe5bf
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -2,7 +2,7 @@

## 1.0.5 under development

- no changes in this release.
- Enh #54: Implement `ProfilerAwareTrait` for `ProfilerAwareInterface` (@terabytesoftw)

## 1.0.4 July 26, 2022

Expand Down
15 changes: 15 additions & 0 deletions src/ProfilerAwareInterface.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Profiler;

interface ProfilerAwareInterface
{
/**
* Sets the profiler instance.
*
* @param ProfilerInterface $profiler The profiler instance.
*/
public function setProfiler(ProfilerInterface $profiler): void;
}
15 changes: 15 additions & 0 deletions src/ProfilerAwareTrait.php
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Profiler;

trait ProfilerAwareTrait
{
protected ?ProfilerInterface $profiler = null;

public function setProfiler(ProfilerInterface $profiler): void
{
$this->profiler = $profiler;
}
}
36 changes: 36 additions & 0 deletions tests/ProfilerAwareTraitTest.php
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Profiler\Tests;

use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Yiisoft\Profiler\Profiler;
use Yiisoft\Profiler\ProfilerAwareInterface;
use Yiisoft\Profiler\ProfilerAwareTrait;
use Yiisoft\Profiler\Target\LogTarget;
use Yiisoft\Profiler\Tests\Logger\ArrayLogger;

final class ProfilerAwareTraitTest extends TestCase
{
public function testTrait(): void
{
$target = new LogTarget(new NullLogger());
$profiler = new Profiler(new ArrayLogger(), [$target]);
$class = new class () implements ProfilerAwareInterface {
use ProfilerAwareTrait;

public function getProfiler(): ?Profiler
{
return $this->profiler;
}
};

$this->assertNull($class->getProfiler());

$class->setProfiler($profiler);

$this->assertSame($profiler, $class->getProfiler());
}
}

0 comments on commit ccfe5bf

Please sign in to comment.