EntityFormLayerTest::testWrongLoad()
last analyzed

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
1
<?php
2
3
4
namespace Pfilsx\FormLayer\Tests\Layer;
5
6
7
use DateTime;
8
use Pfilsx\FormLayer\Exception\InvalidArgumentException;
9
use Pfilsx\FormLayer\Tests\app\Entity\Model;
10
use Pfilsx\FormLayer\Tests\app\Entity\Node;
11
use Pfilsx\FormLayer\Tests\app\FormLayer\ModelFormLayer;
12
use Pfilsx\FormLayer\Tests\app\FormLayer\NodeFormLayer;
13
use PHPUnit\Framework\TestCase;
14
15
class EntityFormLayerTest extends TestCase
16
{
17
    /**
18
     * @dataProvider getLayers
19
     * @param string $formLayerClass
20
     * @param string $entityClass
21
     * @param bool $useMethod
22
     */
23
    public function testEmptyCreate($formLayerClass, $entityClass, $useMethod)
24
    {
25
        /**
26
         * @var NodeFormLayer|ModelFormLayer $layer
27
         */
28
        $layer = new $formLayerClass();
29
        $this->assertNull($layer->getId());
30
        $this->assertNull($layer->createdAt);
31
        $this->assertNull($layer->content);
32
33
        $layer->createdAt = '01.01.1970';
34
        $node = $layer->create();
35
        $this->assertInstanceOf($entityClass, $node);
36
        $this->assertEquals(new DateTime('01.01.1970'), $useMethod ? $node->getCreatedAt() : $node->createdAt);
37
    }
38
39
    /**
40
     * @dataProvider getLayers
41
     * @param string $formLayerClass
42
     */
43
    public function testWrongLoad($formLayerClass)
44
    {
45
        $node = new class {
46
        };
47
        /**
48
         * @var NodeFormLayer|ModelFormLayer $layer
49
         */
50
        $layer = new $formLayerClass();
51
        $this->expectException(InvalidArgumentException::class);
52
        $layer->load($node);
53
    }
54
55
    /**
56
     * @dataProvider getLayers
57
     * @param string $formLayerClass
58
     * @param string $entityClass
59
     */
60
    public function testLoad($formLayerClass, $entityClass, $useMethod)
61
    {
62
        /**
63
         * @var Node|Model $node
64
         */
65
        $node = new $entityClass();
66
        if ($useMethod) {
67
            $node->setCreatedAt(new DateTime('01.01.1970'))
0 ignored issues
show
Bug introduced by
The method setCreatedAt() does not exist on Pfilsx\FormLayer\Tests\app\Entity\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
            $node->/** @scrutinizer ignore-call */ 
68
                   setCreatedAt(new DateTime('01.01.1970'))

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
68
                ->setId(1)->setContent('Test content');
0 ignored issues
show
Bug introduced by
The method setContent() does not exist on Pfilsx\FormLayer\Tests\app\Entity\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                ->setId(1)->/** @scrutinizer ignore-call */ setContent('Test content');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setId() does not exist on Pfilsx\FormLayer\Tests\app\Entity\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
                ->/** @scrutinizer ignore-call */ setId(1)->setContent('Test content');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        } else {
70
            $node->createdAt = new DateTime('01.01.1970');
0 ignored issues
show
Bug introduced by
The property createdAt is declared protected in Pfilsx\FormLayer\Tests\app\Entity\Node and cannot be accessed from this context.
Loading history...
71
            $node->id = 1;
0 ignored issues
show
Bug introduced by
The property id is declared protected in Pfilsx\FormLayer\Tests\app\Entity\Node and cannot be accessed from this context.
Loading history...
72
            $node->content = 'Test content';
0 ignored issues
show
Bug introduced by
The property content is declared protected in Pfilsx\FormLayer\Tests\app\Entity\Node and cannot be accessed from this context.
Loading history...
73
        }
74
        /**
75
         * @var NodeFormLayer|ModelFormLayer $layer
76
         */
77
        $layer = new $formLayerClass();
78
        $layer->load($node);
79
        $this->assertEquals(1, $layer->getId());
80
        $this->assertEquals('01.01.1970', $layer->createdAt);
81
        $this->assertEquals('Test content', $layer->content);
82
83
        $layer->createdAt = '02.01.1970';
84
        $layer->update();
85
        $this->assertEquals(new DateTime('02.01.1970'), $useMethod ? $node->getCreatedAt() : $node->createdAt);
0 ignored issues
show
Bug introduced by
The method getCreatedAt() does not exist on Pfilsx\FormLayer\Tests\app\Entity\Model. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $this->assertEquals(new DateTime('02.01.1970'), $useMethod ? $node->/** @scrutinizer ignore-call */ getCreatedAt() : $node->createdAt);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
    }
87
88
    /**
89
     * @dataProvider getLayers
90
     * @param string $formLayerClass
91
     * @param string $entityClass
92
     */
93
    public function testForceCreate($formLayerClass, $entityClass)
94
    {
95
        /**
96
         * @var Node|Model $node
97
         */
98
        $node = new $entityClass();
99
        /**
100
         * @var NodeFormLayer|ModelFormLayer $layer
101
         */
102
        $layer = new $formLayerClass();
103
        $layer->load($node);
104
        $this->assertSame($node, $layer->create());
105
        $this->assertNotSame($node, $layer->create(true));
106
    }
107
108
    public function getLayers()
109
    {
110
        yield [
111
            NodeFormLayer::class,
112
            Node::class,
113
            true
114
        ];
115
        yield [
116
            ModelFormLayer::class,
117
            Model::class,
118
            false
119
        ];
120
    }
121
}
122