Skip to content

Commit

Permalink
Merge pull request #11 from zaporylie/add-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
zaporylie authored Feb 14, 2019
2 parents 946aabb + 69dbccc commit 174c30d
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,19 @@ before_install:

install:
- composer --verbose validate
- composer --verbose install

script:
# Run automated tests.
- if [[ ${TRAVIS_PHP_VERSION:0:3} != "5.6" ]]; then composer install && ./vendor/bin/phpunit; fi
- cd .. && composer create-project drupal-composer/drupal-project:8.x-dev drupal-project --stability dev --no-interaction
- ls -lah . && cd drupal-project
# zaporylie/composer-drupal-optimizations has been added to drupal-composer/drupal-project and now must be removed
# in order to measure the performance change.
- composer remove zaporylie/composer-drupal-optimizations
- if [[ $RELEASE = dev ]]; then composer --verbose require --no-update --dev webflo/drupal-core-require-dev:8.6.x-dev; fi;
- if [[ $RELEASE = dev ]]; then composer --verbose require --no-update drupal/core:8.6.x-dev; fi;
- if [[ $RELEASE = dev ]]; then composer --verbose update; fi;
# Check the performance.
- composer update nothing --profile
- composer config repositories.local path "../composer-drupal-optimizations" && composer require zaporylie/composer-drupal-optimizations:@dev
- composer update nothing --profile
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
"description": "Composer plugin to improve composer performance for Drupal projects",
"type": "composer-plugin",
"license": "GPL-2.0-or-later",
"require-dev": {
"composer/composer": "^1.6"
},
"authors": [
{
"name": "Jakub Piasecki",
"email": "jakub@piaseccy.pl"
}
],
"require": {
"composer-plugin-api": "^1.1",
"drupal/core": "^8.5"
"composer-plugin-api": "^1.1"
},
"require-dev": {
"composer/composer": "^1.6",
"phpunit/phpunit": "^6"
},
"autoload": {
"psr-4": {
Expand Down
27 changes: 27 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="true"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<php>
<ini name="error_reporting" value="-1" />
</php>

<filter>
<whitelist>
<directory>./src/</directory>
</whitelist>
</filter>
</phpunit>
11 changes: 8 additions & 3 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
*/
class Cache extends BaseCache
{
private static $lowestTags = [
protected static $lowestTags = [
'symfony/symfony' => 'v3.4.0',
];

public function read($file)
{
$content = parent::read($file);
$content = $this->readFile($file);
if (!\is_array($data = json_decode($content, true))) {
return $content;
}
foreach (array_keys(self::$lowestTags) as $key) {
foreach (array_keys(static::$lowestTags) as $key) {
list($provider, ) = explode('/', $key, 2);
if (0 === strpos($file, "provider-$provider\$")) {
$data = $this->removeLegacyTags($data);
Expand All @@ -30,6 +30,11 @@ public function read($file)
return json_encode($data);
}

protected function readFile($file)
{
return parent::read($file);
}

public function removeLegacyTags(array $data)
{
foreach (self::$lowestTags as $package => $lowestVersion) {
Expand Down
55 changes: 55 additions & 0 deletions tests/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace zaporylie\ComposerDrupalOptimizations\Tests;

use Composer\IO\NullIO;
use PHPUnit\Framework\TestCase;
use zaporylie\ComposerDrupalOptimizations\Cache;

class CacheTest extends TestCase
{

/**
* Tests if data is not malformed and only valid array from valid provider
* is processed.
*
* @param $provided
* @param $expected
*
* @dataProvider provideReadTest
*/
public function testRead($provided, $expected)
{
$cache = new class(new NullIO(), 'test') extends Cache {
protected static $lowestTags = [
'vendor/package' => 'version',
];
protected function readFile($file)
{
// Remove provider - used only for tests.
if (0 === strpos($file, 'provider-vendor$')) {
$file = substr($file, 16);
}
return $file;
}
public function removeLegacyTags(array $data)
{
$data['status'] = 'ok';
return $data;
}
};
static::assertEquals($expected, $cache->read($provided));
}

/**
* Test data.
*/
function provideReadTest()
{
yield 'normal' => ['{"a":"b"}', '{"a":"b"}'];
yield 'falsy' => ['{"a":"b"', '{"a":"b"'];
yield 'empty' => ['', ''];
yield 'matching-incorrect-provider' => ['{"provider":"vendor"}', '{"provider":"vendor"}'];
yield 'matching' => ['provider-vendor${"provider":"vendor"}', '{"provider":"vendor","status":"ok"}'];
}
}

0 comments on commit 174c30d

Please sign in to comment.