Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
- Simplify Travis tests
  - Remove usage of PHPUnit groups which have been removed in nelmio#583
  - Run coverage only for unit tests
- Fix coverage configuration
- Rework a few tests
  • Loading branch information
theofidry committed Oct 29, 2016
1 parent df67278 commit 2d3e9e8
Show file tree
Hide file tree
Showing 34 changed files with 616 additions and 138 deletions.
17 changes: 5 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ matrix:
- php: '7.0'
env: COVERAGE='true'
- php: '7.0'
env: SYMFONY_VERSION='^3.0'
env:
- COVERAGE='true'
- SYMFONY_VERSION='^3.0'
- php: '7.0'
env: SYMFONY_VERSION='^3.1'
- php: '7.0'
Expand Down Expand Up @@ -47,18 +49,9 @@ install:
script:
- |
if [ -n "$COVERAGE" ]; then
phpdbg -qrr bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --testdox --coverage-text --exclude-group=integration,symfony
if [ -n "$SYMFONY_VERSION" ]; then
phpdbg -qrr bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --coverage-text --group=symfony
else
phpdbg -qrr bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --coverage-text --group=integration --exclude-group=symfony
fi
phpdbg -qrr bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --testdox --coverage-text --exclude-group=integration
else
if [ -n "$SYMFONY_VERSION" ]; then
bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --group=symfony
else
bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS --exclude-group=symfony
fi
bin/phpunit -c $PHPUNIT_CONFIG $PHPUNIT_FLAGS
fi
notifications:
Expand Down
4 changes: 3 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@
<filter>
<whitelist>
<directory>src</directory>
<exclude>src/Bridge</exclude>
<exclude>
<directory>src/Bridge</directory>
</exclude>
</whitelist>
</filter>

