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

Fix criteria for valid user data #1108

Merged
merged 7 commits into from Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Fix missing source code excerpts for stacktrace frames whose absolute file path is equal to the file path (#1104)
- Fix requirements to construct a valid object instance of the `UserDataBag` class (#1108)

## 3.0.2 (2020-10-02)

Expand Down
43 changes: 17 additions & 26 deletions src/UserDataBag.php
Expand Up @@ -34,8 +34,17 @@ final class UserDataBag
*/
private $metadata = [];

private function __construct()
/**
* UserDataBag constructor.
*
* @param string|int|null $id
*/
public function __construct($id = null, ?string $email = null, ?string $ipAddress = null, ?string $username = null)
{
$this->setId($id);
$this->setEmail($email);
$this->setIpAddress($ipAddress);
$this->setUsername($username);
}

/**
Expand All @@ -45,10 +54,7 @@ private function __construct()
*/
public static function createFromUserIdentifier($id): self
{
$instance = new self();
$instance->setId($id);

return $instance;
return new self($id);
}

/**
Expand All @@ -58,10 +64,7 @@ public static function createFromUserIdentifier($id): self
*/
public static function createFromUserIpAddress(string $ipAddress): self
{
$instance = new self();
$instance->setIpAddress($ipAddress);

return $instance;
return new self(null, null, $ipAddress);
}

/**
Expand All @@ -71,25 +74,21 @@ public static function createFromUserIpAddress(string $ipAddress): self
*/
public static function createFromArray(array $data): self
{
if (!isset($data['id']) && !isset($data['ip_address'])) {
throw new \InvalidArgumentException('Either the "id" or the "ip_address" field must be set.');
}

$instance = new self();

foreach ($data as $field => $value) {
switch ($field) {
case 'id':
$instance->setId($data['id']);
$instance->setId($value);
break;
case 'ip_address':
$instance->setIpAddress($data['ip_address']);
$instance->setIpAddress($value);
break;
case 'email':
$instance->setEmail($data['email']);
$instance->setEmail($value);
break;
case 'username':
$instance->setUsername($data['username']);
$instance->setUsername($value);
break;
default:
$instance->setMetadata($field, $value);
Expand Down Expand Up @@ -117,11 +116,7 @@ public function getId()
*/
public function setId($id): void
{
if (null === $id && null === $this->ipAddress) {
throw new \BadMethodCallException('Either the IP address or the ID must be set.');
}

if (!\is_string($id) && !\is_int($id)) {
if (null !== $id && !\is_string($id) && !\is_int($id)) {
throw new \UnexpectedValueException(sprintf('Expected an integer or string value for the $id argument. Got: "%s".', get_debug_type($id)));
}

Expand Down Expand Up @@ -183,10 +178,6 @@ public function setIpAddress(?string $ipAddress): void
throw new \InvalidArgumentException(sprintf('The "%s" value is not a valid IP address.', $ipAddress));
}

if (null === $ipAddress && null === $this->id) {
throw new \BadMethodCallException('Either the IP address or the ID must be set.');
}

$this->ipAddress = $ipAddress;
}

Expand Down
83 changes: 44 additions & 39 deletions tests/UserDataBagTest.php
Expand Up @@ -9,17 +9,10 @@

final class UserDataBagTest extends TestCase
{
public function testConstructorThrowsIfArgumentIsInvalid(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');

UserDataBag::createFromUserIpAddress('foo');
}

public function testGettersAndSetters(): void
{
$userDataBag = UserDataBag::createFromUserIdentifier('unique_id');
$userDataBag = new UserDataBag();
$userDataBag->setId('unique_id');
$userDataBag->setIpAddress('127.0.0.1');
$userDataBag->setEmail('foo@example.com');
$userDataBag->setUsername('my_user');
Expand Down Expand Up @@ -76,35 +69,26 @@ public function createFromArrayDataProvider(): iterable
];

yield [
[
'id' => 'unique_id',
'email' => 'foo@example.com',
],
'unique_id',
['email' => 'foo@example.com'],
null,
null,
'foo@example.com',
null,
[],
];

yield [
[
'id' => 'unique_id',
'username' => 'my_user',
],
'unique_id',
['username' => 'my_user'],
null,
null,
null,
'my_user',
[],
];

yield [
[
'id' => 'unique_id',
'subscription' => 'basic',
],
'unique_id',
['subscription' => 'basic'],
null,
null,
null,
null,
Expand All @@ -113,17 +97,40 @@ public function createFromArrayDataProvider(): iterable
}

/**
* @dataProvider setIdThrowsIfValueIsUnexpectedValueDataProvider
* @dataProvider unexpectedValueForIdFieldDataProvider
*/
public function testConstructorThrowsIfIdValueIsUnexpectedValue($value, string $expectedExceptionMessage): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

new UserDataBag($value);
}

/**
* @dataProvider unexpectedValueForIdFieldDataProvider
*/
public function testSetIdThrowsIfValueIsUnexpectedValue($value, string $expectedExceptionMessage): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

$userDataBag = new UserDataBag();
$userDataBag->setId($value);
}

/**
* @dataProvider unexpectedValueForIdFieldDataProvider
*/
public function testCreateFromUserIdentifierThrowsIfArgumentIsInvalid($value, string $expectedExceptionMessage): void
{
$this->expectException(\UnexpectedValueException::class);
$this->expectExceptionMessage($expectedExceptionMessage);

UserDataBag::createFromUserIdentifier($value);
}

public function setIdThrowsIfValueIsUnexpectedValueDataProvider(): iterable
public function unexpectedValueForIdFieldDataProvider(): iterable
{
yield [
12.34,
Expand All @@ -136,31 +143,29 @@ public function setIdThrowsIfValueIsUnexpectedValueDataProvider(): iterable
];
}

public function testSetIdThrowsIfBothArgumentAndIpAddressAreNull(): void
public function testConstructorThrowsIfIpAddressArgumentIsInvalid(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Either the IP address or the ID must be set.');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');

$userDataBag = UserDataBag::createFromUserIdentifier('unique_id');
$userDataBag->setId(null);
new UserDataBag(null, null, 'foo');
}

public function testSetIpAddressThrowsIfBothArgumentAndIdAreNull(): void
public function testSetIpAddressThrowsIfArgumentIsInvalid(): void
{
$this->expectException(\BadMethodCallException::class);
$this->expectExceptionMessage('Either the IP address or the ID must be set.');
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');

$userDataBag = UserDataBag::createFromUserIpAddress('127.0.0.1');
$userDataBag->setIpAddress(null);
$userDataBag = new UserDataBag();
$userDataBag->setIpAddress('foo');
}

public function testSetIpAddressThrowsIfArgumentIsInvalid(): void
public function testCreateFromIpAddressThrowsIfArgumentIsInvalid(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The "foo" value is not a valid IP address.');

$userDataBag = UserDataBag::createFromUserIpAddress('127.0.0.1');
$userDataBag->setIpAddress('foo');
UserDataBag::createFromUserIpAddress('foo');
}

public function testMerge(): void
Expand Down