Skip to content

Commit

Permalink
More tidy test code
Browse files Browse the repository at this point in the history
  • Loading branch information
Finesse committed Dec 4, 2017
1 parent 4cbd39d commit f091732
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 106 deletions.
6 changes: 5 additions & 1 deletion src/Services/WebfontCSSGenerator/WebfontCSSGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public function __construct(array $families = [], string $rootURL = '')
{
foreach ($families as $index => $family) {
if (!($family instanceof Family)) {
throw new \InvalidArgumentException('Argument $families['.$index.'] expected to be a Family instance, '.gettype($family).' given.');
throw new \InvalidArgumentException(sprintf(
'Argument $families[%s] expected to be a Family instance, %s given.',
$index,
is_object($family) ? 'a '.get_class($family).' instance' : gettype($family)
));
}

$this->families[$family->name] = $family;
Expand Down
55 changes: 54 additions & 1 deletion tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,67 @@ protected function tearDown()
\Mockery::close();
}

/**
* Asserts that one CSS code is semantically equal to the other CSS code.
*
* @param string $expected
* @param string $actual
* @param string $message
*/
protected function assertCSSEquals(string $expected, string $actual, string $message = '')
{
$this->assertEquals(
$this->removeUnnecessaryCharsFromCSS($expected),
$this->removeUnnecessaryCharsFromCSS($actual),
$message
);
}

/**
* Removes unnecessary characters from CSS text for more convenient comparison.
*
* @param string $css
* @return string
*/
protected static function removeUnnecessaryCharsFromCSS(string $css): string
protected function removeUnnecessaryCharsFromCSS(string $css): string
{
return preg_replace('/[\s]+/', '', $css);
}

/**
* Asserts that the given callback throws the given exception.
*
* @param string $expectClass The name of the expected exception class
* @param callable $callback A callback which should throw the exception
* @param callable|null $onException A function to call after exception check. It may be used to test the exception.
*/
protected function assertException(string $expectClass, callable $callback, callable $onException = null)
{
try {
$callback();
} catch (\Throwable $exception) {
$this->assertInstanceOf($expectClass, $exception);
if ($onException) {
$onException($exception);
}
return;
}

$this->fail('No exception has been thrown');
}

/**
* Asserts that the given object has the given attributes with the given values.
*
* @param array $expectedAttributes Attributes. The indexes are the attributes names, the values are the attributes
* values.
* @param mixed $actualObject Object
*/
protected function assertAttributes(array $expectedAttributes, $actualObject)
{
foreach ($expectedAttributes as $property => $value) {
$this->assertObjectHasAttribute($property, $actualObject);
$this->assertAttributeEquals($value, $property, $actualObject);
}
}
}
50 changes: 26 additions & 24 deletions tests/Unit/WebfontCSSGenerator/FamilyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@

class FamilyTest extends BaseTestCase
{
public function testIncorrectDirectory()
{
$this->expectException(InvalidSettingsException::class);
Family::createFromSettings('test', [
'directory' => ['foo', 'bar']
]);
}

public function testIncorrectStyles()
{
$this->expectException(InvalidSettingsException::class);
Family::createFromSettings('test', [
'styles' => '1234'
]);
}

public function testCreateFromSettings()
{
$family = Family::createFromSettings('Foo bar', [
Expand All @@ -36,11 +20,13 @@ public function testCreateFromSettings()
'600' => 'font_semibold.*'
]
]);
$this->assertAttributeEquals('Foo bar', 'name', $family);
$this->assertAttributeEquals(true, 'forbidLocalSource', $family);
$this->assertAttributeEquals('example/dir', 'directory', $family);
$this->assertAttributeCount(3, 'styles', $family);
$this->assertAttributes([
'name' => 'Foo bar',
'forbidLocalSource' => true,
'directory' => 'example/dir'
], $family);;
$this->assertAttributeInternalType('array', 'styles', $family);
$this->assertAttributeCount(3, 'styles', $family);
$this->assertArrayHasKey('400i', $family->styles);
$this->assertInstanceOf(Style::class, $family->styles['400i']);

Expand All @@ -51,13 +37,29 @@ public function testCreateFromSettings()
'700' => 'font_bold.*'
]
]);
$this->assertAttributeEquals('Foo bar 2', 'name', $family);
$this->assertAttributeEquals(null, 'forbidLocalSource', $family);
$this->assertAttributeEquals(null, 'directory', $family);
$this->assertAttributeCount(3, 'styles', $family);
$this->assertAttributes([
'name' => 'Foo bar 2',
'forbidLocalSource' => null,
'directory' => null,
], $family);
$this->assertAttributeInternalType('array', 'styles', $family);
$this->assertAttributeCount(3, 'styles', $family);
$this->assertArrayHasKey('700', $family->styles);
$this->assertInstanceOf(Style::class, $family->styles['700']);

