|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2023 Jeremy Presutti <[email protected]> |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +declare(strict_types=1); |
| 20 | + |
| 21 | +use PHPUnit\Framework\TestCase; |
| 22 | + |
| 23 | +class DeferredTest extends TestCase |
| 24 | +{ |
| 25 | + public function testDeferredMethod(): void |
| 26 | + { |
| 27 | + $this->deferredTester(); |
| 28 | + $output = $this->getActualOutputForAssertion(); |
| 29 | + $this->assertEquals('This is firstThis is second', $output); |
| 30 | + } |
| 31 | + |
| 32 | + public function testCancelledDeferredMethod(): void |
| 33 | + { |
| 34 | + $this->deferredTester(true); |
| 35 | + $output = $this->getActualOutputForAssertion(); |
| 36 | + $this->assertEquals('This is first', $output); |
| 37 | + } |
| 38 | + |
| 39 | + public function testDeferredCallable(): void |
| 40 | + { |
| 41 | + $this->deferredCallableTester(); |
| 42 | + $output = $this->getActualOutputForAssertion(); |
| 43 | + $this->assertEquals('This is firstThis is second', $output); |
| 44 | + } |
| 45 | + |
| 46 | + public function testDeferredCallableEnsureOrder(): void |
| 47 | + { |
| 48 | + $this->deferredCallableTesterEnsureProcessingOrder(); |
| 49 | + $output = $this->getActualOutputForAssertion(); |
| 50 | + $this->assertEquals('This is firstThis is second', $output); |
| 51 | + } |
| 52 | + |
| 53 | + public function testCancelledDeferredCallable(): void |
| 54 | + { |
| 55 | + $this->deferredCallableTester(true); |
| 56 | + $output = $this->getActualOutputForAssertion(); |
| 57 | + $this->assertEquals('This is first', $output); |
| 58 | + } |
| 59 | + |
| 60 | + protected function deferredTester(bool $cancel = false): void |
| 61 | + { |
| 62 | + $deferred = new \Mocks\DeferredMock('This is second'); |
| 63 | + echo 'This is first'; |
| 64 | + if ( $cancel ) { |
| 65 | + $deferred->cancelDeferral(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + protected function deferredCallableTester(bool $cancel = false): void |
| 70 | + { |
| 71 | + $deferred = new \Feast\DeferredCall(function() { echo 'This is second'; }); |
| 72 | + echo 'This is first'; |
| 73 | + if ( $cancel ) { |
| 74 | + $deferred->cancelDeferral(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + protected function deferredCallableTesterEnsureProcessingOrder(bool $cancel = false): void |
| 79 | + { |
| 80 | + $output = 'This is second'; |
| 81 | + $deferred = new \Feast\DeferredCall(function() use ($output) { echo $output; }); |
| 82 | + $output = 'This is not second'; |
| 83 | + echo 'This is first'; |
| 84 | + if ( $cancel ) { |
| 85 | + $deferred->cancelDeferral(); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | +} |
0 commit comments