|
| 1 | +<?php |
| 2 | + |
| 3 | +use Bexio\BexioClient; |
| 4 | +use Bexio\Support\QueryBuilder; |
| 5 | + |
| 6 | +beforeEach(function () { |
| 7 | + $this->client = BexioClient::testAccount(); |
| 8 | + $this->queryBuilder = new QueryBuilder( |
| 9 | + resourceClass: TestResource::class, |
| 10 | + client: $this->client |
| 11 | + ); |
| 12 | +}); |
| 13 | + |
| 14 | +it('executes callback when condition is truthy', function () { |
| 15 | + $callbackExecuted = false; |
| 16 | + |
| 17 | + $this->queryBuilder->when(true, function ($query) use (&$callbackExecuted) { |
| 18 | + $callbackExecuted = true; |
| 19 | + }); |
| 20 | + |
| 21 | + expect($callbackExecuted)->toBeTrue(); |
| 22 | +}); |
| 23 | + |
| 24 | +it('does not execute callback when condition is falsy', function () { |
| 25 | + $callbackExecuted = false; |
| 26 | + |
| 27 | + $this->queryBuilder->when(false, function ($query) use (&$callbackExecuted) { |
| 28 | + $callbackExecuted = true; |
| 29 | + }); |
| 30 | + |
| 31 | + expect($callbackExecuted)->toBeFalse(); |
| 32 | +}); |
| 33 | + |
| 34 | +it('does not execute callback when condition is null', function () { |
| 35 | + $callbackExecuted = false; |
| 36 | + |
| 37 | + $this->queryBuilder->when(null, function ($query) use (&$callbackExecuted) { |
| 38 | + $callbackExecuted = true; |
| 39 | + }); |
| 40 | + |
| 41 | + expect($callbackExecuted)->toBeFalse(); |
| 42 | +}); |
| 43 | + |
| 44 | +it('passes the query builder instance to callback', function () { |
| 45 | + $this->queryBuilder->when(true, function ($query) { |
| 46 | + expect($query)->toBeInstanceOf(QueryBuilder::class); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +it('passes the value to callback', function () { |
| 51 | + $value = 'test-value'; |
| 52 | + |
| 53 | + $this->queryBuilder->when($value, function ($query, $passedValue) use ($value) { |
| 54 | + expect($passedValue)->toBe($value); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +it('executes default callback when condition is falsy and default is provided', function () { |
| 59 | + $defaultCallbackExecuted = false; |
| 60 | + |
| 61 | + $this->queryBuilder->when( |
| 62 | + false, |
| 63 | + function ($query) { |
| 64 | + // This should not execute |
| 65 | + }, |
| 66 | + function ($query) use (&$defaultCallbackExecuted) { |
| 67 | + $defaultCallbackExecuted = true; |
| 68 | + } |
| 69 | + ); |
| 70 | + |
| 71 | + expect($defaultCallbackExecuted)->toBeTrue(); |
| 72 | +}); |
| 73 | + |
| 74 | +it('does not execute default callback when condition is truthy', function () { |
| 75 | + $defaultCallbackExecuted = false; |
| 76 | + $mainCallbackExecuted = false; |
| 77 | + |
| 78 | + $this->queryBuilder->when( |
| 79 | + true, |
| 80 | + function ($query) use (&$mainCallbackExecuted) { |
| 81 | + $mainCallbackExecuted = true; |
| 82 | + }, |
| 83 | + function ($query) use (&$defaultCallbackExecuted) { |
| 84 | + $defaultCallbackExecuted = true; |
| 85 | + } |
| 86 | + ); |
| 87 | + |
| 88 | + expect($mainCallbackExecuted)->toBeTrue(); |
| 89 | + expect($defaultCallbackExecuted)->toBeFalse(); |
| 90 | +}); |
| 91 | + |
| 92 | +it('returns the query builder instance for method chaining', function () { |
| 93 | + $result = $this->queryBuilder->when(true, function ($query) { |
| 94 | + // Do nothing |
| 95 | + }); |
| 96 | + |
| 97 | + expect($result)->toBe($this->queryBuilder); |
| 98 | +}); |
| 99 | + |
| 100 | +it('allows chaining multiple when calls', function () { |
| 101 | + $firstExecuted = false; |
| 102 | + $secondExecuted = false; |
| 103 | + |
| 104 | + $this->queryBuilder |
| 105 | + ->when(true, function ($query) use (&$firstExecuted) { |
| 106 | + $firstExecuted = true; |
| 107 | + }) |
| 108 | + ->when(true, function ($query) use (&$secondExecuted) { |
| 109 | + $secondExecuted = true; |
| 110 | + }); |
| 111 | + |
| 112 | + expect($firstExecuted)->toBeTrue(); |
| 113 | + expect($secondExecuted)->toBeTrue(); |
| 114 | +}); |
| 115 | + |
| 116 | +it('can modify query builder parameters in callback', function () { |
| 117 | + $this->queryBuilder->when(true, function ($query) { |
| 118 | + $query->limit(10)->offset(5); |
| 119 | + }); |
| 120 | + |
| 121 | + // Access the parameters using reflection since they're protected |
| 122 | + $reflection = new ReflectionClass($this->queryBuilder); |
| 123 | + $parametersProperty = $reflection->getProperty('parameters'); |
| 124 | + $parametersProperty->setAccessible(true); |
| 125 | + $parameters = $parametersProperty->getValue($this->queryBuilder); |
| 126 | + |
| 127 | + expect($parameters->get('limit'))->toBe(10); |
| 128 | + expect($parameters->get('offset'))->toBe(5); |
| 129 | +}); |
| 130 | + |
| 131 | +it('works with various truthy values', function ($truthyValue) { |
| 132 | + $callbackExecuted = false; |
| 133 | + |
| 134 | + $this->queryBuilder->when($truthyValue, function ($query) use (&$callbackExecuted) { |
| 135 | + $callbackExecuted = true; |
| 136 | + }); |
| 137 | + |
| 138 | + expect($callbackExecuted)->toBeTrue(); |
| 139 | +})->with([ |
| 140 | + [true], |
| 141 | + [1], |
| 142 | + ['non-empty-string'], |
| 143 | + [[1, 2, 3]], |
| 144 | + ]); |
| 145 | + |
| 146 | +it('works with various falsy values', function ($falsyValue) { |
| 147 | + $callbackExecuted = false; |
| 148 | + |
| 149 | + $this->queryBuilder->when($falsyValue, function ($query) use (&$callbackExecuted) { |
| 150 | + $callbackExecuted = true; |
| 151 | + }); |
| 152 | + |
| 153 | + expect($callbackExecuted)->toBeFalse(); |
| 154 | +})->with([ |
| 155 | + [false], |
| 156 | + [0], |
| 157 | + [''], |
| 158 | + [null], |
| 159 | + [[]], |
| 160 | + ]); |
| 161 | + |
| 162 | +// Test helper class |
| 163 | +class TestResource |
| 164 | +{ |
| 165 | + public const INDEX_REQUEST = TestIndexRequest::class; |
| 166 | +} |
| 167 | + |
| 168 | +class TestIndexRequest |
| 169 | +{ |
| 170 | + public function __construct( |
| 171 | + public ?int $limit = null, |
| 172 | + public ?int $offset = null, |
| 173 | + ) { |
| 174 | + } |
| 175 | +} |
| 176 | + |
0 commit comments