Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ebln committed Jun 9, 2024
1 parent 2bcbb43 commit 7b45d98
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 131 deletions.
1 change: 1 addition & 0 deletions .github/workflows/buildTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ jobs:
./vendor/bin/psalm --monochrome --no-progress --stats --show-info=false
- name: phpstan
run: |
./vendor/bin/phpstan --version
./vendor/bin/phpstan --no-interaction --no-ansi analyse
- name: Mess Detector
run: |
Expand Down
122 changes: 61 additions & 61 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,69 +50,69 @@
'fully_qualified_strict_types' => false,
'single_space_around_construct' => [
'constructs_followed_by_a_single_space' => [ // default, minus const
'abstract',
'as',
'attribute',
'break',
'case',
'catch',
'class',
'clone',
'comment',
'const_import',
'continue',
'do',
'echo',
'else',
'elseif',
'enum',
'extends',
'final',
'finally',
'for',
'foreach',
'function',
'function_import',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'match',
'named_argument',
'namespace',
'new',
'open_tag_with_echo',
'php_doc',
'php_open',
'print',
'private',
'protected',
'public',
'readonly',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'type_colon',
'use',
'use_lambda',
'use_trait',
'var',
'while',
'yield',
'yield_from',
'abstract',
'as',
'attribute',
'break',
'case',
'catch',
'class',
'clone',
'comment',
'const_import',
'continue',
'do',
'echo',
'else',
'elseif',
'enum',
'extends',
'final',
'finally',
'for',
'foreach',
'function',
'function_import',
'global',
'goto',
'if',
'implements',
'include',
'include_once',
'instanceof',
'insteadof',
'interface',
'match',
'named_argument',
'namespace',
'new',
'open_tag_with_echo',
'php_doc',
'php_open',
'print',
'private',
'protected',
'public',
'readonly',
'require',
'require_once',
'return',
'static',
'switch',
'throw',
'trait',
'try',
'type_colon',
'use',
'use_lambda',
'use_trait',
'var',
'while',
'yield',
'yield_from',
],

],
'void_return' => true,
]
)
->setFinder($finder)
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ includes:

