Skip to content
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

Use Scope class for filtering/where impl. for Model #660

Merged
merged 57 commits into from
Jul 23, 2020
Merged
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
e1e0ae7
[feature] introduce basic model scope functionality
georgehristov Jul 21, 2020
c7e2c85
[fix] remove Model::$conditions property
georgehristov Jul 22, 2020
55e527b
[fix] code comments
georgehristov Jul 22, 2020
b25b01e
[update] require strictly correct junction value
georgehristov Jul 22, 2020
64ac1ca
[fix] use assertSame in tests
georgehristov Jul 22, 2020
e57df59
[update] remove unnecessary code
georgehristov Jul 22, 2020
0e0a040
[update] use limit as param name
georgehristov Jul 22, 2020
aae0f5d
[update] shorter way to ensure AND junction in Scope
georgehristov Jul 22, 2020
a233b34
[update] use str_contains
georgehristov Jul 22, 2020
6ff4b73
[fix] change more assertEquals to assertSame
georgehristov Jul 22, 2020
e3783e8
[update] flexibility for bool only when 1 argument
georgehristov Jul 22, 2020
371d2fa
[update] revert commented out code
georgehristov Jul 22, 2020
e0fd02c
[update] set getModel return type
georgehristov Jul 22, 2020
faef583
[update] use str_contains
georgehristov Jul 22, 2020
88ab0f6
[update] remove ! and ? special symbols
georgehristov Jul 23, 2020
845ca31
[update] introduce multi-level reference condition testing
georgehristov Jul 23, 2020
f7f0d13
[update] remove unnecessary check
georgehristov Jul 23, 2020
08be267
[fix] code comments
georgehristov Jul 23, 2020
f5051e6
[update] improve CS
georgehristov Jul 23, 2020
9c497a5
[update] use spread operator in condition merging
georgehristov Jul 23, 2020
a1c17c1
[update] introduce return type for simplify
georgehristov Jul 23, 2020
b9895f7
[update] introduce test for empty IN array
georgehristov Jul 23, 2020
72286a1
[fix] CS fixer
georgehristov Jul 23, 2020
01b9052
[update] remove unnecessary check
georgehristov Jul 23, 2020
c395540
[update] simplify CompoundCondition::clear
georgehristov Jul 23, 2020
99ec94e
[update] trigger onModelChange only if not same model
georgehristov Jul 23, 2020
a5760b7
[update] initQueryConditions returns void
georgehristov Jul 23, 2020
2438abc
[update] include repeating model reference testing
georgehristov Jul 23, 2020
c3849a9
[update] toArrat to toQueryArgumentsArray
georgehristov Jul 23, 2020
463e82a
[update] isEmpty method
georgehristov Jul 23, 2020
acc19e1
[update] use single quotes
georgehristov Jul 23, 2020
0c2cd3e
[fix] operator to words falls back to actual operator value
georgehristov Jul 23, 2020
8f25544
[fix] array persistence query
georgehristov Jul 23, 2020
e62780c
[update] add complex test for referenced records
mvorisek Jul 23, 2020
3662dc8
[fix] remove ? and ! special symbols in toWords
georgehristov Jul 23, 2020
b8b579d
[update] optional model as argument on toWords
georgehristov Jul 23, 2020
95d93df
[fix] operatorToWords
georgehristov Jul 23, 2020
5643d6c
[update] rename toQueryArgumentsArray to toQueryArguments
georgehristov Jul 23, 2020
f8e3e4f
[fix] CS Fixer
georgehristov Jul 23, 2020
9b3d02c
[fix] typo
georgehristov Jul 23, 2020
5f3b779
typo
DarkSide666 Jul 23, 2020
caf4c39
typo
DarkSide666 Jul 23, 2020
719d59e
add complex but always true condition
mvorisek Jul 23, 2020
accfa6c
make onChangeModel protected
mvorisek Jul 23, 2020
b28e39a
[update] separate exception info
georgehristov Jul 23, 2020
3aa7820
[fix] condition with NULL value
georgehristov Jul 23, 2020
47ca200
no need to clone in BasicCondition::toQueryArguments()
mvorisek Jul 23, 2020
08c5191
fix toWords() doc
mvorisek Jul 23, 2020
f22c8a7
remove on() (expensive model clone)
mvorisek Jul 23, 2020
4464c57
[update] simplify toWords further
georgehristov Jul 23, 2020
408e104
rename mergeXx to createXx
mvorisek Jul 23, 2020
1ed5129
new self to new static
mvorisek Jul 23, 2020
8487663
[fix] test toWords
georgehristov Jul 23, 2020
8197334
fix Scope::createAnd/Or for real world usage
mvorisek Jul 23, 2020
1de2aec
make Scope constructor protected
mvorisek Jul 23, 2020
0b65f8e
fix CS
mvorisek Jul 23, 2020
954db74
[update] introduce addCondition method to CompoundCondition
georgehristov Jul 23, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[update] introduce multi-level reference condition testing
georgehristov committed Jul 23, 2020
commit 845ca31219e746cf2c97fb7fe947c7daee3bc610
84 changes: 74 additions & 10 deletions tests/ScopeTest.php
Original file line number Diff line number Diff line change
@@ -46,13 +46,34 @@ public function init(): void
$this->hasOne('country_id', new SCountry())
->withTitle()
->addFields(['country_code' => 'code', 'is_eu']);

