Skip to content

Commit 86e9b6a

Browse files
authored
Merge pull request #2 from cycle/feature/properties
Feature/properties
2 parents 1a57693 + 284a54b commit 86e9b6a

19 files changed

+369
-433
lines changed

tests/Promise/Fixtures/EntityWithConstants.php

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/Promise/Fixtures/EntityWithTrait.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/Promise/Fixtures/EntityWithoutConstants.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/Promise/ProxyPrinter/BaseProxyPrinterTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
abstract class BaseProxyPrinterTest extends BaseTest
2020
{
21+
protected const NS = 'Cycle\ORM\Promise\Tests\Promises';
2122
public const DRIVER = 'sqlite';
2223

2324
/** @var \Spiral\Core\Container */

tests/Promise/ProxyPrinter/ConstantsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class ConstantsTest extends BaseProxyPrinterTest
1313
public function testWithoutConflicts(): void
1414
{
1515
$classname = Fixtures\EntityWithoutConstConflicts::class;
16-
$as = 'Cycle\ORM\Promise\Tests\Promises\EntityProxy' .__CLASS__ . __LINE__;
16+
$as = self::NS .__CLASS__ . __LINE__;
1717
$reflection = new \ReflectionClass($classname);
1818

1919
$parent = Declarations::createParentFromReflection($reflection);
@@ -43,7 +43,7 @@ public function testWithoutConflicts(): void
4343
public function testWithConflicts(): void
4444
{
4545
$classname = Fixtures\EntityWithConstConflicts::class;
46-
$as = 'Cycle\ORM\Promise\Tests\Promises\EntityProxy' . __CLASS__ .__LINE__;
46+
$as = self::NS . __CLASS__ .__LINE__;
4747
$reflection = new \ReflectionClass($classname);
4848

4949
$parent = Declarations::createParentFromReflection($reflection);
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cycle\ORM\Promise\Tests\ProxyPrinter;
5+
6+
use Cycle\ORM\Promise\Declaration\Declarations;
7+
8+
class ConstructorTest extends BaseProxyPrinterTest
9+
{
10+
/**
11+
* @dataProvider constructorDataProvider
12+
*
13+
* @param string $classname
14+
* @param string $as
15+
*
16+
* @throws \ReflectionException
17+
*/
18+
public function testHasConstructor(string $classname, string $as): void
19+
{
20+
$reflection = new \ReflectionClass($classname);
21+
$parent = Declarations::createParentFromReflection($reflection);
22+
$class = Declarations::createClassFromName($as, $parent);
23+
$output = $this->make($reflection, $class, $parent);
24+
$output = ltrim($output, '<?php');
25+
26+
$this->assertFalse(class_exists($class->getFullName()));
27+
28+
eval($output);
29+
30+
$reflection = new \ReflectionClass($class->getFullName());
31+
$constructor = $reflection->getConstructor();
32+
$this->assertNotNull($constructor);
33+
}
34+
35+
public function constructorDataProvider(): array
36+
{
37+
return [
38+
[Fixtures\EntityWithoutConstructor::class, 'EntityProxy' . __CLASS__ . __LINE__],
39+
[Fixtures\EntityWithConstructor::class, 'EntityProxy' . __CLASS__ . __LINE__],
40+
];
41+
}
42+
43+
/**
44+
* @throws \ReflectionException
45+
*/
46+
public function testNotContainParentConstructor(): void
47+
{
48+
$class = Fixtures\EntityWithoutConstructor::class;
49+
$as = 'EntityProxy' . __CLASS__ . __LINE__;
50+
51+
$r = new \ReflectionClass($class);
52+
$parent = Declarations::createParentFromReflection($r);
53+
$class = Declarations::createClassFromName($as, $parent);
54+
$output = $this->make($r, $class, $parent);
55+
$output = ltrim($output, '<?php');
56+
57+
$this->assertFalse(class_exists($class->getFullName()));
58+
59+
eval($output);
60+
61+
$this->assertStringNotContainsString('parent::__construct();', $output);
62+
}
63+
64+
/**
65+
* @throws \ReflectionException
66+
*/
67+
public function testContainParentConstructor(): void
68+
{
69+
$class = Fixtures\EntityWithConstructor::class;
70+
$as = 'EntityProxy' . __CLASS__ . __LINE__;
71+
72+
$reflection = new \ReflectionClass($class);
73+
$parent = Declarations::createParentFromReflection($reflection);
74+
$class = Declarations::createClassFromName($as, $parent);
75+
$output = $this->make($reflection, $class, $parent);
76+
$output = ltrim($output, '<?php');
77+
78+
$this->assertFalse(class_exists($class->getFullName()));
79+
80+
eval($output);
81+
82+
$this->assertStringContainsString('parent::__construct();', $output);
83+
}
84+
85+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cycle\ORM\Promise\Tests\ProxyPrinter;
5+
6+
use Cycle\ORM\Promise\Declaration\Declarations;
7+
use Cycle\ORM\Promise\PromiseInterface;
8+
use Cycle\ORM\Promise\Utils;
9+
10+
class DeclarationTest extends BaseProxyPrinterTest
11+
{
12+
/**
13+
* @throws \ReflectionException
14+
*/
15+
public function testDeclaration(): void
16+
{
17+
$classname = Fixtures\Entity::class;
18+
$as = 'EntityProxy' . __LINE__;
19+
20+
$r = new \ReflectionClass($classname);
21+
$parent = Declarations::createParentFromReflection($r);
22+
$class = Declarations::createClassFromName($as, $parent);
23+
$output = $this->make($r, $class, $parent);
24+
$output = ltrim($output, '<?php');
25+
26+
$this->assertFalse(class_exists($class->getFullName()));
27+
28+
eval($output);
29+
30+
$this->assertStringNotContainsString('abstract', $output);
31+
$this->assertStringContainsString(sprintf(
32+
'class %s extends %s implements %s',
33+
$as,
34+
Utils::shortName($classname),
35+
Utils::shortName(PromiseInterface::class)
36+
), $output);
37+
38+
$proxy = $this->makeProxyObject($classname, $class->getFullName());
39+
40+
$this->assertInstanceOf($class->getFullName(), $proxy);
41+
$this->assertInstanceOf($classname, $proxy);
42+
$this->assertInstanceOf(PromiseInterface::class, $proxy);
43+
}
44+
45+
/**
46+
* @dataProvider traitsDataProvider
47+
*
48+
* @param string $classname
49+
* @param string $as
50+
*
51+
* @throws \ReflectionException
52+
*/
53+
public function testTraits(string $classname, string $as): void
54+
{
55+
$r = new \ReflectionClass($classname);
56+
$parent = Declarations::createParentFromReflection($r);
57+
$class = Declarations::createClassFromName($as, $parent);
58+
$this->assertStringNotContainsString(' use ', $this->make($r, $class, $parent));
59+
}
60+
61+
public function traitsDataProvider(): array
62+
{
63+
return [
64+
[Fixtures\EntityWithoutTrait::class, 'EntityProxy' . __LINE__],
65+
[Fixtures\EntityWithTrait::class, 'EntityProxy' . __LINE__],
66+
];
67+
}
68+
69+
/**
70+
* @param string $className
71+
* @param string $proxyFullName
72+
*
73+
* @return object
74+
*/
75+
private function makeProxyObject(string $className, string $proxyFullName)
76+
{
77+
return $this->container->make($proxyFullName, ['role' => $className, 'scope' => []]);
78+
}
79+
}

tests/Promise/Fixtures/EntityWithoutTrait.php renamed to tests/Promise/ProxyPrinter/Fixtures/Entity.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Cycle\ORM\Promise\Tests\Fixtures;
4+
namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Fixtures;
55

6-
class EntityWithoutTrait
6+
abstract class Entity
77
{
8+
use TestTrait;
89
const MY_CONST = 'value';
910

1011
public $public;
@@ -18,8 +19,9 @@ public function __construct()
1819
//have some body
1920
}
2021

21-
public function public()
22+
public function public(): ?string
2223
{
24+
return 'pub';
2325
}
2426

2527
public function __resolver()
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Fixtures;
5+
6+
class EntityInNamespace
7+
{
8+
9+
}

tests/Promise/Fixtures/EntityWithConstructor.php renamed to tests/Promise/ProxyPrinter/Fixtures/EntityWithConstructor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace Cycle\ORM\Promise\Tests\Fixtures;
4+
namespace Cycle\ORM\Promise\Tests\ProxyPrinter\Fixtures;
55

66
class EntityWithConstructor
77
{

0 commit comments

Comments
 (0)