Made user for UserAttribut optional

This commit is contained in:
Kevin Frantz 2019-02-03 00:30:53 +01:00
parent aa735367d9
commit bd2318ab5b
3 changed files with 33 additions and 1 deletions

View File

@ -7,21 +7,37 @@ use App\Entity\UserInterface;
/** /**
* @author kevinfrantz * @author kevinfrantz
*
* @see UserAttributInterface
*/ */
trait UserAttribut trait UserAttribut
{ {
/** /**
* @var User * @var User|null
*/ */
protected $user; protected $user;
/**
* @param UserInterface $user
*/
public function setUser(UserInterface $user): void public function setUser(UserInterface $user): void
{ {
$this->user = $user; $this->user = $user;
} }
/**
* @return UserInterface
*/
public function getUser(): UserInterface public function getUser(): UserInterface
{ {
return $this->user; return $this->user;
} }
/**
* @return bool
*/
public function hasUser(): bool
{
return isset($this->user);
}
} }

View File

@ -9,7 +9,18 @@ use App\Entity\UserInterface;
*/ */
interface UserAttributInterface interface UserAttributInterface
{ {
/**
* @param UserInterface $user
*/
public function setUser(UserInterface $user): void; public function setUser(UserInterface $user): void;
/**
* @return UserInterface
*/
public function getUser(): UserInterface; public function getUser(): UserInterface;
/**
* @return bool Returns if a user is set
*/
public function hasUser(): bool;
} }

View File

@ -7,6 +7,9 @@ use App\Attribut\UserAttributInterface;
use App\Attribut\UserAttribut; use App\Attribut\UserAttribut;
use App\Entity\UserInterface; use App\Entity\UserInterface;
/**
* @author kevinfrantz
*/
class UserAttributTest extends TestCase class UserAttributTest extends TestCase
{ {
/** /**
@ -23,6 +26,7 @@ class UserAttributTest extends TestCase
public function testConstructor(): void public function testConstructor(): void
{ {
$this->assertFalse($this->user->hasUser());
$this->expectException(\TypeError::class); $this->expectException(\TypeError::class);
$this->user->getUser(); $this->user->getUser();
} }
@ -32,5 +36,6 @@ class UserAttributTest extends TestCase
$user = $this->createMock(UserInterface::class); $user = $this->createMock(UserInterface::class);
$this->assertNull($this->user->setUser($user)); $this->assertNull($this->user->setUser($user));
$this->assertEquals($user, $this->user->getUser()); $this->assertEquals($user, $this->user->getUser());
$this->assertTrue($this->user->hasUser());
} }
} }