$this->hasMany('Tickets', [new STicket(), 'their_field' => 'user']);
}
}

class STicket extends Model
{
public $table = 'ticket';

public $caption = 'Ticket';

public function init(): void
{
parent::init();

$this->addField('number');
$this->addField('venue');
$this->addField('is_vip', ['type' => 'boolean', 'default' => false]);

$this->hasOne('user', new SUser());
}
}

class ScopeTest extends \atk4\schema\PhpunitTestCase
{
protected $user;
protected $country;
protected $ticket;

protected function setUp(): void
{
@@ -84,6 +105,16 @@ protected function setUp(): void
['name' => 'Aerton', 'surname' => 'Senna', 'country_code' => 'BR'],
['name' => 'Rubens', 'surname' => 'Barichello', 'country_code' => 'BR'],
]);

$this->ticket = new STicket($this->db);

$this->getMigrator($this->ticket)->drop()->create();

$this->ticket->import([
['number' => '001', 'venue' => 'Best Stadium', 'user' => 1],
['number' => '002', 'venue' => 'Best Stadium', 'user' => 2],
['number' => '003', 'venue' => 'Best Stadium', 'user' => 2],
]);
}

public function testCondition()
@@ -162,31 +193,64 @@ public function testContitionOnReferencedRecords()
$this->assertEquals('LV', $u->get('country_code'));
}

$user = clone $this->user;

// users that have no ticket
$user->addCondition('Tickets/#', 0);

$this->assertEquals(3, $user->action('count')->getOne());

foreach ($user as $u) {
$this->assertTrue(in_array($u->get('name'), ['Alain', 'Aerton', 'Rubens']));
}

$country = clone $this->country;

// countries with no users
$country->addCondition('Users/!');
// countries with more than one user
$country->addCondition('Users/#', '>', 1);

foreach ($country as $c) {
$this->assertEmpty($c->get('user_names'));
$this->assertEquals('BR', $c->get('code'));
}

$country = clone $this->country;

// countries with any user
$country->addCondition('Users/?');
// countries with users that have ticket number 001
$country->addCondition('Users/Tickets/number', '001');

foreach ($country as $c) {
$this->assertNotEmpty($c->get('user_names'));
$this->assertEquals('CA', $c->get('code'));
}

$country = clone $this->country;

// countries with users that have more than one ticket
$country->addCondition('Users/Tickets/#', '>', 1);

foreach ($country as $c) {
$this->assertEquals('LV', $c->get('code'));
}

$country = clone $this->country;

// countries with more than one user
$country->addCondition('Users/#', '>', 1);
// countries with users that have any tickets
$country->addCondition('Users/Tickets/#');

$this->assertEquals(2, $country->action('count')->getOne());

foreach ($country as $c) {
$this->assertTrue(in_array($c->get('code'), ['LV', 'CA']));
}

$country = clone $this->country;

// countries with users that have no tickets
$country->addCondition('Users/Tickets/#', 0);

$this->assertEquals(2, $country->action('count')->getOne());

foreach ($country as $c) {
$this->assertEquals('BR', $c->get('code'));
$this->assertTrue(in_array($c->get('code'), ['FR', 'BR']));
}
georgehristov marked this conversation as resolved.
Show resolved Hide resolved
}