Ejemplo n.º 1
0
/**
 * Implements functionThatShouldNotShowUp();
 */
function test_function()
{
    // Test a normal function should work properly.
    normalFunction('test data');
    // Test exceptions: start simple - call a function that doesn't exist with a space in between the function name
    // and "(".
    functionThatDoesNotExist('param1', 'param2');
    functionThatDoesNotExist2('param1', 'param2');
    // Call a function within a function.
    abc(def('abc'));
    // Use a constant, that can be misconstrued as a function potentially.
    $test_result = 3 - TEST_CONST;
    // Test referring to an object.
    $testJsonResponse = (object) array('data' => 'abc');
    $testJsonResponse->data = 'abc';
    $result = drupal_json_decode($json->data);
    // Create a reference to returned value
    $a =& reference_returner_function();
    // Static method call
    StaticTestClass::staticFunctionCall();
    // Method call
    $someInstance->someMetod();
    // Creating a class
    $a = new stdClass();
}
Ejemplo n.º 2
0
class ServiceLogTest extends \PHPUnit_Framework_TestCase
{
    // Verifies that the service log's error log exists and is writable by the user that's executing
    // the tests. Without these properties, the service log cannot function correctly.
    public function testErrorLogShouldBeWritable()
    {
        $this->assertTrue(file_exists(ServiceLogImpl::ERROR_LOG));
        $this->assertTrue(is_writable(ServiceLogImpl::ERROR_LOG));
    }
    // Verifies that error messages will generate alerts that are to be send to a configured list of
    // recipients. A fake mailing interface is injected to provide this testing, even though this
    // does not guarantee that the default SendmailMailer does what it's meant to do.
    public function testAlertMessages()
    {
        // @codingStandardsIgnoreStart
        // CodeSniffer does not yet understand formatting of anonymous classes.
        $mailer = new class implements IMailer
        {
            public $message;
            public function send(Message $message)
            {
                $this->message = $message;
            }
        };
        // @codingStandardsIgnoreEnd
        $serviceLog = new ServiceLogImpl($mailer);
        $serviceLog->onServiceExecuted('id-success', 0.123);
        try {
            functionThatDoesNotExist();
        } catch (\Throwable $exception) {
            $serviceLog->onServiceException('id-throws', 12.345, $exception);
        }
        $serviceLog->onFinish();
        // A message must have been sent to the Mailer instance by now.
        $this->assertTrue($mailer->message instanceof Message);
        $body = $mailer->message->getBody();
        // Confirm that the |$body| mentions the identifiers of the services that threw an exception
        // as well as the log message associated with the exception.
        $this->assertContains('id-throws', $body);
        $this->assertContains('functionThatDoesNotExist', $body);
    }
    // Verifies that successful runs of the service manager will write messages to the log, but will
    // not distribute an alert message.
    public function testSuccessMessageBailOut()
    {
        // @codingStandardsIgnoreStart
        // CodeSniffer does not yet understand formatting of anonymous classes.
        $mailer = new class implements IMailer
        {
            public $message;
            public function send(Message $message)
            {
                $this->message = $message;
            }
        };
        // @codingStandardsIgnoreEnd
        $serviceLog = new ServiceLogImpl($mailer);
        $serviceLog->onServiceExecuted('id-success', 0.123);
        $serviceLog->onFinish();
        $this->assertEquals(1, $serviceLog->getMessageCountForTesting());
        $this->assertNull($mailer->message);
    }
}