Skip to content

Commit f24925e

Browse files
Merge pull request #42 from dapr/add/tests
Add more tests and minor bug fixes
2 parents 32c706b + 7ddc668 commit f24925e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+805
-136
lines changed

src/config.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
use Dapr\State\IManageState;
2525
use Dapr\State\StateManager;
2626
use Dapr\State\TransactionalState;
27+
use FastRoute\DataGenerator\GroupCountBased;
28+
use FastRoute\RouteCollector;
29+
use FastRoute\RouteParser\Std;
2730
use Monolog\Handler\ErrorLogHandler;
2831
use Monolog\Logger;
2932
use Monolog\Processor\PsrLogMessageProcessor;
33+
use Nyholm\Psr7\Factory\Psr17Factory;
34+
use Nyholm\Psr7Server\ServerRequestCreator;
3035
use Psr\Log\LoggerInterface;
3136
use Psr\Log\LogLevel;
3237

@@ -78,17 +83,27 @@
7883
get('dapr.deserializers.custom')
7984
),
8085
ExistingOnly::class => autowire(),
81-
FileGenerator::class => autowire(),
86+
FileGenerator::class => autowire(),
8287
IDeserializer::class => autowire(Deserializer::class)->constructorParameter('logger', get('dapr.logger')),
8388
IManageState::class => autowire(StateManager::class)->constructorParameter('logger', get('dapr.logger')),
8489
ISerializer::class => autowire(Serializer::class)->constructorParameter('logger', get('dapr.logger')),
8590
ProxyFactory::class => autowire()->constructorParameter(
8691
'mode',
8792
get('dapr.actors.proxy.generation')
8893
),
94+
Psr17Factory::class => autowire(),
8995
Publish::class => autowire()->constructorParameter('pubsub', get('dapr.pubsub.default')),
96+
RouteCollector::class => autowire()
97+
->constructorParameter('routeParser', create(Std::class))
98+
->constructorParameter('dataGenerator', create(GroupCountBased::class)),
9099
SecretManager::class => autowire()->constructorParameter('logger', get('dapr.logger')),
91100
SerializationConfig::class => autowire()->constructorParameter('serializers', get('dapr.serializers.custom')),
101+
ServerRequestCreator::class => create()->constructor(
102+
get(Psr17Factory::class),
103+
get(Psr17Factory::class),
104+
get(Psr17Factory::class),
105+
get(Psr17Factory::class)
106+
),
92107
Subscriptions::class => autowire()->constructorParameter(
93108
'subscriptions',
94109
get('dapr.subscriptions')

src/lib/Actors/Actor.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
namespace Dapr\Actors;
44

5+
/**
6+
* Class Actor
7+
*
8+
* A base class to simplify user implementations.
9+
*
10+
* @package Dapr\Actors
11+
* @codeCoverageIgnore Not important
12+
*/
513
abstract class Actor implements IActor
614
{
715
use ActorTrait;

src/lib/Actors/ActorConfig.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
/**
1616
* Class ActorConfig
17+
*
18+
* Handles configuration which is called on /dapr/config
19+
*
1720
* @package Dapr\Actors
1821
*/
1922
class ActorConfig implements ISerialize
@@ -46,6 +49,8 @@ public function __construct(
4649
*/
4750
public function get_actor_type_from_dapr_type(string $dapr_type): string|null
4851
{
52+
if($this->actor_name_to_type[$dapr_type] ?? false) return $this->actor_name_to_type[$dapr_type];
53+
4954
$actors = array_combine($this->get_supported_actors(), $this->actor_name_to_type);
5055

5156
return $actors[$dapr_type] ?? null;
@@ -54,6 +59,7 @@ public function get_actor_type_from_dapr_type(string $dapr_type): string|null
5459
/**
5560
* @return array An array of dapr types
5661
* @throws ReflectionException | LogicException
62+
* @codeCoverageIgnore
5763
*/
5864
public function get_supported_actors(): array
5965
{

src/lib/Actors/ActorProxy.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
/**
1515
* Class ActorProxy
16+
*
17+
* Used by the DynamicGenerator proxy
18+
*
19+
* @codeCoverageIgnore
1620
* @package Dapr
1721
*/
1822
class ActorProxy

src/lib/Actors/ActorRuntime.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
use ReflectionNamedType;
1717

1818
/**
19-
* The Actor Runtime
19+
* Class ActorRuntime
20+
*
21+
* Handles activating, method calls, and deactivating actors.
22+
*
23+
* @package Dapr\Actors
2024
*/
2125
class ActorRuntime
2226
{
@@ -57,8 +61,10 @@ public function do_method(IActor $actor, string $method, mixed $arg): mixed
5761
);
5862
}
5963

60-
public function deactivate_actor(IActor $actor, string $dapr_type, string $id): void
64+
public function deactivate_actor(IActor $actor, string $dapr_type): void
6165
{
66+
$id = $actor->get_id();
67+
6268
$activation_tracker = hash('sha256', $dapr_type.$id);
6369
$activation_tracker = rtrim(
6470
sys_get_temp_dir(),
@@ -91,16 +97,20 @@ public function resolve_actor(string $dapr_type, string $id, callable $loan): mi
9197
$this->validate_actor($reflection);
9298
$states = $this->get_states($reflection, $dapr_type, $id);
9399
$actor = $this->get_actor($reflection, $dapr_type, $id, $states);
100+
// @codeCoverageIgnoreStart
94101
} catch (Exception $exception) {
95102
throw new NotFound('Actor could not be located', previous: $exception);
96103
}
104+
// @codeCoverageIgnoreEnd
97105
$result = $loan($actor);
98106

99107
try {
100108
$this->commit($states);
109+
// @codeCoverageIgnoreStart
101110
} catch (DependencyException | DaprException | NotFoundException $e) {
102111
throw new SaveStateFailure('Failed to commit actor state', previous: $e);
103112
}
113+
// @codeCoverageIgnoreEnd
104114

105115
return $result;
106116
}
@@ -118,8 +128,10 @@ protected function locate_actor(string $dapr_type): ReflectionClass
118128
{
119129
$type = $this->actor_config->get_actor_type_from_dapr_type($dapr_type);
120130
if ( ! class_exists($type)) {
131+
// @codeCoverageIgnoreStart
121132
$this->logger->critical('Unable to locate an actor for {t}', ['t' => $type]);
122133
throw new NotFound();
134+
// @codeCoverageIgnoreEnd
123135
}
124136

125137
return new ReflectionClass($type);
@@ -138,8 +150,10 @@ protected function validate_actor(ReflectionClass $reflection): bool
138150
return true;
139151
}
140152

153+
// @codeCoverageIgnoreStart
141154
$this->logger->critical('Actor does not implement the IActor interface');
142155
throw new NotFound();
156+
// @codeCoverageIgnoreEnd
143157
}
144158

145159
/**

src/lib/Actors/ActorState.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
use ReflectionException;
1818
use ReflectionProperty;
1919

20+
/**
21+
* Class ActorState
22+
*
23+
* Handles actor state transactions.
24+
*
25+
* @package Dapr\Actors
26+
*/
2027
abstract class ActorState
2128
{
2229
/**

src/lib/Actors/Attributes/DaprType.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
use Attribute;
66

7+
/**
8+
* Class DaprType
9+
*
10+
* Annotates a class as an actor that implements a specific Dapr Type.
11+
*
12+
* @package Dapr\Actors\Attributes
13+
*/
714
#[Attribute(Attribute::TARGET_CLASS)]
815
class DaprType
916
{

src/lib/Actors/Generators/CachedGenerator.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,34 @@
66
use JetBrains\PhpStorm\Pure;
77
use Psr\Container\ContainerInterface;
88

9+
/**
10+
* Class CachedGenerator
11+
*
12+
* Caches the generated file.
13+
*
14+
* @package Dapr\Actors\Generators
15+
*/
916
class CachedGenerator extends ExistingOnly
1017
{
11-
#[Pure] public function __construct(
18+
protected string $cache_dir;
19+
20+
public function __construct(
1221
string $interface,
1322
string $dapr_type,
1423
FactoryInterface $factory,
1524
ContainerInterface $container
1625
) {
1726
parent::__construct($interface, $dapr_type, $factory, $container);
27+
$this->cache_dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'dapr-proxy-cache'.DIRECTORY_SEPARATOR;
28+
}
29+
30+
/**
31+
* Set the cache directory
32+
*
33+
* @param string $dir
34+
*/
35+
public function set_cache_dir(string $dir) {
36+
$this->cache_dir = $dir;
1837
}
1938

2039
public function get_proxy(string $id): object
@@ -25,11 +44,10 @@ public function get_proxy(string $id): object
2544
['interface' => $this->interface, 'dapr_type' => $this->dapr_type]
2645
);
2746
$file = $file_generator->generate_file();
28-
$dir = sys_get_temp_dir().DIRECTORY_SEPARATOR.'dapr-proxy-cache'.DIRECTORY_SEPARATOR;
29-
if ( ! is_dir($dir)) {
30-
mkdir($dir);
47+
if ( ! is_dir($this->cache_dir)) {
48+
mkdir($this->cache_dir);
3149
}
32-
$filename = $dir.$this->get_short_class_name();
50+
$filename = $this->cache_dir.$this->get_short_class_name();
3351
file_put_contents($filename, $file);
3452
require_once $filename;
3553
}

src/lib/Actors/Generators/DynamicGenerator.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@
1313
use Nette\PhpGenerator\Method;
1414
use Psr\Container\ContainerInterface;
1515

16+
/**
17+
* Class DynamicGenerator
18+
*
19+
* Uses some quirks of PHP magic functions to provide a proxy
20+
*
21+
* @package Dapr\Actors\Generators
22+
*/
1623
class DynamicGenerator extends GenerateProxy
1724
{
1825

src/lib/Actors/Generators/ExistingOnly.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
use Nette\PhpGenerator\Method;
1212
use Psr\Container\ContainerInterface;
1313

14+
/**
15+
* Class ExistingOnly
16+
*
17+
* Only allows existing proxies to be used. Does not generate a proxy.
18+
*
19+
* @package Dapr\Actors\Generators
20+
*/
1421
class ExistingOnly extends GenerateProxy
1522
{
1623
#[Pure] public function __construct(
@@ -37,16 +44,30 @@ public function get_proxy(string $id)
3744
return $proxy;
3845
}
3946

47+
/**
48+
* @codeCoverageIgnore Never happens
49+
* @param Method $method
50+
*/
4051
protected function generate_failure_method(Method $method): void
4152
{
4253
throw new LogicException();
4354
}
4455

56+
/**
57+
* @codeCoverageIgnore Never happens
58+
* @param Method $method
59+
* @param string $id
60+
*/
4561
protected function generate_proxy_method(Method $method, string $id): void
4662
{
4763
throw new LogicException();
4864
}
4965

66+
/**
67+
* @codeCoverageIgnore Never happens
68+
* @param Method $method
69+
* @param string $id
70+
*/
5071
protected function generate_get_id(Method $method, string $id): void
5172
{
5273
throw new LogicException();

0 commit comments

Comments
 (0)