Refactor SessionFlags handling and improve test coverage

This commit is contained in:
netkas 2025-01-14 14:53:47 -05:00
parent 9d98659541
commit cd12c1b987
4 changed files with 293 additions and 36 deletions

View file

@ -0,0 +1,180 @@
<?php
namespace Socialbox\Enums\Flags;
use PHPUnit\Framework\TestCase;
class SessionFlagsTest extends TestCase
{
/**
* Test that fromString correctly converts a valid comma-separated string to an array of SessionFlags.
*/
public function testFromStringWithValidString()
{
$flagString = 'SET_PASSWORD,SET_EMAIL,VER_SMS';
$expectedFlags = [
SessionFlags::SET_PASSWORD,
SessionFlags::SET_EMAIL,
SessionFlags::VER_SMS,
];
$result = SessionFlags::fromString($flagString);
$this->assertEquals($expectedFlags, $result);
}
/**
* Test that fromString handles an empty string correctly by returning an empty array.
*/
public function testFromStringWithEmptyString()
{
$flagString = '';
$result = SessionFlags::fromString($flagString);
$this->assertEquals([], $result);
}
/**
* Test that fromString correctly trims whitespace from flag values.
*/
public function testFromStringWithWhitespace()
{
$flagString = ' SET_PASSWORD , SET_EMAIL , VER_SMS ';
$expectedFlags = [
SessionFlags::SET_PASSWORD,
SessionFlags::SET_EMAIL,
SessionFlags::VER_SMS,
];
$result = SessionFlags::fromString($flagString);
$this->assertEquals($expectedFlags, $result);
}
/**
* Test that fromString throws an error for invalid values.
*/
public function testFromStringWithInvalidValues()
{
$this->expectException(\ValueError::class);
$flagString = 'INVALID_FLAG';
SessionFlags::fromString($flagString);
}
/**
* Test that fromString works for a single valid flag.
*/
public function testFromStringWithSingleValue()
{
$flagString = 'SET_PASSWORD';
$expectedFlags = [SessionFlags::SET_PASSWORD];
$result = SessionFlags::fromString($flagString);
$this->assertEquals($expectedFlags, $result);
}
/**
* Test that fromString correctly handles duplicate flag values in the input string.
*/
public function testFromStringWithDuplicateValues()
{
$flagString = 'SET_EMAIL,SET_EMAIL,VER_SMS';
$expectedFlags = [
SessionFlags::SET_EMAIL,
SessionFlags::SET_EMAIL,
SessionFlags::VER_SMS,
];
$result = SessionFlags::fromString($flagString);
$this->assertEquals($expectedFlags, $result);
}
/**
* Test that isComplete returns true for an empty array of flags.
*/
public function testIsCompleteWithEmptyFlags()
{
$flags = [];
$result = SessionFlags::isComplete($flags);
$this->assertTrue($result);
}
/**
* Test that isComplete returns false when registration flags are incomplete.
*/
public function testIsCompleteWithIncompleteRegistrationFlags()
{
$flags = [
SessionFlags::REGISTRATION_REQUIRED,
SessionFlags::SET_PASSWORD,
];
$result = SessionFlags::isComplete($flags);
$this->assertFalse($result);
}
/**
* Test that isComplete returns false when authentication flags are incomplete.
*/
public function testIsCompleteWithIncompleteAuthenticationFlags()
{
$flags = [
SessionFlags::AUTHENTICATION_REQUIRED,
SessionFlags::VER_PASSWORD,
];
$result = SessionFlags::isComplete($flags);
$this->assertFalse($result);
}
/**
* Test that isComplete returns true when registration flags are complete.
*/
public function testIsCompleteWithCompleteRegistrationFlags()
{
$flags = [
SessionFlags::REGISTRATION_REQUIRED,
];
$result = SessionFlags::isComplete($flags);
$this->assertTrue($result);
}
/**
* Test that isComplete returns true when authentication flags are complete.
*/
public function testIsCompleteWithCompleteAuthenticationFlags()
{
$flags = [
SessionFlags::AUTHENTICATION_REQUIRED,
];
$result = SessionFlags::isComplete($flags);
$this->assertTrue($result);
}
/**
* Test that isComplete ignores non-relevant flags while processing.
*/
public function testIsCompleteIgnoringNonRelevantFlags()
{
$flags = [
SessionFlags::RATE_LIMITED,
SessionFlags::AUTHENTICATION_REQUIRED,
];
$result = SessionFlags::isComplete($flags);
$this->assertTrue($result);
}
}

View file

@ -0,0 +1,61 @@
<?php
namespace Socialbox;
use PHPUnit\Framework\TestCase;
use Socialbox\Classes\ServerResolver;
use Socialbox\Enums\Flags\SessionFlags;
class SocialClientTest extends TestCase
{
private const string COFFEE_DOMAIN = 'coffee.com';
private const string TEAPOT_DOMAIN = 'teapot.com';
protected function setUp(): void
{
// Add mocked records for the test domains
ServerResolver::addMock('coffee.com', 'v=socialbox;sb-rpc=http://127.0.0.0:8086/;sb-key=sig:g59Cf8j1wmQmRg1MkveYbpdiZ-1-_hFU9eRRJmQAwmc;sb-exp=0');
ServerResolver::addMock('teapot.com', 'v=socialbox;sb-rpc=http://127.0.0.0:8087/;sb-key=sig:MDXUuripAo_IAv-EZTEoFhpIdhsXxfMLNunSnQzxYiY;sb-exp=0');
}
/**
* Generates a random username based on the given domain.
*
* @param string $domain The domain to be appended to the generated username.
* @return string Returns a randomly generated username in the format 'user<randomString>@<domain>'.
*/
private static function generateUsername(string $domain): string
{
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < 16; $i++)
{
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return 'user' . $randomString . '@' . $domain;
}
public function testConnection() :void
{
$coffeeClient = new SocialClient(self::generateUsername('intvo.id'));
// Check initial session state
$this->assertFalse($coffeeClient->getSessionState()->isAuthenticated());
$this->assertTrue($coffeeClient->getSessionState()->containsFlag(SessionFlags::REGISTRATION_REQUIRED));
$this->assertTrue($coffeeClient->getSessionState()->containsFlag(SessionFlags::SET_PASSWORD));
$this->assertTrue($coffeeClient->getSessionState()->containsFlag(SessionFlags::SET_DISPLAY_NAME));
// Check progressive session state
$this->assertTrue($coffeeClient->settingsSetPassword('coffeePassword'));
$this->assertFalse($coffeeClient->getSessionState()->containsFlag(SessionFlags::SET_PASSWORD));
$this->assertTrue($coffeeClient->settingsSetDisplayName('Coffee User'));
$this->assertFalse($coffeeClient->getSessionState()->containsFlag(SessionFlags::SET_DISPLAY_NAME));
$this->assertFalse($coffeeClient->getSessionState()->containsFlag(SessionFlags::REGISTRATION_REQUIRED));
$this->assertTrue($coffeeClient->getSessionState()->isAuthenticated());
}
}