Skip to content

Commit

Permalink
Add tests for some example code
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark committed Apr 10, 2018
1 parent ea3f7b2 commit a823c7e
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ script:
- packr
- go test -v ./...
- ./gen.sh
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then docker build -t twirphp . && docker run --rm -it twirphp clientcompat -client clientcompat/compat.sh; fi
- if [[ $TRAVIS_OS_NAME == "linux" ]]; then docker build -t twirphp . && docker run --rm -it twirphp clientcompat -client clientcompat/compat.sh && docker run --rm -it twirphp vendorphp/bin/phpunit -v && docker run --rm -it twirphp vendorphp/bin/phpunit -v --group example; fi

deploy:
provider: script
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"autoload-dev": {
"psr-4": {
"Tests\\Twirp\\": "php/tests/",
"": ["clientcompat/generated/", "example/generated/", "example/src/"]
"": ["clientcompat/generated/", "example/generated/", "example/src/"],
"Tests\\Twitch\\Twirp\\Example\\": "example/tests/"
}
},
"config": {
Expand Down
89 changes: 89 additions & 0 deletions example/tests/TwirpErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

namespace Tests\Twitch\Twirp\Example;

use Twirp\ErrorCode;
use Twitch\Twirp\Example\TwirpError;

/**
* @group example
*/
final class TwirpErrorTest extends \PHPUnit\Framework\TestCase
{
/**
* @test
*/
public function it_has_a_code()
{
$error = new TwirpError('code', 'msg');

$this->assertEquals('code', $error->code());
}

/**
* @test
*/
public function it_has_a_message()
{
$error = new TwirpError('code', 'msg');

$this->assertEquals('msg', $error->msg());
}

/**
* @test
*/
public function it_has_metadata()
{
$error = new TwirpError('code', 'msg');
$error->withMeta('key', 'value');

$this->assertEquals('value', $error->meta('key'));
$this->assertEquals('', $error->meta('invalid_key'));
}

/**
* @test
*/
public function it_has_a_map_of_metadata()
{
$error = new TwirpError('code', 'msg');
$error->withMeta('key', 'value');

$this->assertEquals(['key' => 'value'], $error->metaMap());
}

/**
* @test
*/
public function it_creates_a_new_error()
{
$error = TwirpError::newError(ErrorCode::Unauthenticated, 'msg');

$this->assertEquals(ErrorCode::Unauthenticated, $error->code());
$this->assertEquals('msg', $error->msg());
}

/**
* @test
*/
public function it_creates_an_internal_error_when_code_is_invalid()
{
$error = TwirpError::newError('code', 'msg');

$this->assertEquals(ErrorCode::Internal, $error->code());
$this->assertEquals('invalid error type code', $error->msg());
}

/**
* @test
*/
public function it_creates_an_error_from_an_exception()
{
$exception = new \Exception('msg');
$error = TwirpError::errorFromException($exception);

$this->assertEquals(ErrorCode::Internal, $error->code());
$this->assertEquals('msg', $error->msg());
}
}
9 changes: 9 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<testsuite name="Twirp Test Suite">
<directory>./php/tests/</directory>
</testsuite>
<testsuite name="Twirp Example Test Suite">
<directory>./example/tests/</directory>
</testsuite>
</testsuites>

<filter>
Expand All @@ -19,4 +22,10 @@
</exclude>
</whitelist>
</filter>

<groups>
<exclude>
<group>example</group>
</exclude>
</groups>
</phpunit>

0 comments on commit a823c7e

Please sign in to comment.