Skip to content

Commit

Permalink
test: add tests for wheres
Browse files Browse the repository at this point in the history
  • Loading branch information
kauffinger committed Nov 29, 2024
1 parent 0f4637e commit 530ea7d
Show file tree
Hide file tree
Showing 2 changed files with 251 additions and 0 deletions.
144 changes: 144 additions & 0 deletions tests/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
107 changes: 107 additions & 0 deletions tests/Query/Concerns/NonFilterableTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

declare(strict_types=1);

use Innobrain\OnOfficeAdapter\Query\Builder;
use Innobrain\OnOfficeAdapter\Query\Concerns\NonFilterable;

class NonFilterableTestClass extends Builder
{
use NonFilterable;
}

describe('NonFilterable', function () {
it('where should return instance without modifying filters', function () {
$instance = new NonFilterableTestClass;

$result = $instance->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([]);
});
});

0 comments on commit 530ea7d

Please sign in to comment.