Skip to content

Commit

Permalink
Merge pull request #225 from clue-labs/php8-attribute
Browse files Browse the repository at this point in the history
Improve test suite and add dummy `#[PHP8]` attribute
  • Loading branch information
SimonFrings authored May 24, 2023
2 parents eecba13 + 9fbb308 commit 2f62d05
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 145 deletions.
162 changes: 17 additions & 145 deletions tests/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ public function __invoke(): ResponseInterface
}
};

$fn = null;
$fn = $data = null;
$fn = #[PHP8] fn(mixed $data = 42) => new Response(200, [], (string) json_encode($data)); // @phpstan-ignore-line
$container = new Container([
ResponseInterface::class => $fn,
Expand Down Expand Up @@ -1269,17 +1269,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesUnknow
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1301,17 +1293,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesRecurs
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1333,17 +1317,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesString
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class('') {
/** @var string */
private $data;

public function __construct(string $stdClass)
{
$this->data = $stdClass;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert(is_string($stdClass));
}
};

Expand All @@ -1367,17 +1343,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesVariab
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1402,17 +1370,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesObject
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1435,17 +1395,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesString
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1468,17 +1420,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesIntVar
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1501,17 +1445,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesFloatV
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1534,17 +1470,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesBoolVa
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1567,17 +1495,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1597,17 +1517,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesNullab
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var ?\stdClass */
private $data;

public function __construct(?\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1627,17 +1539,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1657,17 +1561,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassN
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1687,17 +1583,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesNullab
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var ?\stdClass */
private $data;

public function __construct(?\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1717,17 +1605,9 @@ public function testCallableReturnsCallableThatThrowsWhenFactoryReferencesClassM
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class(new \stdClass()) {
/** @var \stdClass */
private $data;

public function __construct(\stdClass $data)
{
$this->data = $data;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert($data instanceof \stdClass);
}
};

Expand All @@ -1747,17 +1627,9 @@ public function testCallableReturnsCallableThatThrowsWhenConstructorWithoutFacto
$request = new ServerRequest('GET', 'http://example.com/');

$controller = new class('Alice') {
/** @var string */
private $data;

public function __construct(string $name)
{
$this->data = $name;
}

public function __invoke(ServerRequestInterface $request): Response
{
return new Response(200, [], (string) json_encode($this->data));
assert(is_string($name));
}
};

Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/InvalidConstructorIntersection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace FrameworkX\Tests\Fixtures;

use FrameworkX\Tests\PHP8;

/** PHP 8.1+ **/
class InvalidConstructorIntersection
{
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixtures/InvalidConstructorUnion.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace FrameworkX\Tests\Fixtures;

use FrameworkX\Tests\PHP8;

/** PHP 8.0+ **/
class InvalidConstructorUnion
{
Expand Down
13 changes: 13 additions & 0 deletions tests/PHP8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace FrameworkX\Tests;

/** Dummy attribute used to comment out code for PHP < 8 to ensure compatibility across test matrix */
#[\Attribute]
class PHP8
{
public function __construct()
{
assert(\PHP_VERSION_ID >= 80000);
}
}

0 comments on commit 2f62d05

Please sign in to comment.