File: tests/EmailTest.php

Recommend this page to a friend!
  Classes of Scott Arciszewski  >  PHP GPG Email Encrypt  >  tests/EmailTest.php  >  Download  
File: tests/EmailTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: PHP GPG Email Encrypt
Encrypt, decrypt and sign email messages with GPG
Author: By
Last change: Boyscouting commit (please read more)

- Use new base exception class
- Clone message objects (mutability is bad)
- Added API methods for updating config at runtime
- Previously, config could only be changed via the constructor
- Fixed docblocks, etc. for Psalm checks
Simplify licensing, boyscouting.
Date: 2 years ago
Size: 3,918 bytes
 

Contents

Class file image Download
<?php
use ParagonIE\GPGMailer\GPGMailer;
use
ParagonIE\GPGMailer\GPGMailerException;
use
PHPUnit\Framework\TestCase;
use
Zend\Mail\Message;
use
Zend\Mail\Transport\File;

/**
 * Class EmailTest
 */
class EmailTest extends TestCase
{
   
/**
     * @covers GPGMailer::import()
     * @covers GPGMailer::export()
     * @throws Crypt_GPG_Exception
     * @throws Crypt_GPG_FileException
     * @throws PEAR_Exception
     * @throws GPGMailerException
     */
   
public function testImport()
    {
       
// Instantiate GPGMailer:
       
$gpgMailer = new GPGMailer(
            new
File(),
            [
'homedir' => '~']
        );

       
$publicKey = file_get_contents(__DIR__ . '/public.key');
       
$fingerprint = $gpgMailer->import($publicKey);
       
$this->assertSame(
           
'1B6EFC02852A489B8162033CC7C64BB7CA403A7E',
           
$fingerprint
       
);

       
$exported = $gpgMailer->export($fingerprint);
       
$this->assertSame(
           
$this->stripComment($publicKey),
           
$this->stripComment($exported)
        );
    }

   
/**
     * @covers GPGMailer::decrypt()
     * @covers GPGMailer::encrypt()
     * @throws Crypt_GPG_Exception
     * @throws Crypt_GPG_FileException
     * @throws PEAR_Exception
     * @throws GPGMailerException
     */
   
public function testEncryptedMessage()
    {
       
// First, create a Zend\Mail message as usual:
       
$plaintext = 'Cleartext for now. Do not worry; this gets encrypted. Don\' actually send this, however.';
       
$plaintext .= \random_bytes(32);
       
$message = new Message;
       
$message->addTo('nobody@example.com', 'GPGMailer Test Email');
       
$message->setBody($plaintext);

       
// Instantiate GPGMailer:
       
$gpgMailer = new GPGMailer(
            new
File(),
            [
'homedir' => '~']
        );

       
$publicKey = file_get_contents(__DIR__ . '/public.key');
       
$fingerprint = $gpgMailer->import($publicKey);

       
$encrypted = $gpgMailer->encrypt($message, $fingerprint);
       
$body = $encrypted->getBodyText();
       
$this->assertTrue(
            \
strpos($body, '-----BEGIN PGP MESSAGE-----') !== false
       
);
       
$privateKey = file_get_contents(__DIR__ . '/private.key');
       
$gpgMailer->setPrivateKey($privateKey);
       
       
$decrypted = $gpgMailer->decrypt($encrypted);
       
$this->assertSame(
           
$plaintext,
           
$decrypted->getBodyText()
        );
    }

   
/**
     * @covers GPGMailer::sign()
     * @covers GPGMailer::verify()
     * @throws Exception
     * @throws PEAR_Exception
     */
   
public function testSignedMessage()
    {
       
// First, create a Zend\Mail message as usual:
       
$message = new Message;
       
$message->addTo('nobody@example.com', 'GPGMailer Test Email');
       
$message->setBody(
           
'Cleartext for now. We are going to sign this.'
       
);

       
$privateKey = file_get_contents(__DIR__ . '/private.key');

       
// Instantiate GPGMailer:
       
$gpgMailer = new GPGMailer(
            new
File(),
            [
'homedir' => '~'],
           
$privateKey
       
);

       
$publicKey = file_get_contents(__DIR__ . '/public.key');
       
$signature = $gpgMailer->sign($message);
       
$this->assertTrue(
            \
strpos($signature->getBodyText(), '-----BEGIN PGP SIGNATURE-----') !== false
       
);

       
$fingerprint = $gpgMailer->import($publicKey);
       
$this->assertTrue(
           
$gpgMailer->verify($signature, $fingerprint)
        );
    }

   
/**
     * @param string $input
     * @return string
     */
   
protected function stripComment(string $input): string
   
{
       
$pieces = \explode("\n", $input);
        foreach (
$pieces as $i => $piece) {
            if (\
preg_match('/^Version:/', $piece)) {
                unset(
$pieces[$i]);
            }
        }
        return \
str_replace("\r", '', \implode('', $pieces));
    }
}


For more information send a message to info at phpclasses dot org.