Skip to content

Commit 6cf10ce

Browse files
committed
Fix tests
1 parent 0bd708b commit 6cf10ce

13 files changed

+199
-74
lines changed

docker-compose.examples.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ version: '2'
33
services:
44
zen-examples-php:
55
container_name: zen-examples-php
6-
image: php:8.0.0beta1-cli-alpine3.12
6+
image: php:8.0.0alpha3-cli-alpine3.12
77
volumes:
88
- .:/var/www
99
working_dir: /var/www

phpunit.xml.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit
33
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4-
bootstrap="./vendor/autoload.php" colors="true">
4+
bootstrap="./vendor/autoload.php" colors="true" convertDeprecationsToExceptions="false">
55
<coverage processUncoveredFiles="true">
66
<include>
77
<directory suffix=".php">src</directory>

tests/AbstractCompiledContainerTest.php

+21-11
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Psr\Container\ContainerInterface;
9+
use stdClass;
910
use WoohooLabs\Zen\Exception\NotFoundException;
10-
use WoohooLabs\Zen\Tests\Double\StubContainer;
11+
use WoohooLabs\Zen\Tests\Double\StubPrototypeContainer;
1112
use WoohooLabs\Zen\Tests\Double\StubContainerEntry;
13+
use WoohooLabs\Zen\Tests\Double\StubSingletonContainer;
1214
use WoohooLabs\Zen\Tests\Fixture\Container\ContainerWithInjectedProperty;
1315

