1+ <?php
2+
3+
4+ namespace Surfnet \SamlBundle \Tests \Unit \Metadata ;
5+
6+ use PHPUnit \Framework \TestCase ;
7+ use ReflectionMethod ;
8+ use Surfnet \SamlBundle \Metadata \MetadataFactory ;
9+
10+ class MetadataFactoryTest extends TestCase
11+ {
12+ public function testGetCertificateData (): void
13+ {
14+ $ publicKeyFile = __DIR__ . '/certificate.pem ' ; // File with test certificate in PEM format
15+ // Read the public key file and remove the first and last lines and all newlines
16+ $ expectedCertificate = str_replace ("\n" , '' , implode ("" , array_slice (file ($ publicKeyFile ), 1 , -1 )));
17+
18+ // Setup a mock for the MetadataFactory with the real getCertificateData method
19+ // and add the mocked File class to it
20+ $ metadataFactoryMock = $ this ->getMockBuilder (MetadataFactory::class)
21+ ->disableOriginalConstructor ()
22+ ->onlyMethods (['getCertificateData ' ])
23+ ->getMock ();
24+
25+ // Setup a reflection to call the private method
26+ $ reflectionMethod = new ReflectionMethod ($ metadataFactoryMock ::class, 'getCertificateData ' );
27+
28+ // Test getCertificateData method with a valid certificate
29+ $ result = $ reflectionMethod ->invoke ($ metadataFactoryMock , $ publicKeyFile );
30+ $ this ->assertEquals ($ expectedCertificate , $ result );
31+
32+ // Test with an invalid certificate
33+ $ invalidPublicKeyFile = __DIR__ . '/invalid_certificate.pem ' ; // File with invalid certificate
34+ $ this ->expectException (\RuntimeException::class);
35+ $ this ->expectExceptionMessage ('Could not parse PEM certificate in ' . $ invalidPublicKeyFile );
36+ $ reflectionMethod ->invoke ($ metadataFactoryMock , $ invalidPublicKeyFile );
37+ }
38+ }
0 commit comments