-
Notifications
You must be signed in to change notification settings - Fork 14
feat: implement queue assertion helpers #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 18 commits
44f823f
4aa6260
b5c32a3
4e42e4a
a6d3519
ea0f94d
d6073ec
151527e
e6f5df4
85ba290
eab1d55
2df9b17
5a5a790
5c010b9
7a1d4a1
b7e5c3c
a88892f
55bf96b
d8d60be
4a59481
019ae09
59fff99
088de9a
2d18ac1
47f8fb8
a4cc42b
fa52d8a
660fefe
d09cb4a
5aa6369
444ce8f
eb347e7
5ed9bbf
9477cd8
db9e1c4
ad57ad5
6c8111d
335db99
4dd36dc
40aeee9
8189338
41b4fce
4303612
1c9b862
cd5242e
3b53ae1
2e4d0d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,10 @@ | |
|
|
||
| namespace RonasIT\Support\Traits; | ||
|
|
||
| use Illuminate\Support\Arr; | ||
| use Illuminate\Support\Facades\Queue; | ||
| use ReflectionClass; | ||
|
|
||
| trait TestingTrait | ||
| { | ||
| use FixturesTrait; | ||
|
|
@@ -18,4 +22,51 @@ protected function assertExceptionThrew(string $expectedClassName, string $expec | |
|
|
||
| $this->expectExceptionMessageMatches("/{$expectedMessage}/"); | ||
| } | ||
|
|
||
| protected function assertQueueEqualsFixture(string $fixture, bool $exportMode = false): void | ||
| { | ||
| $actualData = []; | ||
|
|
||
| foreach (Queue::pushedJobs() as $namespace => $jobs) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When jobs of different classes are dispatched to the same queue, iterating Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When code under test calls Useful? React with 👍 / 👎. |
||
| $actualData[$namespace] = Arr::map($jobs, function ($job) { | ||
| $job = $this->getJobObject($job); | ||
|
|
||
| return $this->getObjectAttributes($job); | ||
|
AZabolotnikov marked this conversation as resolved.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a test queues a closure job such as Useful? React with 👍 / 👎. |
||
| }); | ||
| } | ||
|
|
||
| $this->assertEqualsFixture("queue_states/{$fixture}", $actualData, $exportMode); | ||
| } | ||
|
|
||
| protected function getJobObject(array $job) | ||
| { | ||
| if (is_object($job['job'])) { | ||
| return $job['job']; | ||
| } | ||
|
|
||
| $data = Arr::wrap($job['data']); | ||
| $className = $job['job']; | ||
|
|
||
| return new $className(...$data); | ||
|
AZabolotnikov marked this conversation as resolved.
|
||
| } | ||
|
|
||
| protected function assertQueueEmpty(): void | ||
|
AZabolotnikov marked this conversation as resolved.
Outdated
|
||
| { | ||
| $this->assertEquals([], Queue::pushedJobs(), 'Failed assert that faked queue is empty.'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When code under test uses Laravel's raw queue API ( Useful? React with 👍 / 👎. |
||
| } | ||
|
AZabolotnikov marked this conversation as resolved.
Outdated
|
||
|
|
||
| protected function getObjectAttributes(object $object): array | ||
|
AZabolotnikov marked this conversation as resolved.
Outdated
|
||
| { | ||
| $result = []; | ||
|
|
||
| $properties = (new ReflectionClass($object))->getProperties(); | ||
|
AZabolotnikov marked this conversation as resolved.
Outdated
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For jobs that extend a base class with private instance state, Useful? React with 👍 / 👎. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When tests rely on this helper to catch changes to queue retry behavior, this path only serializes reflected properties, so jobs that define queue options through methods or attributes (for example Useful? React with 👍 / 👎. |
||
|
|
||
| foreach ($properties as $property) { | ||
| $result[$property->getName()] = $property->isInitialized($object) | ||
| ? $property->getValue($object) | ||
| : null; | ||
| } | ||
|
|
||
| return json_decode(json_encode($result), true); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -2,12 +2,26 @@ | |||
|
|
||||
| namespace RonasIT\Support\Tests; | ||||
|
|
||||
| use Illuminate\Support\Facades\Queue; | ||||
| use Illuminate\Support\Str; | ||||
| use RonasIT\Support\Exceptions\ModelFactoryNotFound; | ||||
| use RonasIT\Support\Tests\Support\Mock\Jobs\AnotherTestJob; | ||||
| use RonasIT\Support\Tests\Support\Mock\Jobs\TestJob; | ||||
| use RonasIT\Support\Tests\Support\Traits\TestingTraitTestTrait; | ||||
| use RonasIT\Support\Traits\TestingTrait; | ||||
|
|
||||
| class TestingTraitTest extends TestCase | ||||
| { | ||||
| use TestingTrait; | ||||
| use TestingTrait, TestingTraitTestTrait; | ||||
|
|
||||
| public static string $laravelMajorVersion; | ||||
|
|
||||
| public function setUp(): void | ||||
| { | ||||
| parent::setUp(); | ||||
|
|
||||
| self::$laravelMajorVersion ??= Str::before($this->app->version(), '.'); | ||||
| } | ||||
|
|
||||
| public function testAssertExceptionThrew(): void | ||||
| { | ||||
|
|
@@ -26,4 +40,60 @@ public function testAssertExceptionThrewNotStrictly(): void | |||
|
|
||||
| throw new ModelFactoryNotFound('full error message'); | ||||
| } | ||||
|
|
||||
| public function testAssertQueueEqualsFixture(): void | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| TestJob::dispatch('some payload', ['another payload']); | ||||
|
|
||||
| $this->assertQueueEqualsVersionedFixture(self::$laravelMajorVersion, 'assert_queue_equals'); | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's use laravel-helpers/src/Traits/FixturesTrait.php Line 134 in 660fefe
|
||||
| } | ||||
|
|
||||
| public function testAssertQueueEqualsFixturePushAsClassName(): void | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| Queue::push(TestJob::class); | ||||
|
|
||||
| $this->assertQueueEqualsVersionedFixture(self::$laravelMajorVersion, 'assert_queue_equals_as_class_name'); | ||||
| } | ||||
|
|
||||
| public function testAssertQueueEqualsFixturePushAsStringWithParams(): void | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| Queue::push(TestJob::class, [ | ||||
| 'payload' => 'some payload', | ||||
| 'anotherPayload' => ['another payload'], | ||||
| ]); | ||||
|
|
||||
| $this->assertQueueEqualsVersionedFixture(self::$laravelMajorVersion, 'assert_queue_equals_as_class_name_with_params'); | ||||
| } | ||||
|
|
||||
| public function testAssertQueueEqualsFixturePushAsStringWithOneParam(): void | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| Queue::push(TestJob::class, 'some payload'); | ||||
|
|
||||
| $this->assertQueueEqualsVersionedFixture(self::$laravelMajorVersion, 'assert_queue_equals_as_class_name_with_one_param'); | ||||
| } | ||||
|
|
||||
| public function testAssertQueueEqualsFixtureDifferentJobs(): void | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| TestJob::dispatch('some payload', ['another payload']); | ||||
| AnotherTestJob::dispatch('some payload', ['another payload']); | ||||
|
|
||||
| $this->assertQueueEqualsVersionedFixture(self::$laravelMajorVersion, 'assert_queue_equals_different_jobs'); | ||||
| } | ||||
|
|
||||
| public function testAssertQueueEmpty() | ||||
| { | ||||
| Queue::fake(); | ||||
|
|
||||
| $this->assertQueueEmpty(); | ||||
| } | ||||
| } | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [ | ||
| "another payload" | ||
| ], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "", | ||
| "anotherPayload": [], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [ | ||
| "another payload" | ||
| ], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [ | ||
| "another payload" | ||
| ], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ], | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\AnotherTestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [ | ||
| "another payload" | ||
| ], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "messageGroup": null, | ||
| "deduplicator": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "", | ||
| "anotherPayload": [], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "messageGroup": null, | ||
| "deduplicator": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "messageGroup": null, | ||
| "deduplicator": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| { | ||
| "RonasIT\\Support\\Tests\\Support\\Mock\\Jobs\\TestJob": [ | ||
| { | ||
| "tries": 5, | ||
| "payload": "some payload", | ||
| "anotherPayload": [ | ||
| "another payload" | ||
| ], | ||
| "job": null, | ||
| "connection": null, | ||
| "queue": null, | ||
| "messageGroup": null, | ||
| "deduplicator": null, | ||
| "delay": null, | ||
| "afterCommit": null, | ||
| "middleware": [], | ||
| "chained": [], | ||
| "chainConnection": null, | ||
| "chainQueue": null, | ||
| "chainCatchCallbacks": null | ||
| } | ||
| ] | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.