Passed
Pull Request — master (#173)
by
unknown
02:52
created

PropertyTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 164
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 48
c 1
b 0
f 1
dl 0
loc 164
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A generate_float() 0 5 1
A generate_firstname() 0 5 1
A create_string_property_from_annotation_or_field_name_with_annotation_url() 0 9 1
A generate_integer() 0 5 1
A create_string_property_from_annotation_or_field_name_with_field_name() 0 5 1
A create_json_property_with_field_name_roles() 0 6 1
A create_date() 0 13 1
A generate_bool() 0 6 1
A create_string_property_from_annotation_or_field_name_with_annotation_email() 0 9 1
A get_probs_from_entity() 0 13 1
A create_json_property_with_default() 0 6 1
A generate_email() 0 5 1
1
<?php
2
3
namespace Zenstruck\Foundry\Tests\Functional\Bundle\Extractor;
4
5
use DateTime;
6
use ReflectionClass;
7
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
8
use Symfony\Component\Validator\Constraints\Email;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Constraints\Email was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Symfony\Component\Validator\Constraints\Url;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Validator\Constraints\Url was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Zenstruck\Foundry\Bundle\Extractor\Property;
11
use Zenstruck\Foundry\Test\Factories;
12
13
class PropertyTest extends KernelTestCase
14
{
15
    use Factories;
16
17
    /**
18
     * @var Property|object|null
19
     */
20
    private $property;
21
22
    protected function setUp(): void
23
    {
24
        self::bootKernel();
25
        $em = static::getContainer()->get('doctrine.orm.default_entity_manager');
26
        $this->property = new Property($em);
0 ignored issues
show
Bug introduced by
It seems like $em can also be of type null; however, parameter $em of Zenstruck\Foundry\Bundle...Property::__construct() does only seem to accept Doctrine\ORM\EntityManagerInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

26
        $this->property = new Property(/** @scrutinizer ignore-type */ $em);
Loading history...
27
        //$this->property = static::getContainer()->get('Zenstruck\Foundry\Bundle\Extractor\Property');
28
    }
29
30
    /**
31
     * @TODO add an TestEntity in this bundle to decouple from Project and possibility to test all Cases
32
     *
33
     * @test
34
     */
35
    public function get_probs_from_entity()
36
    {
37
        // @TODO replace User Entity with Fixtures
38
        $this->markTestSkipped('replace User Entity with Fixtures');
39
40
        $ref = new ReflectionClass('App\Entity\User');
41
        $probs = $this->property->getScalarPropertiesFromDoctrineFieldMappings($ref);
0 ignored issues
show
Bug introduced by
The method getScalarPropertiesFromDoctrineFieldMappings() does not exist on null. ( Ignorable by Annotation )

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

41
        /** @scrutinizer ignore-call */ 
42
        $probs = $this->property->getScalarPropertiesFromDoctrineFieldMappings($ref);

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...
42
43
        $this->assertCount(6, $probs);
44
        $this->assertIsArray($probs['roles']);
45
        $this->assertEquals('ROLE_USER', $probs['roles'][0]);
46
        $this->assertTrue($probs['isVerifiedAccount']);
47
        $this->assertEquals(10, \mb_strlen($probs['lastLogin']));
48
    }
49
50
    /**
51
     * @test
52
     */
53
    public function generate_email()
54
    {
55
        $email = $this->property->createScalarProperties('string', [], 'email');
56
57
        $this->assertStringContainsString('@', $email);
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function generate_firstname()
64
    {
65
        $firstName = $this->property->createScalarProperties('string', [], 'firstName');
66
67
        $this->assertIsString($firstName);
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function generate_integer()
74
    {
75
        $int = $this->property->createScalarProperties('integer', [], 'postalCode');
76
77
        $this->assertIsInt($int);
78
    }
79
80
    /**
81
     * @test
82
     */
83
    public function generate_bool()
84
    {
85
        $bool = $this->property->createScalarProperties('boolean', [], 'isActive');
86
87
        $this->assertTrue($bool);
88
        $this->assertIsBool($bool);
89
    }
90
91
    /**
92
     * @test
93
     */
94
    public function generate_float()
95
    {
96
        $float = $this->property->createScalarProperties('float', [], 'price');
97
98
        $this->assertIsFloat($float);
99
    }
100
101
    /**
102
     * @test
103
     */
104
    public function create_json_property_with_field_name_roles()
105
    {
106
        $roleUser = $this->property->createJsonPropertyFromAnnotationOrFieldName([], 'roles');
107
108
        $this->assertIsArray($roleUser);
109
        $this->assertEquals('ROLE_USER', $roleUser[0]);
110
    }
111
112
    /**
113
     * @test
114
     */
115
    public function create_json_property_with_default()
116
    {
117
        $roleUser = $this->property->createJsonPropertyFromAnnotationOrFieldName([], 'foo');
118
119
        $this->assertIsArray($roleUser);
120
        $this->assertEquals('DEFAULT', $roleUser[0]);
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function create_string_property_from_annotation_or_field_name_with_field_name()
127
    {
128
        $displayName = $this->property->createStringPropertyFromAnnotationOrFieldName([], 'displayName');
129
130
        $this->assertIsString($displayName);
131
    }
132
133
    /**
134
     * @test
135
     */
136
    public function create_string_property_from_annotation_or_field_name_with_annotation_email()
137
    {
138
        // @TODO investigate here
139
        $this->markTestSkipped('Symfony Constraints missing');
140
141
        $emailConstraint = new Email();
142
        $email = $this->property->createStringPropertyFromAnnotationOrFieldName([$emailConstraint], 'displayName');
143
144
        $this->assertStringContainsString('@', $email);
145
    }
146
147
    /**
148
     * @test
149
     */
150
    public function create_string_property_from_annotation_or_field_name_with_annotation_url()
151
    {
152
        // @TODO investigate here
153
        $this->markTestSkipped('Symfony Constraints missing');
154
155
        $urlConstraint = new Url();
156
        $url = $this->property->createStringPropertyFromAnnotationOrFieldName([$urlConstraint], 'displayName');
157
158
        $this->assertStringContainsString('http', $url);
159
    }
160
161
    /**
162
     * @test
163
     */
164
    public function create_date()
165
    {
166
        // reset our properties
167
        $this->property->setProperties([]);
168
169
        $this->property->createDateTimeProperty('someNiceDateField');
170
171
        // assert
172
        $props = $this->property->getProperties();
173
        $date = new DateTime($props['someNiceDateField']);
174
        $this->assertCount(1, $props);
175
        $this->assertEquals(10, \mb_strlen($props['someNiceDateField']));
176
        $this->assertInstanceOf(DateTime::class, $date);
177
    }
178
}
179