Skip to content

Commit c2f2743

Browse files
committed
Merge pull request allure-framework#19 from vania-pooh/master
Added tests
2 parents 427398f + 2ef821e commit c2f2743

File tree

5 files changed

+209
-6
lines changed

5 files changed

+209
-6
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"require": {
1919
"php": ">=5.4.0",
2020
"phpunit/phpunit": ">=4.0.0",
21+
"mikey179/vfsStream": "1.*",
2122
"allure-framework/allure-php-api": "~1.1.0"
2223
},
2324
"autoload": {

phpunit.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
require_once __DIR__ . '/vendor/autoload.php';
3+
date_default_timezone_set('UTC');

phpunit.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
beStrictAboutTestsThatDoNotTestAnything="true"
5+
bootstrap="phpunit.php"
6+
colors="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false"
12+
syntaxCheck="false"
13+
>
14+
<testsuites>
15+
<testsuite name="Allure PHPUnit Adapter Tests">
16+
<directory>test/Yandex/Allure/Adapter/</directory>
17+
</testsuite>
18+
</testsuites>
19+
</phpunit>

src/Yandex/Allure/Adapter/AllureAdapter.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ public function __construct(
5151
$deletePreviousResults = false,
5252
array $ignoredAnnotations = []
5353
) {
54+
55+
$this->prepareOutputDirectory($outputDirectory, $deletePreviousResults);
56+
57+
// Add standard PHPUnit annotations
58+
Annotation\AnnotationProvider::addIgnoredAnnotations($this->ignoredAnnotations);
59+
// Add custom ignored annotations
60+
Annotation\AnnotationProvider::addIgnoredAnnotations($ignoredAnnotations);
61+
}
62+
63+
public function prepareOutputDirectory($outputDirectory, $deletePreviousResults)
64+
{
5465
if (!file_exists($outputDirectory)) {
5566
mkdir($outputDirectory, 0755, true);
5667
}
@@ -65,13 +76,8 @@ public function __construct(
6576
if (is_null(Model\Provider::getOutputDirectory())) {
6677
Model\Provider::setOutputDirectory($outputDirectory);
6778
}
68-
69-
// Add standard PHPUnit annotations
70-
Annotation\AnnotationProvider::addIgnoredAnnotations($this->ignoredAnnotations);
71-
// Add custom ignored annotations
72-
Annotation\AnnotationProvider::addIgnoredAnnotations($ignoredAnnotations);
7379
}
74-
80+
7581
/**
7682
* An error occurred.
7783
*
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
<?php
2+
3+
namespace Yandex\Allure\Adapter;
4+
5+
6+
use Exception;
7+
use org\bovigo\vfs\vfsStream;
8+
use Yandex\Allure\Adapter\Event\TestCaseBrokenEvent;
9+
use Yandex\Allure\Adapter\Event\TestCaseCanceledEvent;
10+
use Yandex\Allure\Adapter\Event\TestCaseFailedEvent;
11+
use Yandex\Allure\Adapter\Event\TestCasePendingEvent;
12+
use Yandex\Allure\Adapter\Support\MockedLifecycle;
13+
14+
const EXCEPTION_MESSAGE = 'test-exception-message';
15+
const ROOT_DIRECTORY = 'test-root-directory';
16+
const TEST_DIRECTORY = 'test-directory';
17+
18+
class AllureAdapterTest extends \PHPUnit_Framework_TestCase
19+
{
20+
21+
/**
22+
* @var MockedLifecycle
23+
*/
24+
private $mockedLifecycle;
25+
26+
/**
27+
* @var AllureAdapter
28+
*/
29+
private $allureAdapter;
30+
31+
protected function setUp()
32+
{
33+
parent::setUp();
34+
$this->mockedLifecycle = new MockedLifecycle();
35+
Allure::setLifecycle($this->getMockedLifecycle());
36+
$this->allureAdapter = new AllureAdapter('test-output-directory', true);
37+
}
38+
39+
public function testPrepareOutputDirectory()
40+
{
41+
$rootDirectory = vfsStream::setup(ROOT_DIRECTORY);
42+
$this->assertFalse($rootDirectory->hasChild(TEST_DIRECTORY));
43+
$newDirectoryPath = vfsStream::url(ROOT_DIRECTORY) . DIRECTORY_SEPARATOR . TEST_DIRECTORY;
44+
Model\Provider::setOutputDirectory(null);
45+
new AllureAdapter($newDirectoryPath, true);
46+
$this->assertTrue($rootDirectory->hasChild(TEST_DIRECTORY));
47+
$this->assertEquals($newDirectoryPath, Model\Provider::getOutputDirectory());
48+
}
49+
50+
public function testAddError()
51+
{
52+
$exception = $this->getException();
53+
$time = $this->getTime();
54+
$this->getAllureAdapter()->addError($this, $exception, $time);
55+
$events = $this->getMockedLifecycle()->getEvents();
56+
$event = new TestCaseBrokenEvent();
57+
$event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
58+
$this->assertEquals(1, sizeof($events));
59+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseBrokenEvent', $events[0]);
60+
$this->assertEquals($event, $events[0]);
61+
}
62+
63+
public function testAddFailure()
64+
{
65+
$exception = new \PHPUnit_Framework_AssertionFailedError(EXCEPTION_MESSAGE);
66+
$time = $this->getTime();
67+
$this->getAllureAdapter()->addFailure($this, $exception, $time);
68+
$events = $this->getMockedLifecycle()->getEvents();
69+
$event = new TestCaseFailedEvent();
70+
$event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
71+
$this->assertEquals(1, sizeof($events));
72+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseFailedEvent', $events[0]);
73+
$this->assertEquals($event, $events[0]);
74+
}
75+
76+
public function testAddIncompleteTest()
77+
{
78+
$exception = $this->getException();
79+
$time = $this->getTime();
80+
$this->getAllureAdapter()->addIncompleteTest($this, $exception, $time);
81+
$this->pendingTestCaseEventAssertions($exception);
82+
}
83+
84+
private function pendingTestCaseEventAssertions(\Exception $exception)
85+
{
86+
$events = $this->getMockedLifecycle()->getEvents();
87+
$event = new TestCasePendingEvent();
88+
$event->withException($exception);
89+
$this->assertEquals(1, sizeof($events));
90+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCasePendingEvent', $events[0]);
91+
$this->assertEquals($event, $events[0]);
92+
}
93+
94+
public function testAddRiskyTest()
95+
{
96+
$exception = $this->getException();
97+
$time = $this->getTime();
98+
$this->getAllureAdapter()->addRiskyTest($this, $exception, $time);
99+
$this->pendingTestCaseEventAssertions($exception);
100+
}
101+
102+
public function testAddSkippedTest()
103+
{
104+
$exception = $this->getException();
105+
$time = $this->getTime();
106+
$this->getAllureAdapter()->addSkippedTest($this, $exception, $time);
107+
$events = $this->getMockedLifecycle()->getEvents();
108+
$event = new TestCaseCanceledEvent();
109+
$event->withException($exception)->withMessage(EXCEPTION_MESSAGE);
110+
$this->assertEquals(1, sizeof($events));
111+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseCanceledEvent', $events[0]);
112+
$this->assertEquals($event, $events[0]);
113+
}
114+
115+
public function testStartTestSuite()
116+
{
117+
$this->getAllureAdapter()->startTestSuite($this->getTestSuite());
118+
$events = $this->getMockedLifecycle()->getEvents();
119+
$this->assertEquals(1, sizeof($events));
120+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteStartedEvent', $events[0]);
121+
}
122+
123+
public function testEndTestSuite()
124+
{
125+
$this->getAllureAdapter()->endTestSuite($this->getTestSuite());
126+
$events = $this->getMockedLifecycle()->getEvents();
127+
$this->assertEquals(1, sizeof($events));
128+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteFinishedEvent', $events[0]);
129+
}
130+
131+
public function testStartTest()
132+
{
133+
$this->getAllureAdapter()->startTestSuite($this->getTestSuite()); //Is needed to set $adapter->suiteName field
134+
$this->getAllureAdapter()->startTest($this);
135+
$events = $this->getMockedLifecycle()->getEvents();
136+
$this->assertEquals(2, sizeof($events));
137+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestSuiteStartedEvent', $events[0]);
138+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseStartedEvent', $events[1]);
139+
}
140+
141+
public function testEndTest()
142+
{
143+
$this->getAllureAdapter()->endTest($this, $this->getTime());
144+
$events = $this->getMockedLifecycle()->getEvents();
145+
$this->assertEquals(1, sizeof($events));
146+
$this->assertInstanceOf('\Yandex\Allure\Adapter\Event\TestCaseFinishedEvent', $events[0]);
147+
}
148+
149+
private function getMockedLifecycle()
150+
{
151+
return $this->mockedLifecycle;
152+
}
153+
154+
private function getAllureAdapter()
155+
{
156+
return $this->allureAdapter;
157+
}
158+
159+
private function getException()
160+
{
161+
return new Exception(EXCEPTION_MESSAGE);
162+
}
163+
164+
private function getTime()
165+
{
166+
return (float)time();
167+
}
168+
169+
private function getTestSuite()
170+
{
171+
return new \PHPUnit_Framework_TestSuite(__CLASS__);
172+
}
173+
174+
}

0 commit comments

Comments
 (0)