parameters:
level: max
tmpDir: .provision/cache
paths:
- src
- tests
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
findUnusedVariablesAndParams="false"
findUnusedCode="false"
findUnusedPsalmSuppress="true"
findUnusedBaselineEntry="true"
errorBaseline="psalm-baseline.xml"
>
<projectFiles>
Expand Down
19 changes: 6 additions & 13 deletions tests/Factory/NowFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,21 @@
*/
final class NowFactoryTest extends TestCase
{
private ?NowFactory $nowFactory = null;

protected function setUp(): void
{
$this->nowFactory = new NowFactory();
}

public function testNowReturnsDateTimeImmutable()
public function testNowReturnsDateTimeImmutable(): void
{
$result = $this->nowFactory->now();
$result = (new NowFactory())->now();
self::assertInstanceOf(\DateTimeImmutable::class, $result);
}

public function testNowReturnsUtcTimezone()
public function testNowReturnsUtcTimezone(): void
{
$result = $this->nowFactory->now();
$result = (new NowFactory())->now();
self::assertSame('UTC', $result->getTimezone()->getName());
}

public function testNowReturnsCurrentTime()
public function testNowReturnsCurrentTime(): void
{
$result = $this->nowFactory->now();
$result = (new NowFactory())->now();
$currentUtcTime = new \DateTimeImmutable('now', new \DateTimeZone('UTC'));

// Allow a small difference to account for execution time
Expand Down
26 changes: 6 additions & 20 deletions tests/Transcriptor/Response/CookieDispatch/AbstractCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,17 @@
*/
final class AbstractCookieTest extends TestCase
{
private ?CookieInterface $cookie = null;

protected function setUp(): void
{
$options = [
'expires' => time() + 3600,
'path' => '/',
'domain' => 'example.com',
'secure' => true,
'httponly' => true,
'samesite' => 'Strict',
];
$this->cookie = $this->createCookie('test', 'value', $options);
}

public function testGetName()
public function testGetName(): void
{
self::assertSame('test', $this->cookie->getName());
self::assertSame('test', $this->createCookie('test', 'value', [])->getName());
}

public function testGetValue()
public function testGetValue(): void
{
self::assertSame('value', $this->cookie->getValue());
self::assertSame('value', $this->createCookie('test', 'value', [])->getValue());
}

public function testConstructorAssignsOptions()
public function testConstructorAssignsOptions(): void
{
$options = [
'expires' => time() + 3600,
Expand All @@ -59,6 +44,7 @@ public function testConstructorAssignsOptions()
self::assertSame($options, $property->getValue($cookie));
}

/** @param array{expires?: int, path?: string, domain?: string, secure?: bool, httponly?: bool, samesite?: 'Lax'|'None'|'Strict'} $options */
private function createCookie(string $name, string $value, array $options): CookieInterface
{
return new class($name, $value, $options) extends AbstractCookie {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
final class CookieContainerTest extends TestCase
{
public function testGetCookies()
public function testGetCookies(): void
{
$cookieMock = $this->createMock(CookieInterface::class);
$cookies = [$cookieMock];
Expand All @@ -24,7 +24,7 @@ public function testGetCookies()
self::assertSame($cookies, $container->getCookies());
}

public function testConstructorAssignsCookies()
public function testConstructorAssignsCookies(): void
{
$cookieMock1 = $this->createMock(CookieInterface::class);
$cookieMock2 = $this->createMock(CookieInterface::class);
Expand Down
19 changes: 7 additions & 12 deletions tests/Transcriptor/Response/CookieDispatch/HeaderCookieTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,19 @@
*/
final class HeaderCookieTest extends TestCase
{
private ?HeaderCookie $headerCookie = null;

protected function setUp(): void
{
$this->headerCookie = new HeaderCookie('test', 'value', 'Set-Cookie: test=value; Path=/; HttpOnly');
}

public function testGetName()
public function testGetName(): void
{
self::assertSame('test', $this->headerCookie->getName());
$headerCookie = new HeaderCookie('test', 'foo', 'Set-Cookie: test=foo; Path=/; HttpOnly');
self::assertSame('test', $headerCookie->getName());
}

public function testGetValue()
public function testGetValue(): void
{
self::assertSame('value', $this->headerCookie->getValue());
$headerCookie = new HeaderCookie('test', 'bar', 'Set-Cookie: test=bar; Path=/; HttpOnly');
self::assertSame('bar', $headerCookie->getValue());
}

public function testConstructorAssignsProperties()
public function testConstructorAssignsProperties(): void
{
$headerCookie = new HeaderCookie('name', 'value', 'Set-Cookie: name=value; Path=/; HttpOnly');

Expand Down
34 changes: 15 additions & 19 deletions tests/xtra_mocks/EventDispatcherMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,23 @@
*/
final class EventDispatcherMockTest extends TestCase
{
private $dispatcher;

protected function setUp(): void
{
$this->dispatcher = new \sfEventDispatcher();
}

/** @covers sfEventDispatcher::connect */
public function testConnect()
public function testConnect(): void
{
$counter = 0;
$listener = static function ($event) use (&$counter): void {
++$counter;
};
self::assertCount(0, $this->dispatcher->getListeners('test.event'));
$this->dispatcher->connect('test.event', $listener);
self::assertCount(1, $this->dispatcher->getListeners('test.event'));
$this->dispatcher->notify(new \sfEvent($this, 'test.event'));
$this->dispatcher->filter(new \sfEvent($this, 'test.event'), null);
$dispatcher = new \sfEventDispatcher();
self::assertCount(0, $dispatcher->getListeners('test.event'));
$dispatcher->connect('test.event', $listener);
self::assertCount(1, $dispatcher->getListeners('test.event'));
$dispatcher->notify(new \sfEvent($this, 'test.event'));
$dispatcher->filter(new \sfEvent($this, 'test.event'), null);
self::assertSame(2, $counter);
}

public function testNotify()
public function testNotify(): void
{
$event = new \sfEvent($this, 'test.event', ['log' => 'foobar']);
$semaphore = (object)['logger' => []];
Expand All @@ -46,21 +40,23 @@ public function testNotify()

return 'handled';
};
$this->dispatcher->connect('test.event', $listener);
$resultEvent = $this->dispatcher->notify($event);
$dispatcher = new \sfEventDispatcher();
$dispatcher->connect('test.event', $listener);
$resultEvent = $dispatcher->notify($event);
self::assertSame($resultEvent, $event);
self::assertSame(['foobar'], $semaphore->logger);
}

public function testFilter()
public function testFilter(): void
{
$event = new \sfEvent($this, 'test.event');
$initialValue = 10;
$listener = static function ($event, $value) {
return $value * 2;
};
$this->dispatcher->connect('test.event', $listener);
$filteredEvent = $this->dispatcher->filter($event, $initialValue);
$dispatcher = new \sfEventDispatcher();
$dispatcher->connect('test.event', $listener);
$filteredEvent = $dispatcher->filter($event, $initialValue);
self::assertSame(20, $filteredEvent->getReturnValue());
}
}
8 changes: 4 additions & 4 deletions tests/xtra_mocks/EventMockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,29 @@
*/
final class EventMockTest extends TestCase
{
public function testEventProperties()
public function testEventProperties(): void
{
$event = new \sfEvent($this, 'test.event', ['key' => 'value']);
self::assertSame('test.event', $event->getName());
self::assertSame($this, $event->getSubject());
self::assertSame('value', $event->offsetGet('key'));
}

public function testSetAndGetReturnValue()
public function testSetAndGetReturnValue(): void
{
$event = new \sfEvent($this, 'test.event');
$event->setReturnValue('test');
self::assertSame('test', $event->getReturnValue());
}

public function testOffsetSetAndGet()
public function testOffsetSetAndGet(): void
{
$event = new \sfEvent($this, 'test.event', []);
$event->offsetSet('newkey', 'newvalue');
self::assertSame('newvalue', $event->offsetGet('newkey'));
}

public function testExceptionForInvalidKey()
public function testExceptionForInvalidKey(): void
{
$this->expectException(\InvalidArgumentException::class);
$event = new \sfEvent($this, 'test.event', []);
Expand Down

0 comments on commit 7b45d98

Please sign in to comment.