From 530ea7d480c0edfa7f06212114f4f3465fd12ede Mon Sep 17 00:00:00 2001 From: Konstantin Auffinger Date: Fri, 29 Nov 2024 09:01:13 +0100 Subject: [PATCH] test: add tests for wheres --- tests/Query/BuilderTest.php | 144 +++++++++++++++++++++ tests/Query/Concerns/NonFilterableTest.php | 107 +++++++++++++++ 2 files changed, 251 insertions(+) create mode 100644 tests/Query/Concerns/NonFilterableTest.php diff --git a/tests/Query/BuilderTest.php b/tests/Query/BuilderTest.php index 000554b..518d16f 100644 --- a/tests/Query/BuilderTest.php +++ b/tests/Query/BuilderTest.php @@ -256,6 +256,150 @@ }); }); +describe('whereNot', function () { + it('should add a not equal filter to the filters property', function () { + $builder = new Builder; + + $builder->whereNot('ID', 1); + + expect($builder->filters)->toBe([ + 'ID' => [ + ['!=', 1], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereNot('ID', 1); + + expect($result)->toBeInstanceOf(Builder::class); + }); +}); + +describe('whereIn', function () { + it('should add an in filter to the filters property', function () { + $builder = new Builder; + + $builder->whereIn('ID', [1, 2, 3]); + + expect($builder->filters)->toBe([ + 'ID' => [ + ['in', [1, 2, 3]], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereIn('ID', [1, 2, 3]); + + expect($result)->toBeInstanceOf(Builder::class); + }); +}); + +describe('whereNotIn', function () { + it('should add a not in filter to the filters property', function () { + $builder = new Builder; + + $builder->whereNotIn('ID', [1, 2, 3]); + + expect($builder->filters)->toBe([ + 'ID' => [ + ['not in', [1, 2, 3]], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereNotIn('ID', [1, 2, 3]); + + expect($result)->toBeInstanceOf(Builder::class); + }); +}); + +describe('whereBetween', function () { + it('should add a between filter to the filters property', function () { + $builder = new Builder; + + $builder->whereBetween('ID', 1, 10); + + expect($builder->filters)->toBe([ + 'ID' => [ + ['between', [1, 10]], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereBetween('ID', 1, 10); + + expect($result)->toBeInstanceOf(Builder::class); + }); + + it('should work with string values', function () { + $builder = new Builder; + + $builder->whereBetween('date', '2023-01-01', '2023-12-31'); + + expect($builder->filters)->toBe([ + 'date' => [ + ['between', ['2023-01-01', '2023-12-31']], + ], + ]); + }); +}); + +describe('whereLike', function () { + it('should add a like filter to the filters property', function () { + $builder = new Builder; + + $builder->whereLike('Name', 'John'); + + expect($builder->filters)->toBe([ + 'Name' => [ + ['like', 'John'], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereLike('Name', 'John'); + + expect($result)->toBeInstanceOf(Builder::class); + }); +}); + +describe('whereNotLike', function () { + it('should add a not like filter to the filters property', function () { + $builder = new Builder; + + $builder->whereNotLike('Name', 'John'); + + expect($builder->filters)->toBe([ + 'Name' => [ + ['not like', 'John'], + ], + ]); + }); + + it('should return the builder instance', function () { + $builder = new Builder; + + $result = $builder->whereNotLike('Name', 'John'); + + expect($result)->toBeInstanceOf(Builder::class); + }); +}); + describe('getFilters', function () { it('should return the filters property', function () { $builder = new Builder; diff --git a/tests/Query/Concerns/NonFilterableTest.php b/tests/Query/Concerns/NonFilterableTest.php new file mode 100644 index 0000000..32e2f1d --- /dev/null +++ b/tests/Query/Concerns/NonFilterableTest.php @@ -0,0 +1,107 @@ +where('column', '=', 'value'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereNot should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereNot('column', 'value'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereIn should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereIn('column', ['value1', 'value2']); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereNotIn should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereNotIn('column', ['value1', 'value2']); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereBetween should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereBetween('column', 1, 10); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + + // Test with string values + $result = $instance->whereBetween('date', '2023-01-01', '2023-12-31'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereLike should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereLike('column', 'value'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('whereNotLike should return instance without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance->whereNotLike('column', 'value'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); + + it('should chain multiple where methods without modifying filters', function () { + $instance = new NonFilterableTestClass; + + $result = $instance + ->where('column1', 'value1') + ->whereNot('column2', 'value2') + ->whereIn('column3', ['value3']) + ->whereNotIn('column4', ['value4']) + ->whereBetween('column5', 1, 10) + ->whereLike('column6', 'value6') + ->whereNotLike('column7', 'value7'); + + expect($result)->toBe($instance) + ->and($result)->toBeInstanceOf(NonFilterableTestClass::class) + ->and($result->filters)->toBe([]); + }); +});