@@ -48,10 +48,34 @@ public function testValidateWithNoEntity()
48
48
$ this ->validator ->validate ('foobar ' , $ constraint );
49
49
}
50
50
51
+ public function testValidateValidEntity ()
52
+ {
53
+ $ this ->context ->expects ($ this ->never ())->method ('buildViolation ' );
54
+ $ constraint = new EntityExist ();
55
+ $ constraint ->entity = 'App\Entity\User ' ;
56
+
57
+ $ repository = $ this ->getMockBuilder (EntityRepository::class)
58
+ ->disableOriginalConstructor ()
59
+ ->getMock ();
60
+ $ repository
61
+ ->expects ($ this ->once ())
62
+ ->method ('findOneBy ' )
63
+ ->with (['id ' => 'foobar ' ])
64
+ ->willReturn ('my_user ' );
65
+
66
+ $ this ->entityManager
67
+ ->expects ($ this ->once ())
68
+ ->method ('getRepository ' )
69
+ ->with ('App\Entity\User ' )
70
+ ->willReturn ($ repository );
71
+
72
+ $ this ->validator ->validate ('foobar ' , $ constraint );
73
+ }
74
+
51
75
/**
52
- * @dataProvider getValidValues
76
+ * @dataProvider getEmptyOrNull
53
77
*/
54
- public function testValidateValidEntity ($ value )
78
+ public function testValidateSkipsIfValueEmptyOrNull ($ value )
55
79
{
56
80
$ this ->context ->expects ($ this ->never ())->method ('buildViolation ' );
57
81
$ constraint = new EntityExist ();
@@ -61,23 +85,22 @@ public function testValidateValidEntity($value)
61
85
->disableOriginalConstructor ()
62
86
->getMock ();
63
87
$ repository
64
- ->expects ($ this ->once ( ))
88
+ ->expects ($ this ->exactly ( 0 ))
65
89
->method ('findOneBy ' )
66
90
->with (['id ' => $ value ])
67
91
->willReturn ('my_user ' );
68
92
69
93
$ this ->entityManager
70
- ->expects ($ this ->once ( ))
94
+ ->expects ($ this ->exactly ( 0 ))
71
95
->method ('getRepository ' )
72
96
->with ('App\Entity\User ' )
73
97
->willReturn ($ repository );
74
98
75
99
$ this ->validator ->validate ($ value , $ constraint );
76
100
}
77
101
78
- public function getValidValues ()
102
+ public function getEmptyOrNull ()
79
103
{
80
- yield ['foobar ' ];
81
104
yield ['' ];
82
105
yield [null ];
83
106
}
0 commit comments