1
+ <?php
2
+
3
+ namespace Symfony \Bundle \DoctrineBundle \Tests \Form \ValueTransformer ;
4
+
5
+ use Doctrine \Common \Collections \ArrayCollection ;
6
+ use Doctrine \ORM \Tools \SchemaTool ;
7
+ use Symfony \Bundle \DoctrineBundle \Form \ValueTransformer \EntityToIDTransformer ;
8
+
9
+ class EntityToIDTransformerTest extends \Symfony \Bundle \DoctrineBundle \Tests \TestCase
10
+ {
11
+ /**
12
+ * @var EntityManager
13
+ */
14
+ private $ em ;
15
+
16
+ public function setUp ()
17
+ {
18
+ parent ::setUp ();
19
+ $ this ->em = $ this ->createTestEntityManager ();
20
+
21
+ $ schemaTool = new SchemaTool ($ this ->em );
22
+ $ classes = array ($ this ->em ->getClassMetadata ('Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag ' ));
23
+ try {
24
+ $ schemaTool ->dropSchema ($ classes );
25
+ } catch (\Exception $ e ) {
26
+
27
+ }
28
+ try {
29
+ $ schemaTool ->createSchema ($ classes );
30
+ } catch (\Exception $ e ) {
31
+
32
+ }
33
+ }
34
+
35
+ public function testRequiredEntityManager ()
36
+ {
37
+ $ this ->setExpectedException ('Symfony\Component\Form\Exception\MissingOptionsException ' );
38
+ $ transformer = new EntityToIDTransformer (array ('className ' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag ' ));
39
+ }
40
+
41
+ public function testRequiredClassName ()
42
+ {
43
+ $ this ->setExpectedException ('Symfony\Component\Form\Exception\MissingOptionsException ' );
44
+ $ transformer = new EntityToIDTransformer (array ('em ' => $ this ->em ));
45
+ }
46
+
47
+ public function createTransformer ()
48
+ {
49
+ $ transformer = new EntityToIDTransformer (array (
50
+ 'em ' => $ this ->em ,
51
+ 'className ' => 'Symfony\Bundle\DoctrineBundle\Tests\Form\ValueTransformer\Tag '
52
+ ));
53
+ return $ transformer ;
54
+ }
55
+
56
+ public function testTranformEmptyValueReturnsNull ()
57
+ {
58
+ $ transformer = $ this ->createTransformer ();
59
+ $ this ->assertEquals (0 , $ transformer ->transform (null ));
60
+ $ this ->assertEquals (0 , $ transformer ->transform ("" ));
61
+ $ this ->assertEquals (0 , $ transformer ->transform (0 ));
62
+ }
63
+
64
+ public function testTransform ()
65
+ {
66
+ $ transformer = $ this ->createTransformer ();
67
+
68
+ $ tag = new Tag ("name " );
69
+ $ this ->em ->persist ($ tag );
70
+ $ this ->em ->flush ();
71
+
72
+ $ this ->assertEquals (1 , $ transformer ->transform ($ tag ));
73
+ }
74
+
75
+ public function testReverseTransformEmptyValue ()
76
+ {
77
+ $ transformer = $ this ->createTransformer ();
78
+ $ this ->assertNull ($ transformer ->reverseTransform (0 , null ));
79
+ }
80
+
81
+ public function testReverseTransform ()
82
+ {
83
+ $ transformer = $ this ->createTransformer ();
84
+
85
+ $ tag = new Tag ("name " );
86
+ $ this ->em ->persist ($ tag );
87
+ $ this ->em ->flush ();
88
+
89
+ $ this ->assertSame ($ tag , $ transformer ->reverseTransform (1 , null ));
90
+ }
91
+ }
0 commit comments