XMLValidationTest.php 4.74 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php

namespace Yandex\Allure\Adapter;

use Doctrine\Common\Annotations\AnnotationRegistry;
use DOMDocument;
use SebastianBergmann\Exporter\Exception;
use Yandex\Allure\Adapter\Event\AddAttachmentEvent;
use Yandex\Allure\Adapter\Event\StepFinishedEvent;
use Yandex\Allure\Adapter\Event\StepStartedEvent;
use Yandex\Allure\Adapter\Event\TestCaseFailedEvent;
use Yandex\Allure\Adapter\Event\TestCaseFinishedEvent;
use Yandex\Allure\Adapter\Event\TestCaseStartedEvent;
use Yandex\Allure\Adapter\Event\TestSuiteFinishedEvent;
use Yandex\Allure\Adapter\Event\TestSuiteStartedEvent;
use Yandex\Allure\Adapter\Model\Description;
use Yandex\Allure\Adapter\Model\DescriptionType;
use Yandex\Allure\Adapter\Model\Label;
use Yandex\Allure\Adapter\Model\Parameter;
use Yandex\Allure\Adapter\Model\ParameterKind;
use Yandex\Allure\Adapter\Model\SeverityLevel;

const TEST_CASE_NAME = 'test-case-name';
const TEST_CASE_TITLE = 'test-case-title';
const TEST_SUITE_UUID = 'test-suite-uuid';
const TEST_SUITE_NAME = 'test-suite-name';
const TEST_SUITE_TITLE = 'test-suite-title';
const DESCRIPTION = 'test-suite-description';
const FEATURE_NAME = 'test-feature';
const STORY_NAME = 'test-story';
const PARAMETER_NAME = 'test-parameter-name';
const PARAMETER_VALUE = 'test-parameter-value';
const FAILURE_MESSAGE = 'failure-message';
const STEP_NAME = 'test-step-name';
const STEP_TITLE = 'test-step-title';
const STEP_ATTACHMENT_TITLE = 'step-attachment-caption';
const STEP_ATTACHMENT_SOURCE = 'step-attachment-source';

class XMLValidationTest extends \PHPUnit_Framework_TestCase
{
    public function testGeneratedXMLIsValid()
    {
        $tmpDir = $this->prepareDirForXML();
        $uuid = $this->generateXML($tmpDir);

        $fileName = $tmpDir . DIRECTORY_SEPARATOR . $uuid . '-testsuite.xml';
        $this->assertTrue(file_exists($fileName));
        $this->validateFileXML($fileName);
    }

    private function prepareDirForXML()
    {
        $tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('allure-xml-test');
        mkdir($tmpDir);
        $this->assertTrue(
            file_exists($tmpDir) &&
            is_writable($tmpDir)
        );

        return $tmpDir;
    }

    private function generateXML($tmpDir)
    {
        AnnotationRegistry::registerAutoloadNamespace(
            'JMS\Serializer\Annotation',
            __DIR__ . "/../../../../vendor/jms/serializer/src"
        );
        Model\Provider::setOutputDirectory($tmpDir);
        Allure::setDefaultLifecycle();
        $testSuiteStartedEvent = new TestSuiteStartedEvent(TEST_SUITE_NAME);
        $uuid = $testSuiteStartedEvent->getUuid();
        $testSuiteStartedEvent->setTitle(TEST_SUITE_TITLE);
        $testSuiteStartedEvent->setDescription(new Description(DescriptionType::HTML, DESCRIPTION));
        $testSuiteStartedEvent->setLabels([
            Label::feature(FEATURE_NAME),
            Label::story(STORY_NAME)
        ]);
        Allure::lifecycle()->fire($testSuiteStartedEvent);

        $testCaseStartedEvent = new TestCaseStartedEvent($uuid, TEST_CASE_NAME);
        $testCaseStartedEvent->setDescription(new Description(DescriptionType::MARKDOWN, DESCRIPTION));
        $testCaseStartedEvent->setLabels([
            Label::feature(FEATURE_NAME),
            Label::story(STORY_NAME),
            Label::severity(SeverityLevel::MINOR)
        ]);
        $testCaseStartedEvent->setTitle(TEST_CASE_TITLE);
        $testCaseStartedEvent->setParameters([
            new Parameter(PARAMETER_NAME, PARAMETER_VALUE, ParameterKind::SYSTEM_PROPERTY)
        ]);
        Allure::lifecycle()->fire($testCaseStartedEvent);

        $testCaseFailureEvent = new TestCaseFailedEvent();
        $testCaseFailureEvent = $testCaseFailureEvent->withMessage(FAILURE_MESSAGE)->withException(new \Exception());
        Allure::lifecycle()->fire($testCaseFailureEvent);

        $stepStartedEvent = new StepStartedEvent(STEP_NAME);
        $stepStartedEvent = $stepStartedEvent->withTitle(STEP_TITLE);
        Allure::lifecycle()->fire($stepStartedEvent);
        Allure::lifecycle()->fire(
            new AddAttachmentEvent(STEP_ATTACHMENT_SOURCE, STEP_ATTACHMENT_TITLE, 'text/plain')
        );
        Allure::lifecycle()->fire(new StepFinishedEvent());

        Allure::lifecycle()->fire(new TestCaseFinishedEvent());
        Allure::lifecycle()->fire(new TestSuiteFinishedEvent($uuid));

        return $uuid;
    }

    private function validateFileXML($fileName)
    {
        libxml_use_internal_errors(true); //Comment this line to see DOMDocument XML validation errors
        $dom = new DOMDocument();
        $dom->load($fileName);
        $schemaFilename = __DIR__ . DIRECTORY_SEPARATOR . 'allure-1.4.0.xsd';
        $isSchemaValid = $dom->schemaValidate($schemaFilename);
        $this->assertTrue($isSchemaValid, 'XML file should be valid');
    }
}