public function testIssue20() { $qs = User::objects()->with(['addresses'])->asArray(); $this->assertEquals([['customer' => ['id' => 1, 'user_id' => 1, 'address' => 'address'], 'id' => 1, 'username' => 'foo', 'password' => '', 'user_id' => 1], ['customer' => ['id' => null, 'user_id' => null, 'address' => ''], 'id' => 2, 'username' => 'bar', 'password' => '', 'user_id' => '']], $qs->all()); $qs = User::objects()->filter(['addresses__address' => 'address'])->with(['addresses'])->asArray(); $this->assertEquals([['customer' => ['id' => 1, 'user_id' => 1, 'address' => 'address'], 'id' => 1, 'username' => 'foo', 'password' => '', 'user_id' => 1]], $qs->all()); }
public function testSubqueryIn() { $qs = Group::objects()->filter(['id' => 1]); $users = User::objects()->filter(['groups__pk__in' => $qs->select('id')])->all(); $this->assertEquals(count($users), 1); $this->assertEquals($users[0]->username, 'Max'); }
public function testLookupsClean1() { $filter = ['addresses__address__contains' => 'Anton']; $count = 1; $qs = User::objects()->filter($filter); $this->assertEquals($count, $qs->count()); $this->assertEquals($count, count($qs->all())); }
public function testIssue25() { $this->assertEquals(2, User::objects()->count()); $qs = User::objects()->filter(['addresses__address' => 'address']); $this->assertEquals(1, $qs->count()); User::objects()->filter(['addresses__address' => 'address'])->delete(); $this->assertEquals(1, User::objects()->count()); }
public function testInit() { $this->assertEquals(5, Product::objects()->count()); $this->assertEquals(1, Category::objects()->count()); $this->assertEquals(1, User::objects()->count()); $this->assertEquals(1, Customer::objects()->count()); $this->assertEquals(1, Order::objects()->count()); $this->assertEquals(5, Order::objects()->get(['pk' => 1])->products->count()); $this->assertEquals(1, ProductList::objects()->count()); }
public function testVia() { $group = new Group(); $group->name = 'Administrators'; $this->assertNull($group->pk); $this->assertInstanceOf('\\Mindy\\Orm\\ManyToManyManager', $group->users); $this->assertEquals(0, $group->users->count()); $this->assertEquals([], $group->users->all()); $this->assertTrue($group->save()); $this->assertEquals(1, $group->pk); $user = new User(); $user->username = '******'; $user->password = '******'; $this->assertTrue($user->save()); $this->assertEquals(0, count($group->users->all())); $group->users->link($user); $this->assertEquals(1, count($group->users->all())); $new = Group::objects()->get(['id' => 1]); $this->assertEquals(1, count($new->users->all())); $memberships = Membership::objects()->filter(['group_id' => 1, 'user_id' => 1])->all(); $this->assertEquals(1, count($memberships)); }
public function testResizeUser() { $user = User::findOne(1); $user->setScenario('update'); $this->assertTrue($user->save()); $thumbPath = $user->getThumbUploadPath('image', 'thumb'); $thumbInfo = getimagesize($thumbPath); $this->assertEquals(400, $thumbInfo[0]); $this->assertEquals(300, $thumbInfo[1]); $previewPath = $user->getThumbUploadPath('image', 'preview'); $previewInfo = getimagesize($previewPath); $this->assertEquals(200, $previewInfo[0]); $this->assertEquals(200, $previewInfo[1]); }
public function setUp() { parent::setUp(); $category = new Category(); $category->name = 'test'; $category->save(); $user = new User(); $user->password = 123456; $user->username = '******'; $user->save(); $customer = new Customer(); $customer->user = $user; $customer->address = 'example super address'; $customer->save(); $products = []; foreach ([1, 2, 3, 4, 5] as $i) { $product = new Product(); $product->name = $i; $product->price = $i; $product->description = $i; $product->category = $category; $product->save(); $products[] = $product; } $order1 = new Order(); $order1->customer = $customer; $order1->save(); foreach ($products as $p) { $order1->products->link($p); } $order2 = new Order(); $order2->customer = $customer; $order2->discount = 1; $order2->save(); $order2->products = $products; $order2->save(); }
public function testCustomValidation() { /* @var $nameField \Mindy\Orm\Fields\Field */ $model = new User(); $this->assertFalse($model->isValid()); $this->assertEquals(['username' => ['Cannot be empty', 'Minimal length is 3']], $model->getErrors()); $nameField = $model->getField('username'); $this->assertEquals(['Cannot be empty', 'Minimal length is 3'], $nameField->getErrors()); $this->assertFalse($nameField->isValid()); $this->assertEquals(['Cannot be empty', 'Minimal length is 3'], $nameField->getErrors()); $model->username = '******'; $this->assertEquals('hi', $model->username); $this->assertFalse($model->isValid()); $this->assertEquals('hi', $model->username); $model->username = '******'; $model->isValid(); $this->assertEquals(['username' => ['Maximum length is 20']], $model->getErrors()); $model->isValid(); $this->assertEquals(['username' => ['Maximum length is 20']], $model->getErrors()); }
public function testIssue63() { $this->assertEquals(2, User::objects()->count()); foreach (User::objects()->all() as $i => $user) { $customer = Customer::objects()->get(['pk' => $i + 1]); $customer->user = $user; $customer->save(['user']); } $this->assertEquals(2, Customer::objects()->count()); list($first, $last) = Customer::objects()->order(['pk'])->asArray()->all(); $this->assertEquals(['id' => 1, 'user_id' => 1, 'address' => 'foo'], $first); $this->assertEquals(['id' => 2, 'user_id' => 2, 'address' => 'bar'], $last); $this->assertEquals(1, Customer::objects()->filter(['user__username' => 'foo'])->count()); $this->assertEquals(1, Customer::objects()->filter(['user__username' => 'bar'])->count()); }
/** * @return \yii\db\ActiveQuery */ public function getUsers() { return $this->hasMany(User::className(), ['id' => 'user_id'])->via('projectUsers'); }
/** * method that test if the system found all records * @expectedException \yii\base\Exception */ public function testFindAll() { User::findAll([]); }
/** * Check if relation configs are equal * @param \arogachev\ManyToMany\components\ManyToManyRelation $relation */ protected function assertRelationConfigsEqual($relation) { $this->assertEquals($relation->table, 'tests_users'); $this->assertEquals($relation->ownAttribute, 'test_id'); $this->assertEquals($relation->relatedModel, User::className()); $this->assertEquals($relation->relatedAttribute, 'user_id'); }
public function testSum() { $this->assertEquals(User::objects()->sum('id'), 3); $this->assertEquals(User::objects()->filter(['addresses__address__startswith' => 'A'])->sum('addresses__id'), 3); }
public static function getFields() { return ['user' => ['class' => ForeignField::className(), 'modelClass' => User::className(), 'relatedName' => 'addresses', 'null' => true], 'address' => ['class' => TextField::className()]]; }
/** * @return \yii\db\ActiveQuery */ public function getUsersViaRelation() { return $this->hasMany(User::className(), ['id' => 'user_id'])->via('testUsers'); }
public static function getFields() { return ['name' => ['class' => CharField::className()], 'users' => ['class' => ManyToManyField::className(), 'modelClass' => User::className(), 'through' => Membership::className()]]; }
public static function getFields() { return ['group' => ['class' => ForeignField::className(), 'modelClass' => Group::className()], 'user' => ['class' => ForeignField::className(), 'modelClass' => User::className()]]; }
public function testValuesList() { $group = new Group(); $group->name = 'Administrators'; $group->save(); $groupProg = new Group(); $groupProg->name = 'Programmers'; $groupProg->save(); $anton = new User(); $anton->username = '******'; $anton->password = '******'; $anton->save(); $groupProg->users->link($anton); $anton_home = new Customer(); $anton_home->address = "Anton home"; $anton_home->user = $anton; $anton_home->save(); $anton_work = new Customer(); $anton_work->address = "Anton work"; $anton_work->user = $anton; $anton_work->save(); $max = new User(); $max->username = '******'; $max->password = '******'; $max->save(); $group->users->link($max); $max_home = new Customer(); $max_home->address = "Max home"; $max_home->user = $max; $max_home->save(); $values = Customer::objects()->valuesList(['address', 'user__username']); $this->assertEquals([['address' => 'Anton home', 'user__username' => 'Anton'], ['address' => "Anton work", 'user__username' => 'Anton'], ['address' => "Max home", 'user__username' => 'Max']], $values); $this->assertEquals([['address' => 'Anton home', 'user__username' => 'Anton'], ['address' => "Anton work", 'user__username' => 'Anton'], ['address' => "Max home", 'user__username' => 'Max']], Customer::objects()->valuesList(['address', 'user__username'])); $this->assertEquals(['Anton', 'Anton', 'Max'], Customer::objects()->valuesList(['user__username'], true)); }
public function testManyToManyLookup() { $users = User::objects()->order(['-groups__name'])->all(); $this->assertEquals(count($users), 2); $this->assertEquals($users[0]->username, 'Anton'); $this->assertEquals($users[1]->username, 'Max'); $addresses = User::objects()->order(['groups__name'])->all(); $this->assertEquals(count($users), 2); $this->assertEquals($addresses[0]->username, 'Max'); $this->assertEquals($addresses[1]->username, 'Anton'); }
public function testAssignSingleEmptyObjectToHasManyRelationShouldSucceed() { $project = new Project(); $user = User::findOne(1); $project->users = null; $this->assertEquals(0, count($project->users), 'Project should have 0 users after assignment'); }
public function testSetAttributesIssue65() { list($model, $created) = User::objects()->getOrCreate(['username' => 'Max', 'password' => 'VeryGoodP@ssword']); $this->assertFalse($model->getIsNewRecord()); $this->assertEquals('Max', $model->username); $this->assertEquals('VeryGoodP@ssword', $model->password); $this->assertEquals(1, $model->pk); $model->setAttributes(['username' => 'foo']); $saved = $model->save(['username']); $this->assertTrue($saved); $this->assertEquals('foo', $model->username); // Test $user = User::objects()->get(['pk' => 1]); $this->assertEquals('foo', $user->username); $user->setAttributes(['username' => 'bar']); $this->assertEquals('bar', $user->username); $this->assertEquals(['username' => 'bar'], $user->getDirtyAttributes(['username'])); $saved = $user->save(['username']); $this->assertEquals('bar', $user->username); $this->assertTrue($saved); $user->setAttributes(['password' => 1]); $this->assertEquals(['password' => 1], $user->getDirtyAttributes(['password'])); $user->save(['password']); $this->assertEquals(1, $user->password); $this->assertTrue($saved); }
public function testCreate() { $user = User::objects()->get(['pk' => 1]); Customer::objects()->create(['user' => $user, 'address' => 'Broadway']); Customer::objects()->create(['user_id' => $user->id, 'address' => 'Broadway']); $address1 = Customer::objects()->get(['pk' => 3]); $address2 = Customer::objects()->get(['pk' => 4]); $this->assertEquals($user->id, $address1->user->id); $this->assertEquals($user->id, $address2->user_id); }
public function testIssue19() { $this->assertEquals('SELECT `tests_user_1`.* FROM `tests_user` `tests_user_1` WHERE (`tests_user_1`.`id`=6) LIMIT 1', User::objects()->limit(1)->getSql(['id' => 6])); $this->assertEquals('SELECT `tests_user_1`.* FROM `tests_user` `tests_user_1` WHERE ((`tests_user_1`.`id`=6)) LIMIT 1', User::objects()->filter(['id' => 6])->limit(1)->getSql()); }
public function lookupProvider() { return [[Customer::className(), ['user__username' => 'Anton'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username"=\'Anton\')', 2], [Customer::className(), ['user__username__startswith' => 'A'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username" LIKE \'A%\')', 2], [Customer::className(), ['user__groups__name' => 'Administrators'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_group_4"."name"=\'Administrators\')', 3], [Customer::className(), ['user__groups__name__endswith' => 's'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_group_4"."name" LIKE \'%s\')', 3], [User::className(), ['addresses__address__contains' => 'Anton'], 'SELECT COUNT(DISTINCT "tests_user_1"."id") FROM "tests_user" "tests_user_1" LEFT OUTER JOIN "tests_customer" "tests_customer_2" ON "tests_user_1"."id" = "tests_customer_2"."user_id" WHERE ("tests_customer_2"."address" LIKE \'%Anton%\')', 1], [Customer::className(), ['user__username' => 'Max', 'user__pk' => '2'], 'SELECT COUNT(*) FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" WHERE ("tests_user_2"."username"=\'Max\') AND ("tests_user_2"."id"=\'2\')', 1], [Customer::className(), ['user__username' => 'Max', 'user__groups__pk' => '1'], 'SELECT COUNT(DISTINCT "tests_customer_1"."id") FROM "tests_customer" "tests_customer_1" LEFT OUTER JOIN "tests_user" "tests_user_2" ON "tests_customer_1"."user_id" = "tests_user_2"."id" LEFT OUTER JOIN "tests_membership" "tests_membership_3" ON "tests_user_2"."id" = "tests_membership_3"."user_id" LEFT OUTER JOIN "tests_group" "tests_group_4" ON "tests_membership_3"."group_id" = "tests_group_4"."id" WHERE ("tests_user_2"."username"=\'Max\') AND ("tests_group_4"."id"=\'1\')', 1]]; }