Skip to content

Commit

Permalink
add test setup and phpcs check to circle ci (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Apr 11, 2018
1 parent e19b1a1 commit f975ddd
Show file tree
Hide file tree
Showing 11 changed files with 367 additions and 10 deletions.
22 changes: 22 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 2

jobs:
build:
docker:
- image: circleci/php:5.6-node-browsers

steps:
- checkout
- run: sudo composer self-update
- restore_cache:
keys:
- composer-v1-{{ checksum "composer.json" }}
- composer-v1-
- run: composer install -n --prefer-dist
- save_cache:
key: composer-v1-{{ checksum "composer.json" }}
paths:
- vendor
- ~/.composer/cache
- run: php -d memory_limit=2G ./vendor/bin/phpunit
- run: php -n -d memory_limit=2G ./vendor/bin/php-cs-fixer fix --verbose --diff --diff-format=udiff --dry-run
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github export-ignore
.circleci export-ignore
.gitignore export-ignore
.php_cs export-ignore
.gitattributes export-ignore
/tests export-ignore
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
/composer.phar
/vendor

# Cache
/.php_cs.cache

# IDEs
/.idea
*.iml
*~

# System files
.DS_Store
*.swp
21 changes: 21 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'ordered_imports' => true,
'concat_space' => ['spacing' => 'one'],
'array_syntax' => ['syntax' => 'short'],
'php_unit_construct' => true,
'phpdoc_align' => false,
'class_definition' => [
'multiLineExtendsEachSingleLine' => true,
]
])
->setFinder(
PhpCsFixer\Finder::create()
->exclude('Resources')
->exclude('vendor')
->in(__DIR__)
);
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
"twig/twig": "^1.11 || ^2.0"
},
"require-dev": {
"symfony/property-access": "^2.8 || ^3.0 || ^4.0"
"phpunit/phpunit": "^5.0 || ^6.0 || ^7.0",
"symfony/property-access": "^2.8 || ^3.0 || ^4.0",
"friendsofphp/php-cs-fixer": "^2.11"
},
"suggest": {
"symfony/property-access": "The ImageTwigExtension requires the symfony/property-access service."
Expand Down
6 changes: 3 additions & 3 deletions docs/image.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ This could be:

```twig
<img alt="Logo"
title="Logo"
title="Description"
src="/uploads/media/sulu-400x400/01/image.jpg?v=1-0"
srcset="/uploads/media/sulu-400x400/01/image.jpg?v=1-0 1024w, /uploads/media/sulu-170x170/01/image.jpg?v=1-0 800w, /uploads/media/sulu-100x100/01/image.jpg?v=1-0 460w"
sizes="(max-width: 1024px) 100vw, (max-width: 800px) 100vw, 100vw"
Expand Down Expand Up @@ -88,8 +88,8 @@ This could be:
<source media="(max-width: 650px)"
srcset="/uploads/media/sulu-400x400/01/image.jpg?v=1-0 1024w, /uploads/media/sulu-170x170/01/image.jpg?v=1-0 800w, /uploads/media/sulu-100x100/01/image.jpg?v=1-0 460w"
sizes="(max-width: 1024px) 100vw, (max-width: 800px) 100vw, 100vw">
<img alt="cody_redwoodsa-X2"
title="cody_redwoodsa-X2"
<img alt="Title"
title="Description"
src="/uploads/media/sulu-400x400/01/image.jpg?v=1-0"
class="image-class">
</picture>
Expand Down
8 changes: 8 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit colors="true" bootstrap="./vendor/autoload.php">
<testsuites>
<testsuite name="Massive Twig Extensions">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/ComponentTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function getComponents($jsonEncode = true, $clear = true)
*
* @param bool $jsonEncode
*
* @return string.
* @return string
*/
public function getComponentList($jsonEncode = false)
{
Expand Down Expand Up @@ -156,7 +156,7 @@ public function getServices($jsonEncode = true, $clear = true)
*
* @param bool $jsonEncode
*
* @return string.
* @return string
*/
public function getServiceList($jsonEncode = false)
{
Expand Down
14 changes: 10 additions & 4 deletions src/ImageTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ public function getImage($media, $attributes = [], $sources = [])
return '';
}

if (is_array($media)) {
$media = (object) $media;
}

$propertyAccessor = PropertyAccess::createPropertyAccessor();

// Thumbnails exists all times - it only can be empty.
Expand Down Expand Up @@ -63,17 +67,19 @@ public function getImage($media, $attributes = [], $sources = [])
$sourceTags = '';
foreach ($sources as $media => $sourceAttributes) {
// Get the source tag with all given attributes.
$sourceTags .= $this->createTag('source', array_merge(['media' => $media], $sourceAttributes), $thumbnails);
$sourceTags .= $this->createTag('source', array_merge(['media' => $media], $sourceAttributes), $thumbnails);
}

// Returns the picture tag with all sources and the fallback image tag.
return sprintf('<picture>%s%s</picture>', $sourceTags, $imgTag);
}

/**
* @param $tag
* @param $attributes
* @param $thumbnails
* Create html tag.
*
* @param string $tag
* @param array $attributes
* @param array $thumbnails
*
* @return string
*/
Expand Down
182 changes: 182 additions & 0 deletions tests/ComponentTwigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
<?php

use Massive\Component\Web\ComponentTwigExtension;
use PHPUnit\Framework\TestCase;

class ComponentTwigExtensionTest extends TestCase
{
/**
* @var ComponentTwigExtension
*/
private $componentTwigExtension;

public function setup()
{
$this->componentTwigExtension = new ComponentTwigExtension();
}

public function testRegisterComponent()
{
$this->assertEquals('test-1', $this->componentTwigExtension->registerComponent('test'));

$this->assertEquals(
json_encode([
[
'name' => 'test',
'id' => 'test-1',
'options' => new stdClass(),
],
]),
$this->componentTwigExtension->getComponents()
);
}

public function testRegisterMultipleComponent()
{
$this->assertEquals('test-1', $this->componentTwigExtension->registerComponent('test'));
$this->assertEquals('test-2', $this->componentTwigExtension->registerComponent('test'));

$this->assertEquals(
json_encode([
[
'name' => 'test',
'id' => 'test-1',
'options' => new stdClass(),
],
[
'name' => 'test',
'id' => 'test-2',
'options' => new stdClass(),
],
]),
$this->componentTwigExtension->getComponents()
);
}

public function testRegisterCustomIdComponent()
{
$this->assertEquals('custom', $this->componentTwigExtension->registerComponent('test', ['id' => 'custom']));

$this->assertEquals(
json_encode([
[
'name' => 'test',
'id' => 'custom',
'options' => (object) ['id' => 'custom'],
],
]),
$this->componentTwigExtension->getComponents()
);
}

public function testRegisterOptionComponent()
{
$this->assertEquals('test-1', $this->componentTwigExtension->registerComponent('test', ['option1' => 'value1', 'option2' => 'value2']));

$this->assertEquals(
json_encode([
[
'name' => 'test',
'id' => 'test-1',
'options' => (object) [
'option1' => 'value1',
'option2' => 'value2',
],
],
]),
$this->componentTwigExtension->getComponents()
);
}

public function testRegisterComponentArray()
{
$this->assertEquals('test-1', $this->componentTwigExtension->registerComponent('test'));

$this->assertEquals(
[
[
'name' => 'test',
'id' => 'test-1',
'options' => new stdClass(),
],
],
$this->componentTwigExtension->getComponents(false)
);
}

public function testRegisterComponentClear()
{
$this->assertEquals('test-1', $this->componentTwigExtension->registerComponent('test'));

// Get components without clearing them.
$this->assertEquals(
[
[
'name' => 'test',
'id' => 'test-1',
'options' => new stdClass(),
],
],
$this->componentTwigExtension->getComponents(false, false)
);

// Get components with clearing.
$this->assertEquals(
[
[
'name' => 'test',
'id' => 'test-1',
'options' => new stdClass(),
],
],
$this->componentTwigExtension->getComponents(false)
);

// Check if cleared correctly.
$this->assertEquals([], $this->componentTwigExtension->getComponents(false));
}

public function testComponentList()
{
$this->componentTwigExtension->registerComponent('test');
$this->componentTwigExtension->registerComponent('test');
$this->componentTwigExtension->registerComponent('test2');
$this->componentTwigExtension->registerComponent('test3');

$componentList = $this->componentTwigExtension->getComponentList();

$this->assertCount(3, $componentList);
$this->assertEquals(['test', 'test2', 'test3'], $componentList);
}

public function testCallService()
{
$this->componentTwigExtension->callService('service', 'function', ['key' => 'value']);

$this->assertEquals(
json_encode([
[
'name' => 'service',
'func' => 'function',
'args' => [
'key' => 'value',
],
],
]),
$this->componentTwigExtension->getServices()
);
}

public function testGetServices()
{
$this->componentTwigExtension->callService('service', 'function', ['key' => 'value']);
$this->componentTwigExtension->callService('service2', 'function', ['key' => 'value']);
$this->componentTwigExtension->callService('service2', 'function', ['key' => 'value']);
$this->componentTwigExtension->callService('service3', 'function', ['key' => 'value']);

$servicesList = $this->componentTwigExtension->getServiceList();

$this->assertCount(3, $servicesList);
$this->assertEquals(['service', 'service2', 'service3'], $servicesList);
}
}
Loading

0 comments on commit f975ddd

Please sign in to comment.