/** * @covers Foote\Ginny\Map\BaseModel::primaryFields * @covers Foote\Ginny\Map\BaseModel::belongsToMany * @covers Foote\Ginny\Map\BaseModel::titleField */ public function testManyToMany() { // constuctor $user = new BaseModel('User'); $role = new BaseModel('Role'); $userRole = new BaseModel('UserRole'); $userRole->manyToMany = true; $this->assertEquals('UserRole', $userRole->name); $this->assertEquals('UserRoles', $userRole->plural); /** * Add $fields BaseField[] to $userRole BaseModel * * UserRole.id * UserRole.user_id * UserRole.role_id * * @see BaseField */ $fields['UserRole']['id'] = new BaseField('id'); $fields['UserRole']['id']->primary = true; $fields['UserRole']['id']->autoIncrement = true; $userRole->addField($fields['UserRole']['id']); $fields['UserRole']['user_id'] = new BaseField('user_id'); $fields['UserRole']['user_id']->type = 'integer'; $fields['UserRole']['user_id']->owner = $user; $userRole->addField($fields['UserRole']['user_id']); $fields['UserRole']['role_id'] = new BaseField('role_id'); $fields['UserRole']['role_id']->type = 'integer'; $fields['UserRole']['role_id']->owner = $role; $userRole->addField($fields['UserRole']['role_id']); /** * Test functions that fetched filtered collections from $user->fields * * @see BaseModel::titleField * @see BaseModel::primaryFields */ $this->assertEquals('id', $userRole->titleField()); $collection['primaryFields'] = $userRole->primaryFields(); $this->assertTrue($collection['primaryFields']->containsKey('id')); $this->assertFalse($collection['primaryFields']->containsKey('user_id')); $this->assertFalse($collection['primaryFields']->containsKey('role_id')); /** * * UserRole belongsTo User * UserRole belongsTo Role * User belongsToMany Role * Role belongsToMany User * * we need $userRole to have it's respective "belongsTo" before we * add belongsToMany to $user and $role * * @see BaseAssociation */ BaseAssociation::create('User', ['owner' => $userRole, 'ownerKey' => 'user_id', 'type' => 'belongsTo', 'target' => $user, 'targetKey' => 'id']); BaseAssociation::create('Role', ['owner' => $userRole, 'ownerKey' => 'role_id', 'type' => 'belongsTo', 'target' => $role, 'targetKey' => 'id']); # now we are ready for $user belongsToMany $role, etc BaseAssociation::create('Role', ['owner' => $user, 'ownerKey' => 'id', 'type' => 'belongsToMany', 'target' => $role, 'targetKey' => 'id', 'pivot' => $userRole]); BaseAssociation::create('User', ['owner' => $role, 'ownerKey' => 'id', 'type' => 'belongsToMany', 'target' => $user, 'targetKey' => 'id', 'pivot' => $userRole]); /** * Test functions that fetched filtered collections from $user->fields * * @see BaseModel::belongsToMany */ $collection['belongsToMany'] = $user->belongsToMany(); $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $collection['belongsToMany']); $this->assertTrue($collection['belongsToMany']->containsKey('Role')); $collection['belongsToMany'] = $role->belongsToMany(); $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $collection['belongsToMany']); $this->assertTrue($collection['belongsToMany']->containsKey('User')); }