// Incorrect directory
$this->assertException(InvalidSettingsException::class, function () {
Family::createFromSettings('test', [
'directory' => ['foo', 'bar']
]);
});

// Incorrect styles
$this->assertException(InvalidSettingsException::class, function () {
Family::createFromSettings('test', [
'styles' => '1234'
]);
});
}

public function testGetStyle()
Expand Down
158 changes: 88 additions & 70 deletions tests/Unit/WebfontCSSGenerator/StyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,6 @@

class StyleTest extends BaseTestCase
{
public function testIncorrectName()
{
$this->expectException(InvalidSettingsException::class);
$this->expectExceptionMessage('The style name `foo` has invalid format.');
Style::createFromSettings('foo', 'font.*');
}

public function testIncorrectSettingsArgumentType()
{
$this->expectException(InvalidSettingsException::class);
$this->expectExceptionMessage('The $settings argument must be array or string, integer given.');
Style::createFromSettings('400', 12345);
}

public function testIncorrectDirectory()
{
$this->expectException(InvalidSettingsException::class);
$this->expectExceptionMessage('The $settings[directory] argument must be string or null, array given.');
Style::createFromSettings('400', [
'directory' => ['foo', 'bar'],
'files' => 'font.*'
]);
}

public function testIncorrectFilesSetting()
{
$this->expectException(InvalidSettingsException::class);
$this->expectExceptionMessage('The $settings[files] argument must be array or string, object given.');
Style::createFromSettings('400', [
'directory' => 'dir',
'files' => new \StdClass()
]);
}

public function testIncorrectFileSettings()
{
$this->expectException(InvalidSettingsException::class);
$this->expectExceptionMessage('The $settings[files][0] argument must be array or string, boolean given.');
Style::createFromSettings('400', [
'directory' => 'dir',
'files' => [true, 'file']
]);
}

public function testCreateFromSettings()
{
$style = Style::createFromSettings('500i', [
Expand All @@ -65,36 +21,98 @@ public function testCreateFromSettings()
'\\subdir\\font_medium.svg'
]
]);
$this->assertAttributeEquals(500, 'weight', $style);
$this->assertAttributeEquals(true, 'isItalic', $style);
$this->assertAttributeEquals(false, 'forbidLocalSource', $style);
$this->assertAttributeEquals('style/directory', 'directory', $style);
$this->assertAttributeEquals([
'subdir/font_medium.eot',
'subdir/font_medium.ttf',
'subdir/font_medium.woff',
'subdir/font_medium.woff2',
'subdir/font_medium.svg'
], 'filesList', $style);
$this->assertAttributeEquals(null, 'filesGlob', $style);
$this->assertAttributes([
'weight' => 500,
'isItalic' => true,
'forbidLocalSource' => false,
'directory' => 'style/directory',
'filesList' => [
'subdir/font_medium.eot',
'subdir/font_medium.ttf',
'subdir/font_medium.woff',
'subdir/font_medium.woff2',
'subdir/font_medium.svg'
],
'filesGlob' => null
], $style);

$style = Style::createFromSettings(800, [
'files' => '\\subdir\\font_heavy.*'
]);
$this->assertAttributeEquals(800, 'weight', $style);
$this->assertAttributeEquals(false, 'isItalic', $style);
$this->assertAttributeEquals(null, 'forbidLocalSource', $style);
$this->assertAttributeEquals(null, 'directory', $style);
$this->assertAttributeEquals([], 'filesList', $style);
$this->assertAttributeEquals('subdir/font_heavy.*', 'filesGlob', $style);
$this->assertAttributes([
'weight' => 800,
'isItalic' => false,
'forbidLocalSource' => null,
'directory' => null,
'filesList' => [],
'filesGlob' => 'subdir/font_heavy.*'
], $style);

$style = Style::createFromSettings('100i', 'subdir\\font_thin.*');
$this->assertAttributeEquals(100, 'weight', $style);
$this->assertAttributeEquals(true, 'isItalic', $style);
$this->assertAttributeEquals(null, 'forbidLocalSource', $style);
$this->assertAttributeEquals(null, 'directory', $style);
$this->assertAttributeEquals([], 'filesList', $style);
$this->assertAttributeEquals('subdir/font_thin.*', 'filesGlob', $style);
$this->assertAttributes([
'weight' => 100,
'isItalic' => true,
'forbidLocalSource' => null,
'directory' => null,
'filesList' => [],
'filesGlob' => 'subdir/font_thin.*'
], $style);

// Incorrect name
$this->assertException(InvalidSettingsException::class, function () {
Style::createFromSettings('foo', 'font.*');
}, function (\Throwable $exception) {
$this->assertEquals('The style name `foo` has invalid format.', $exception->getMessage());
});

// Incorrect settings argument type
$this->assertException(InvalidSettingsException::class, function () {
Style::createFromSettings('400', 12345);
}, function (\Throwable $exception) {
$this->assertEquals(
'The $settings argument must be array or string, integer given.',
$exception->getMessage()
);
});

// Incorrect directory
$this->assertException(InvalidSettingsException::class, function () {
Style::createFromSettings('400', [
'directory' => ['foo', 'bar'],
'files' => 'font.*'
]);
}, function (\Throwable $exception) {
$this->assertEquals(
'The $settings[directory] argument must be string or null, array given.',
$exception->getMessage()
);
});

// Incorrect files setting
$this->assertException(InvalidSettingsException::class, function () {
Style::createFromSettings('400', [
'directory' => 'dir',
'files' => new \StdClass()
]);
}, function (\Throwable $exception) {
$this->assertEquals(
'The $settings[files] argument must be array or string, object given.',
$exception->getMessage()
);
});

// Incorrect file settings
$this->assertException(InvalidSettingsException::class, function () {
Style::createFromSettings('400', [
'directory' => 'dir',
'files' => [true, 'file']
]);
}, function (\Throwable $exception) {
$this->assertEquals(
'The $settings[files][0] argument must be array or string, boolean given.',
$exception->getMessage()
);
});
}

public function testGetName()
Expand Down Expand Up @@ -136,13 +154,13 @@ public function testGetFilesInDirectory()
'test-font.woff2',
'test-font.eot',
'test-font.svg'
], $style->getFilesInDirectory($testDirectory), "\$canonicalize = true", $delta = 0.0, $maxDepth = 10, $canonicalize = true);
], $style->getFilesInDirectory($testDirectory), '', 0.0, 10, true);
} finally {
@unlink($testDirectory.'/test-font.ttf');
@unlink($testDirectory.'/test-font.woff2');
@unlink($testDirectory.'/test-font.eot');
@unlink($testDirectory.'/test-font.svg');
rmdir($testDirectory);
@rmdir($testDirectory);
}
}
}
Loading

0 comments on commit f091732

Please # to comment.