Expand Down
11 changes: 10 additions & 1 deletion src/Definition/Value/FixtureReferenceValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@ final class FixtureReferenceValue implements ValueInterface
public function __construct($reference)
{
if (false === is_string($reference) && false === $reference instanceof ValueInterface) {
if (null === $reference) {
$referenceString = 'null';
} elseif (is_array($reference)) {
$referenceString = 'array';
} else {
$referenceString = is_scalar($reference) ? gettype($reference) : get_class($reference);
}

//TODO: move those exceptions into factories
throw new \InvalidArgumentException(
sprintf(
'Expected reference to be either a string or a "%s" instance, got "%s" instead.',
ValueInterface::class,
is_scalar($reference) ? gettype($reference) : get_class($reference)
$referenceString
)
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Definition/Value/ValueForCurrentValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Nelmio\Alice\Definition\ValueInterface;

/**
* Value object representing '<current()>'.
* Value object placeholder for '<current()>'.
*/
final class ValueForCurrentValue implements ValueInterface
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,6 @@ private function denormalizeTemporaryFixture(
FlagBag $flags
): array
{
if (null === $this->denormalizer) {
throw DenormalizerNotFoundException::createUnexpectedCall(__METHOD__);
}

$tempFixtureId = uniqid('temporary_id');
$builtFixtures = $this->denormalizer->denormalize(
$builtFixtures,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ public function parse(Token $token)
{
$range = $this->buildRange($token);
$references = [];
for ($currentIndex = $range->getFrom(); $currentIndex <= $range->getTo(); $currentIndex++) {
$from = $range->getFrom();
$to = $range->getTo();
for ($currentIndex = $from; $currentIndex <= $to; $currentIndex++) {
$fixtureId = str_replace($this->token, $currentIndex, $range->getName());
$references[] = new FixtureReferenceValue($fixtureId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public function parse(Token $token)
{
$value = $this->tokenizer->detokenize($token->getValue());
$realValue = preg_replace('/^<\((.*)\)>$/', '<identity($1)>', $value);
if (null === $realValue) {
throw ParseException::createForToken($token);
}

return $this->decoratedTokenParser->parse(
new Token($realValue, new TokenType(TokenType::FUNCTION_TYPE))
Expand Down
50 changes: 0 additions & 50 deletions src/Generator/ResolverGenerator.php

This file was deleted.

1 change: 1 addition & 0 deletions src/NotClonableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trait NotClonableTrait
{
public function __clone()
{
//TODO: throw a custom exception
throw new \DomainException(
'This class is not clonable. This could be the case because this has not been needed yet. Do not hesitate '
.'to reach out the maintainers to know if this can be made clonable.'
Expand Down
2 changes: 2 additions & 0 deletions tests/Definition/Fixture/TemplatingFixtureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Nelmio\Alice\Definition\Fixture;

use Nelmio\Alice\Definition\FakeMethodCall;
use Nelmio\Alice\Definition\Flag\DummyFlag;
use Nelmio\Alice\Definition\Flag\ExtendFlag;
use Nelmio\Alice\Definition\Flag\TemplateFlag;
use Nelmio\Alice\Definition\FlagBag;
Expand Down Expand Up @@ -65,6 +66,7 @@ public function testReadAccessorsReturnPropertiesValues()
$this->assertTrue($fixture->isATemplate());
$this->assertTrue($fixture->extendsFixtures());
$this->assertEquals([new FixtureReference('user_base')], $fixture->getExtendedFixturesReferences());
$this->assertEquals($flags, $fixture->getFlags());

$decoratedFixtureProphecy->getId()->shouldHaveBeenCalledTimes(2);
$decoratedFixtureProphecy->getClassName()->shouldHaveBeenCalledTimes(1);
Expand Down
76 changes: 76 additions & 0 deletions tests/Definition/Value/FixtureReferenceValueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,82 @@ public function testIsAValue()
$this->assertTrue(is_a(FixtureReferenceValue::class, ValueInterface::class, true));
}

public function testCanBeInstantiatedWithOnlyAStringOrAValue()
{
new FixtureReferenceValue('user0');
new FixtureReferenceValue(new FakeValue());

try {
new FixtureReferenceValue(null);
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "null" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue(true);
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "boolean" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue(10);
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "integer" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue(.5);
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "double" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue([]);
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "array" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue(new \stdClass());
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "stdClass" instead.',
$exception->getMessage()
);
}

try {
new FixtureReferenceValue(function () {});
} catch (\InvalidArgumentException $exception) {
$this->assertEquals(
'Expected reference to be either a string or a "Nelmio\Alice\Definition\ValueInterface" instance, got'
.' "Closure" instead.',
$exception->getMessage()
);
}
}

public function testReadAccessorsReturnPropertiesValues()
{
$value = new FixtureReferenceValue('user0');
Expand Down
39 changes: 39 additions & 0 deletions tests/Definition/Value/ValueForCurrentValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <hello@nelm.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\Definition\Value;

use Nelmio\Alice\Definition\ValueInterface;

/**
* @covers \Nelmio\Alice\Definition\Value\ValueForCurrentValue
*/
class ValueForCurrentValueTest extends \PHPUnit_Framework_TestCase
{
public function testIsAValue()
{
$this->assertTrue(is_a(ValueForCurrentValue::class, ValueInterface::class, true));
}

public function testReadAccessorsReturnPropertiesValues()
{
$value = new ValueForCurrentValue();
$this->assertEquals('current', $value->getValue());
}

public function testCanBeCastedIntoAString()
{
$value = new ValueForCurrentValue();
$this->assertEquals('current', $value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ public function testTestCreateNewExceptionWithFactoryForFixture()
'No suitable fixture denormalizer found to handle the fixture with the reference "foo".',
$exception->getMessage()
);
$this->assertEquals(0, $exception->getCode());
$this->assertNull($exception->getPrevious());


$code = 500;
$previous = new \Error('hello');

$exception = DenormalizerNotFoundException::createForFixture('foo', $code, $previous);
$this->assertEquals(
'No suitable fixture denormalizer found to handle the fixture with the reference "foo".',
$exception->getMessage()
);
$this->assertEquals($code, $exception->getCode());
$this->assertSame($previous, $exception->getPrevious());
}

public function testTestCreateNewExceptionWithFactoryForUnexpectedCall()
Expand All @@ -48,5 +62,32 @@ public function testTestCreateNewExceptionWithFactoryForUnexpectedCall()
'Expected method "fake" to be called only if it has a denormalizer.',
$exception->getMessage()
);
$this->assertEquals(0, $exception->getCode());
$this->assertNull($exception->getPrevious());


$code = 500;
$previous = new \Error('hello');

$exception = DenormalizerNotFoundException::createUnexpectedCall('fake', $code, $previous);
$this->assertEquals(
'Expected method "fake" to be called only if it has a denormalizer.',
$exception->getMessage()
);
$this->assertEquals($code, $exception->getCode());
$this->assertSame($previous, $exception->getPrevious());
}

public function testIsExtensible()
{
$exception = ChildDenormalizerNotFoundException::createForFixture('foo');
$this->assertInstanceOf(ChildDenormalizerNotFoundException::class, $exception);

$exception = ChildDenormalizerNotFoundException::createUnexpectedCall('fake');
$this->assertInstanceOf(ChildDenormalizerNotFoundException::class, $exception);
}
}

class ChildDenormalizerNotFoundException extends DenormalizerNotFoundException
{
}
Loading

0 comments on commit 2d3e9e8

Please sign in to comment.