public function testAccountCreation()
 {
     $user = \User::newFromName('foo');
     $user->setEmail('email');
     $user->setRealName('realname');
     $req = new PasswordAuthenticationRequest();
     $req->action = AuthManager::ACTION_CREATE;
     $reqs = [PasswordAuthenticationRequest::class => $req];
     $plugin = $this->getMock('AuthPlugin');
     $plugin->expects($this->any())->method('domainList')->willReturn([]);
     $plugin->expects($this->any())->method('canCreateAccounts')->will($this->returnValue(false));
     $plugin->expects($this->never())->method('addUser');
     $provider = new AuthPluginPrimaryAuthenticationProvider($plugin);
     try {
         $provider->beginPrimaryAccountCreation($user, $user, []);
         $this->fail('Expected exception was not thrown');
     } catch (\BadMethodCallException $ex) {
         $this->assertSame('Shouldn\'t call this when accountCreationType() is NONE', $ex->getMessage());
     }
     $plugin = $this->getMock('AuthPlugin');
     $plugin->expects($this->any())->method('domainList')->willReturn([]);
     $plugin->expects($this->any())->method('canCreateAccounts')->will($this->returnValue(true));
     $plugin->expects($this->never())->method('addUser');
     $provider = new AuthPluginPrimaryAuthenticationProvider($plugin);
     $this->assertEquals(AuthenticationResponse::newAbstain(), $provider->beginPrimaryAccountCreation($user, $user, []));
     $req->username = '******';
     $req->password = null;
     $this->assertEquals(AuthenticationResponse::newAbstain(), $provider->beginPrimaryAccountCreation($user, $user, $reqs));
     $req->username = null;
     $req->password = '******';
     $this->assertEquals(AuthenticationResponse::newAbstain(), $provider->beginPrimaryAccountCreation($user, $user, $reqs));
     $req->username = '******';
     $req->password = '******';
     $plugin = $this->getMock('AuthPlugin');
     $plugin->expects($this->any())->method('domainList')->willReturn([]);
     $plugin->expects($this->any())->method('canCreateAccounts')->will($this->returnValue(true));
     $plugin->expects($this->once())->method('addUser')->with($this->callback(function ($u) {
         return $u instanceof \User && $u->getName() === 'Foo';
     }), $this->equalTo('bar'), $this->equalTo('email'), $this->equalTo('realname'))->will($this->returnValue(true));
     $provider = new AuthPluginPrimaryAuthenticationProvider($plugin);
     $this->assertEquals(AuthenticationResponse::newPass(), $provider->beginPrimaryAccountCreation($user, $user, $reqs));
     $plugin = $this->getMock('AuthPlugin');
     $plugin->expects($this->any())->method('domainList')->willReturn([]);
     $plugin->expects($this->any())->method('canCreateAccounts')->will($this->returnValue(true));
     $plugin->expects($this->once())->method('addUser')->with($this->callback(function ($u) {
         return $u instanceof \User && $u->getName() === 'Foo';
     }), $this->equalTo('bar'), $this->equalTo('email'), $this->equalTo('realname'))->will($this->returnValue(false));
     $provider = new AuthPluginPrimaryAuthenticationProvider($plugin);
     $ret = $provider->beginPrimaryAccountCreation($user, $user, $reqs);
     $this->assertSame(AuthenticationResponse::FAIL, $ret->status);
     $this->assertSame('authmanager-authplugin-create-fail', $ret->message->getKey());
     $plugin = $this->getMock('AuthPlugin');
     $plugin->expects($this->any())->method('canCreateAccounts')->will($this->returnValue(true));
     $plugin->expects($this->any())->method('domainList')->will($this->returnValue(['Domain1', 'Domain2']));
     $plugin->expects($this->any())->method('validDomain')->will($this->returnCallback(function ($domain) {
         return in_array($domain, ['Domain1', 'Domain2']);
     }));
     $plugin->expects($this->once())->method('setDomain')->with($this->equalTo('Domain2'));
     $plugin->expects($this->once())->method('addUser')->with($this->callback(function ($u) {
         return $u instanceof \User && $u->getName() === 'Foo';
     }), $this->equalTo('bar'))->will($this->returnValue(true));
     $provider = new AuthPluginPrimaryAuthenticationProvider($plugin);
     list($req) = $provider->getAuthenticationRequests(AuthManager::ACTION_CREATE, []);
     $req->username = '******';
     $req->password = '******';
     $req->domain = 'Domain2';
     $provider->beginPrimaryAccountCreation($user, $user, [$req]);
 }