-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Improve test coverage * Apply fixes from StyleCI * Apply Rector changes (CI) --------- Co-authored-by: StyleCI Bot <[email protected]> Co-authored-by: viktorprogger <[email protected]>
- Loading branch information
1 parent
4561dde
commit a1b7171
Showing
8 changed files
with
293 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Tests\App; | ||
|
||
use Yiisoft\Queue\Message\EnvelopeInterface; | ||
use Yiisoft\Queue\Message\EnvelopeTrait; | ||
use Yiisoft\Queue\Message\MessageInterface; | ||
|
||
final class DummyEnvelope implements EnvelopeInterface | ||
{ | ||
use EnvelopeTrait; | ||
|
||
public static function fromMessage(MessageInterface $message): self | ||
{ | ||
$instance = new self(); | ||
$instance->message = $message; | ||
|
||
return $instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Tests\App; | ||
|
||
class StaticMessageHandler | ||
{ | ||
public static bool $wasHandled = false; | ||
|
||
public static function handle(): void | ||
{ | ||
self::$wasHandled = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Tests\Unit\Command; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Queue\Cli\SoftLimitTrait; | ||
|
||
final class SoftLimitTraitTest extends TestCase | ||
{ | ||
public function testMemoryLimitNotReachedWhenLimitIsZero(): void | ||
{ | ||
$instance = new class () { | ||
use SoftLimitTrait { | ||
memoryLimitReached as public; | ||
} | ||
|
||
protected function getMemoryLimit(): int | ||
{ | ||
return 0; | ||
} | ||
}; | ||
|
||
$this->assertFalse($instance->memoryLimitReached()); | ||
} | ||
|
||
public function testMemoryLimitNotReachedWhenUsageIsLower(): void | ||
{ | ||
$currentMemoryUsage = memory_get_usage(true); | ||
$instance = new class ($currentMemoryUsage + 1024 * 1024) { // 1MB higher than current usage | ||
use SoftLimitTrait { | ||
memoryLimitReached as public; | ||
} | ||
|
||
public function __construct(private int $limit) | ||
{ | ||
} | ||
|
||
protected function getMemoryLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
}; | ||
|
||
$this->assertFalse($instance->memoryLimitReached()); | ||
} | ||
|
||
public function testMemoryLimitReachedWhenUsageIsHigher(): void | ||
{ | ||
$currentMemoryUsage = memory_get_usage(true); | ||
$instance = new class ($currentMemoryUsage - 1024) { // 1KB lower than current usage | ||
use SoftLimitTrait { | ||
memoryLimitReached as public; | ||
} | ||
|
||
public function __construct(private int $limit) | ||
{ | ||
} | ||
|
||
protected function getMemoryLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
}; | ||
|
||
$this->assertTrue($instance->memoryLimitReached()); | ||
} | ||
|
||
public function testMemoryLimitExceededWhenUsageIncreases(): void | ||
{ | ||
$currentMemoryUsage = memory_get_usage(true); | ||
$instance = new class ($currentMemoryUsage + 5 * 1024 * 1024) { // Set limit 5MB higher than current usage | ||
use SoftLimitTrait { | ||
memoryLimitReached as public; | ||
} | ||
|
||
public function __construct(private int $limit) | ||
{ | ||
} | ||
|
||
protected function getMemoryLimit(): int | ||
{ | ||
return $this->limit; | ||
} | ||
}; | ||
|
||
// Initially memory limit is not reached | ||
$this->assertFalse($instance->memoryLimitReached()); | ||
|
||
// Create a large string to increase memory usage | ||
$largeString = str_repeat('x', 5 * 1024 * 1024 + 1); // 5MB and 1 byte string | ||
|
||
// Now memory limit should be exceeded | ||
$this->assertTrue($instance->memoryLimitReached()); | ||
|
||
// Clean up to free memory | ||
unset($largeString); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\Queue\Tests\Unit\Message; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Yiisoft\Queue\Message\Message; | ||
use Yiisoft\Queue\Tests\App\DummyEnvelope; | ||
|
||
final class EnvelopeTraitTest extends TestCase | ||
{ | ||
private function createTestEnvelope(): DummyEnvelope | ||
{ | ||
return new DummyEnvelope(); | ||
} | ||
|
||
public function testFromData(): void | ||
{ | ||
$handlerName = 'test-handler'; | ||
$data = ['key' => 'value']; | ||
$metadata = ['meta' => 'data']; | ||
|
||
$envelope = DummyEnvelope::fromData($handlerName, $data, $metadata); | ||
|
||
$this->assertInstanceOf(DummyEnvelope::class, $envelope); | ||
$this->assertSame($handlerName, $envelope->getHandlerName()); | ||
$this->assertSame($data, $envelope->getData()); | ||
$this->assertArrayHasKey('meta', $envelope->getMetadata()); | ||
$this->assertSame('data', $envelope->getMetadata()['meta']); | ||
} | ||
|
||
public function testWithMessage(): void | ||
{ | ||
$originalMessage = new Message('original-handler', 'original-data'); | ||
$newMessage = new Message('new-handler', 'new-data'); | ||
|
||
$envelope = $this->createTestEnvelope(); | ||
$envelope = $envelope->withMessage($originalMessage); | ||
|
||
$this->assertSame($originalMessage, $envelope->getMessage()); | ||
|
||
$newEnvelope = $envelope->withMessage($newMessage); | ||
|
||
$this->assertNotSame($envelope, $newEnvelope); | ||
$this->assertSame($newMessage, $newEnvelope->getMessage()); | ||
$this->assertSame($originalMessage, $envelope->getMessage()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters