Skip to content
11 changes: 6 additions & 5 deletions tests/Factory/SentryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Consistence\Sentry\Factory;

use Consistence\Sentry\Metadata\SentryIdentificator;
use PHPUnit\Framework\Assert;

class SentryFactoryTest extends \PHPUnit\Framework\TestCase
{
Expand All @@ -13,7 +14,7 @@ public function testGetSentry(): void
{
$factory = $this->createMock(SentryFactory::class);
$factory
->expects($this->once())
->expects(self::once())
->method('getSentry');

$factory->getSentry(new SentryIdentificator('string'));
Expand All @@ -24,15 +25,15 @@ public function testNoSentry(): void
$sentryIdentificator = new SentryIdentificator('string');
$factory = $this->createMock(SentryFactory::class);
$factory
->expects($this->once())
->expects(self::once())
->method('getSentry')
->will($this->throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator)));
->will(self::throwException(new \Consistence\Sentry\Factory\NoSentryForIdentificatorException($sentryIdentificator)));

try {
$factory->getSentry($sentryIdentificator);
$this->fail();
Assert::fail('Exception expected');
} catch (\Consistence\Sentry\Factory\NoSentryForIdentificatorException $e) {
$this->assertSame($sentryIdentificator, $e->getSentryIdentificator());
Assert::assertSame($sentryIdentificator, $e->getSentryIdentificator());
}
}

Expand Down
127 changes: 94 additions & 33 deletions tests/Factory/Simple/SimpleSentryFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,132 @@
use Consistence\Sentry\Type\CollectionType;
use Consistence\Sentry\Type\Sentry;
use Consistence\Sentry\Type\SimpleType;
use Generator;
use PHPUnit\Framework\Assert;