1416
class AbstractCompiledContainerTest extends TestCase
@@ -18,7 +20,7 @@ class AbstractCompiledContainerTest extends TestCase
1820
*/
1921
public function hasReturnsFalse(): void
2022
{
21-
$container = $this->createStubContainer();
23+
$container = $this->createStubSingletonContainer();
2224

2325
$hasEntry = $container->has("TestContainerEntry");
2426

@@ -30,7 +32,7 @@ public function hasReturnsFalse(): void
3032
*/
3133
public function hasReturnsTrue(): void
3234
{
33-
$container = $this->createStubContainer();
35+
$container = $this->createStubSingletonContainer();
3436

3537
$hasEntry = $container->has(StubContainerEntry::class);
3638

@@ -42,7 +44,7 @@ public function hasReturnsTrue(): void
4244
*/
4345
public function getThrowsNotFoundException(): void
4446
{
45-
$container = $this->createStubContainer();
47+
$container = $this->createStubSingletonContainer();
4648

4749
$this->expectException(NotFoundException::class);
4850

@@ -54,7 +56,7 @@ public function getThrowsNotFoundException(): void
5456
*/
5557
public function getReturnsPrototypeEntry(): void
5658
{
57-
$container = $this->createStubContainer();
59+
$container = $this->createStubPrototypeContainer();
5860

5961
$entry1 = $container->get(StubContainerEntry::class);
6062
$entry2 = $container->get(StubContainerEntry::class);
@@ -69,11 +71,14 @@ public function getReturnsPrototypeEntry(): void
6971
*/
7072
public function getReturnsSingletonEntry(): void
7173
{
72-
$container = $this->createStubContainer(true);
74+
$container = $this->createStubSingletonContainer();
7375

74-
$entry = $container->get(StubContainerEntry::class);
76+
$entry1 = $container->get(StubContainerEntry::class);
77+
$entry2 = $container->get(StubContainerEntry::class);
7578

76-
$this->assertSame($container->get(StubContainerEntry::class), $entry);
79+
$this->assertInstanceOf(StubContainerEntry::class, $entry1);
80+
$this->assertInstanceOf(StubContainerEntry::class, $entry2);
81+
$this->assertSame($entry1, $entry2);
7782
}
7883

7984
/**
@@ -85,11 +90,16 @@ public function setProperty(): void
8590

8691
$property = $container->getProperty();
8792

88-
$this->assertTrue($property);
93+
$this->assertInstanceOf(stdClass::class, $property);
94+
}
95+
96+
private function createStubSingletonContainer(): ContainerInterface
97+
{
98+
return new StubSingletonContainer();
8999
}
90100

91-
private function createStubContainer(bool $isSingleton = false): ContainerInterface
101+
private function createStubPrototypeContainer(): ContainerInterface
92102
{
93-
return new StubContainer($isSingleton);
103+
return new StubPrototypeContainer();
94104
}
95105
}

tests/Container/ContainerCompilerTest.php

+43-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function compileContainerWithEntryPoint(): void
9696
/**
9797
* @test
9898
*/
99-
public function compileContainerWithFileBasedEntryPoint(): void
99+
public function compileContainerWithFileBasedEntryPointWhenInlinable(): void
100100
{
101101
$compiler = new ContainerCompiler();
102102

@@ -110,19 +110,58 @@ public function compileContainerWithFileBasedEntryPoint(): void
110110
),
111111
],
112112
"WoohooLabs\\Zen\\Tests\\Fixture\\Container",
113-
"ContainerWithFileBasedEntryPoint",
113+
"ContainerWithFileBasedEntryPointWhenInlinable",
114114
true,
115115
true,
116116
true
117117
),
118118
[
119-
StubSingletonDefinition::class => new StubSingletonDefinition(true, true),
119+
StubSingletonDefinition::class => new StubSingletonDefinition(true, true, 0, 0, true),
120120
],
121121
[]
122122
);
123123

124124
$this->assertEquals(
125-
$this->getCompiledContainerSourceCode("ContainerWithFileBasedEntryPoint.php"),
125+
$this->getCompiledContainerSourceCode("ContainerWithFileBasedEntryPointWhenInlinable.php"),
126+
$container["container"]
127+
);
128+
129+
$this->assertEquals(
130+
$this->getCompiledContainerSourceCode("ContainerWithFileBasedEntryPoint-Definition.php"),
131+
$container["definitions"]["WoohooLabs__Zen__Tests__Double__StubSingletonDefinition.php"]
132+
);
133+
}
134+
135+
/**
136+
* @test
137+
*/
138+
public function compileContainerWithFileBasedEntryPointWhenNotInlinable(): void
139+
{
140+
$compiler = new ContainerCompiler();
141+
142+
$container = $compiler->compile(
143+
new StubCompilerConfig(
144+
[
145+
new StubContainerConfig(
146+
[
147+
StubSingletonDefinition::class,
148+
]
149+
),
150+
],
151+
"WoohooLabs\\Zen\\Tests\\Fixture\\Container",
152+
"ContainerWithFileBasedEntryPointWhenNotInlinable",
153+
true,
154+
true,
155+
true
156+
),
157+
[
158+
StubSingletonDefinition::class => new StubSingletonDefinition(true, true, 0, 0, false),
159+
],
160+
[]
161+
);
162+
163+
$this->assertEquals(
164+
$this->getCompiledContainerSourceCode("ContainerWithFileBasedEntryPointWhenNotInlinable.php"),
126165
$container["container"]
127166
);
128167

tests/Double/StubContainer.php

-47
This file was deleted.

tests/Double/StubContainerEntry.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
namespace WoohooLabs\Zen\Tests\Double;
66

7+
use stdClass;
8+
79
class StubContainerEntry
810
{
9-
private bool $a = false;
10-
11-
public function getA(): bool
11+
public function getA(): stdClass
1212
{
13-
return $this->a;
13+
return new stdClass();
1414
}
1515
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace WoohooLabs\Zen\Tests\Double;
5+
6+
use WoohooLabs\Zen\AbstractCompiledContainer;
7+
use WoohooLabs\Zen\Exception\NotFoundException;
8+
9+
class StubPrototypeContainer extends AbstractCompiledContainer
10+
{
11+
/**
12+
* @param string $id
13+
*/
14+
public function has($id): bool
15+
{
16+
return match ($id) {
17+
'WoohooLabs\Zen\Tests\Double\StubContainerEntry' => true,
18+
default => false,
19+
};
20+
}
21+
22+
/**
23+
* @param string $id
24+
* @throws NotFoundException
25+
*/
26+
public function get($id): mixed
27+
{
28+
return $this->singletonEntries[$id] ?? match ($id) {
29+
'WoohooLabs\Zen\Tests\Double\StubContainerEntry' => new \WoohooLabs\Zen\Tests\Double\StubContainerEntry(),
30+
default => throw new NotFoundException($id),
31+
};
32+
}
33+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace WoohooLabs\Zen\Tests\Double;
5+
6+
use WoohooLabs\Zen\AbstractCompiledContainer;
7+
use WoohooLabs\Zen\Exception\NotFoundException;
8+
9+
class StubSingletonContainer extends AbstractCompiledContainer
10+
{
11+
/**
12+
* @param string $id
13+
*/
14+
public function has($id): bool
15+
{
16+
return match ($id) {
17+
'WoohooLabs\Zen\Tests\Double\StubContainerEntry' => true,
18+
default => false,
19+
};
20+
}
21+
22+
/**
23+
* @param string $id
24+
* @throws NotFoundException
25+
*/
26+
public function get($id): mixed
27+
{
28+
return $this->singletonEntries[$id] ?? match ($id) {
29+
'WoohooLabs\Zen\Tests\Double\StubContainerEntry' => $this->singletonEntries['WoohooLabs\Zen\Tests\Double\StubContainerEntry'] = new \WoohooLabs\Zen\Tests\Double\StubContainerEntry(),
30+
default => throw new NotFoundException($id),
31+
};
32+
}
33+
}

tests/Double/StubSingletonDefinition.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,22 @@
88

99
class StubSingletonDefinition extends TestDefinition
1010
{
11-
public function __construct(bool $isEntryPoint = false, bool $isFileBased = false, int $referenceCount = 0)
12-
{
13-
parent::__construct(self::class, true, $isEntryPoint, $isFileBased, $referenceCount);
11+
public function __construct(
12+
bool $isEntryPoint = false,
13+
bool $isFileBased = false,
14+
int $singletonReferenceCount = 0,
15+
int $prototypeReferenceCount = 0,
16+
bool $isDefinitionInlinable = false
17+
) {
18+
parent::__construct(
19+
self::class,
20+
true,
21+
$isEntryPoint,
22+
$isFileBased,
23+
$singletonReferenceCount,
24+
$prototypeReferenceCount,
25+
$isDefinitionInlinable
26+
);
1427
}
1528

1629
/**

tests/Double/TestDefinition.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@
1111

1212
class TestDefinition extends AbstractDefinition
1313
{
14+
private bool $definitionInlinable;
15+
1416
public function __construct(
1517
string $id,
1618
bool $isSingleton = true,
1719
bool $isEntryPoint = false,
1820
bool $isFileBased = false,
1921
int $singletonReferenceCount = 0,
20-
int $prototypeReferenceCount = 0
22+
int $prototypeReferenceCount = 0,
23+
bool $isDefinitionInlinable = false
2124
) {
2225
parent::__construct($id, $isSingleton, $isEntryPoint, $isFileBased, $singletonReferenceCount, $prototypeReferenceCount);
26+
$this->definitionInlinable = $isDefinitionInlinable;
2327
}
2428

2529
public function needsDependencyResolution(): bool
@@ -61,4 +65,9 @@ public function compile(
6165
): string {
6266
return "";
6367
}
68+
69+
public function isDefinitionInlinable(string $parentId = ""): bool
70+
{
71+
return $this->definitionInlinable;
72+
}
6473
}

0 commit comments

Comments
 (0)