1313namespace App \Tests \Controller ;
1414
1515use App \Audit \AuditRecordType ;
16+ use App \Entity \AuditRecord ;
1617use App \Entity \Package ;
1718use App \Entity \Version ;
1819use Doctrine \DBAL \Connection ;
@@ -36,35 +37,42 @@ protected function tearDown(): void
3637 parent ::tearDown ();
3738 }
3839
39- public function testVersionChangesGetRecorded (): void
40+ public function testVersionCreationGetsRecorded (): void
4041 {
4142 $ container = static ::getContainer ();
4243 $ em = $ container ->get (ManagerRegistry::class)->getManager ();
4344
44- $ package = new Package ();
45- $ package ->setRepository ('https://github.com/composer/composer ' );
45+ $ version = $ this ->createPackageAndVersion ();
46+
47+ $ log = $ em ->getRepository (AuditRecord::class)->findOneBy ([
48+ 'type ' => AuditRecordType::VersionCreated,
49+ 'packageId ' => $ version ->getPackage ()->getId (),
50+ ]);
51+
52+ self ::assertNotNull ($ log , 'No audit record created for new version ' );
53+ self ::assertSame ('automation ' , $ log ->attributes ['actor ' ]);
54+ self ::assertSame ('composer ' , $ log ->vendor );
55+ self ::assertEqualsCanonicalizing ([
56+ 'name ' => 'composer/composer ' ,
57+ 'actor ' => 'automation ' ,
58+ 'version ' => '1.0.0 ' ,
59+ ], $ log ->attributes );
60+ }
4661
47- $ version = new Version ();
48- $ version ->setPackage ($ package );
49- $ version ->setName ($ package ->getName ());
50- $ version ->setVersion ('1.0.0 ' );
51- $ version ->setNormalizedVersion ('1.0.0.0 ' );
52- $ version ->setDevelopment (false );
53- $ version ->setLicense ([]);
54- $ version ->setAutoload ([]);
55- $ version ->setDist (['reference ' => 'old-dist-ref ' , 'type ' => 'zip ' , 'url ' => 'https://example.org/dist.zip ' ]);
62+ public function testVersionChangesGetRecorded (): void
63+ {
64+ $ container = static ::getContainer ();
65+ $ em = $ container ->get (ManagerRegistry::class)->getManager ();
5666
57- $ em ->persist ($ package );
58- $ em ->persist ($ version );
59- $ em ->flush ();
67+ $ version = $ this ->createPackageAndVersion ();
6068
6169 $ version ->setDist (['reference ' => 'new-dist-ref ' , 'type ' => 'zip ' , 'url ' => 'https://example.org/dist.zip ' ]);
6270 $ version ->setSource (['reference ' => 'new-source-ref ' , 'type ' => 'git ' , 'url ' => 'git://example.org/dist.zip ' ]);
6371 $ em ->persist ($ version );
6472 $ em ->flush ();
6573
6674 $ logs = $ container ->get (Connection::class)->fetchAllAssociative ('SELECT * FROM audit_log ORDER BY id DESC ' );
67- self ::assertCount (2 , $ logs ); // package creation + version reference change
75+ self ::assertCount (3 , $ logs ); // package creation + version creation + version reference change
6876 self ::assertSame (AuditRecordType::VersionReferenceChanged->value , $ logs [0 ]['type ' ]);
6977 self ::assertSame ('{"name": "composer/composer", "dist_to": "new-dist-ref", "version": "1.0.0", "dist_from": "old-dist-ref", "source_to": "new-source-ref", "source_from": null} ' , $ logs [0 ]['attributes ' ]);
7078
@@ -74,15 +82,15 @@ public function testVersionChangesGetRecorded(): void
7482 $ em ->flush ();
7583
7684 $ logs = $ container ->get (Connection::class)->fetchAllAssociative ('SELECT * FROM audit_log ORDER BY id DESC ' );
77- self ::assertCount (2 , $ logs ); // package creation + version reference change
85+ self ::assertCount (3 , $ logs ); // package creation + version creation + version reference change
7886
7987 // verify that changing dist only without ref change does not create new audit log and does not crash
8088 $ version ->setDist (['reference ' => 'new-dist-ref ' , 'type ' => 'zip2 ' , 'url ' => 'https://example.org/dist.zip2 ' ]);
8189 $ em ->persist ($ version );
8290 $ em ->flush ();
8391
8492 $ logs = $ container ->get (Connection::class)->fetchAllAssociative ('SELECT * FROM audit_log ORDER BY id DESC ' );
85- self ::assertCount (2 , $ logs ); // package creation + version reference change
93+ self ::assertCount (3 , $ logs ); // package creation + version creation + version reference change
8694
8795 // verify that only reference changes triggers a new audit log
8896 $ version ->setDist (['reference ' => 'new-dist-ref ' , 'type ' => 'zip3 ' , 'url ' => 'https://example.org/dist.zip2 ' ]);
@@ -91,13 +99,38 @@ public function testVersionChangesGetRecorded(): void
9199 $ em ->flush ();
92100
93101 $ logs = $ container ->get (Connection::class)->fetchAllAssociative ('SELECT * FROM audit_log ORDER BY id DESC ' );
94- self ::assertCount (2 , $ logs );
102+ self ::assertCount (3 , $ logs );
95103
96104 $ em ->remove ($ version );
97105 $ em ->flush ();
98106
99107 $ logs = $ container ->get (Connection::class)->fetchAllAssociative ('SELECT * FROM audit_log ORDER BY id DESC ' );
100- self ::assertCount (3 , $ logs );
108+ self ::assertCount (4 , $ logs );
101109 self ::assertSame (AuditRecordType::VersionDeleted->value , $ logs [0 ]['type ' ]);
102110 }
111+
112+ private function createPackageAndVersion (): Version
113+ {
114+ $ container = static ::getContainer ();
115+ $ em = $ container ->get (ManagerRegistry::class)->getManager ();
116+
117+ $ package = new Package ();
118+ $ package ->setRepository ('https://github.com/composer/composer ' );
119+
120+ $ version = new Version ();
121+ $ version ->setPackage ($ package );
122+ $ version ->setName ($ package ->getName ());
123+ $ version ->setVersion ('1.0.0 ' );
124+ $ version ->setNormalizedVersion ('1.0.0.0 ' );
125+ $ version ->setDevelopment (false );
126+ $ version ->setLicense ([]);
127+ $ version ->setAutoload ([]);
128+ $ version ->setDist (['reference ' => 'old-dist-ref ' , 'type ' => 'zip ' , 'url ' => 'https://example.org/dist.zip ' ]);
129+
130+ $ em ->persist ($ package );
131+ $ em ->persist ($ version );
132+ $ em ->flush ();
133+
134+ return $ version ;
135+ }
103136}
0 commit comments