class SimpleSentryFactoryTest extends \PHPUnit\Framework\TestCase
{

/**
* @return mixed[][]
* @return mixed[][]|\Generator
*/
public function sentryIdentificatorToSentryProvider(): array
public function sentryIdentificatorToSentryDataProvider(): Generator
{
return [
[new SentryIdentificator('string'), new SimpleType()],
[new SentryIdentificator('int'), new SimpleType()],
[new SentryIdentificator('bool'), new SimpleType()],
[new SentryIdentificator('float'), new SimpleType()],
[new SentryIdentificator('integer'), new SimpleType()],
[new SentryIdentificator('boolean'), new SimpleType()],
[new SentryIdentificator('mixed'), new SimpleType()],
[new SentryIdentificator('DateTimeImmutable'), new SimpleType()],
[new SentryIdentificator('string[]'), new CollectionType()],
[new SentryIdentificator('int[]'), new CollectionType()],
[new SentryIdentificator('bool[]'), new CollectionType()],
[new SentryIdentificator('float[]'), new CollectionType()],
[new SentryIdentificator('integer[]'), new CollectionType()],
[new SentryIdentificator('boolean[]'), new CollectionType()],
[new SentryIdentificator('mixed[]'), new CollectionType()],
[new SentryIdentificator('DateTimeImmutable[]'), new CollectionType()],
yield 'string' => [
'sentryIdentificator' => new SentryIdentificator('string'),
'sentry' => new SimpleType(),
];
yield 'int' => [
'sentryIdentificator' => new SentryIdentificator('int'),
'sentry' => new SimpleType(),
];
yield 'bool' => [
'sentryIdentificator' => new SentryIdentificator('bool'),
'sentry' => new SimpleType(),
];
yield 'float' => [
'sentryIdentificator' => new SentryIdentificator('float'),
'sentry' => new SimpleType(),
];
yield 'integer' => [
'sentryIdentificator' => new SentryIdentificator('integer'),
'sentry' => new SimpleType(),
];
yield 'boolean' => [
'sentryIdentificator' => new SentryIdentificator('boolean'),
'sentry' => new SimpleType(),
];
yield 'mixed' => [
'sentryIdentificator' => new SentryIdentificator('mixed'),
'sentry' => new SimpleType(),
];
yield 'object' => [
'sentryIdentificator' => new SentryIdentificator('DateTimeImmutable'),
'sentry' => new SimpleType(),
];
yield 'string array' => [
'sentryIdentificator' => new SentryIdentificator('string[]'),
'sentry' => new CollectionType(),
];
yield 'int array' => [
'sentryIdentificator' => new SentryIdentificator('int[]'),
'sentry' => new CollectionType(),
];
yield 'bool array' => [
'sentryIdentificator' => new SentryIdentificator('bool[]'),
'sentry' => new CollectionType(),
];
yield 'float array' => [
'sentryIdentificator' => new SentryIdentificator('float[]'),
'sentry' => new CollectionType(),
];
yield 'integer array' => [
'sentryIdentificator' => new SentryIdentificator('integer[]'),
'sentry' => new CollectionType(),
];
yield 'boolean array' => [
'sentryIdentificator' => new SentryIdentificator('boolean[]'),
'sentry' => new CollectionType(),
];
yield 'mixed array' => [
'sentryIdentificator' => new SentryIdentificator('mixed[]'),
'sentry' => new CollectionType(),
];
yield 'object array' => [
'sentryIdentificator' => new SentryIdentificator('DateTimeImmutable[]'),
'sentry' => new CollectionType(),
];
}

/**
* @dataProvider sentryIdentificatorToSentryProvider
* @dataProvider sentryIdentificatorToSentryDataProvider
*
* @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator
* @param \Consistence\Sentry\Type\Sentry $sentry
*/
public function testGetSentry(SentryIdentificator $sentryIdentificator, Sentry $sentry): void
{
$factory = new SimpleSentryFactory(new SentryIdentificatorParser());
$this->assertTrue($factory->getSentry($sentryIdentificator) instanceof $sentry);
Assert::assertTrue($factory->getSentry($sentryIdentificator) instanceof $sentry);
}

public function testSameSentryInstance(): void
{
$factory = new SimpleSentryFactory(new SentryIdentificatorParser());
$sentry = $factory->getSentry(new SentryIdentificator('string'));
$this->assertSame($sentry, $factory->getSentry(new SentryIdentificator('string')));
Assert::assertSame($sentry, $factory->getSentry(new SentryIdentificator('string')));
}

public function testNoSentry(): void
/**
* @return mixed[][]|\Generator
*/
public function getSentryWhenNoSentryForIdentificatorDataProvider(): Generator
{
$factory = new SimpleSentryFactory(new SentryIdentificatorParser());

$this->expectException(\Consistence\Sentry\Factory\NoSentryForIdentificatorException::class);

$factory->getSentry(new SentryIdentificator(''));
yield 'empty string' => [
'sentryIdentificator' => new SentryIdentificator(''),
];
yield 'nonexistent object' => [
'sentryIdentificator' => new SentryIdentificator('Foo\Bar'),
];
}

public function testNonexistingObject(): void
/**
* @dataProvider getSentryWhenNoSentryForIdentificatorDataProvider
*
* @param \Consistence\Sentry\Metadata\SentryIdentificator $sentryIdentificator
*/
public function testGetSentryWhenNoSentryForIdentificator(
SentryIdentificator $sentryIdentificator
): void
{
$factory = new SimpleSentryFactory(new SentryIdentificatorParser());

$this->expectException(\Consistence\Sentry\Factory\NoSentryForIdentificatorException::class);
$this->expectExceptionMessage('Foo\Bar');

$factory->getSentry(new SentryIdentificator('Foo\Bar'));
try {
$factory->getSentry($sentryIdentificator);
Assert::fail('Exception expected');
} catch (\Consistence\Sentry\Factory\NoSentryForIdentificatorException $e) {
Assert::assertSame($sentryIdentificator, $e->getSentryIdentificator());
}
}

}
11 changes: 6 additions & 5 deletions tests/Generated/SentryAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Consistence\Sentry\Generated;

use PHPUnit\Framework\Assert;
use org\bovigo\vfs\vfsStream;

class SentryAutoloaderTest extends \PHPUnit\Framework\TestCase
Expand All @@ -21,19 +22,19 @@ public function testRebuild(): void

$generator = $this->createMock(SentryGenerator::class);
$generator
->expects($this->once())
->expects(self::once())
->method('generateAll')
->will($this->returnValue($classMap));
->will(self::returnValue($classMap));

$autoloader = new SentryAutoloader($generator, $targetPath);

$this->assertFalse($autoloader->isClassMapReady());
Assert::assertFalse($autoloader->isClassMapReady());

$autoloader->rebuild();

$this->assertTrue($autoloader->isClassMapReady());
Assert::assertTrue($autoloader->isClassMapReady());

$this->assertEquals($classMap, require $targetPath);
Assert::assertEquals($classMap, require $targetPath);
}

}
Loading