Skip to content

Commit

Permalink
Add PHP Codesniffer. Update PHPUnit to support 7.4 (#36)
Browse files Browse the repository at this point in the history
* Add PHP Codesniffer. Update PHPUnit to support 7.4

* Update to Silverstripe's coding convensions

* Fix linting based on Silverstripe standards

* Update build status image in README

* Update module requirements in README
  • Loading branch information
chrispenny authored Oct 20, 2021
1 parent 91af156 commit 7b0764e
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 237 deletions.
47 changes: 18 additions & 29 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
language: php
version: ~> 1.0

dist: trusty
import:
- silverstripe/silverstripe-travis-shared:config/provision/standard.yml

cache:
directories:
- $HOME/.composer/cache/files

env:
global:
- COMPOSER_ROOT_VERSION=1.0.x-dev

matrix:
fast_finish: true
jobs:
include:
- php: 7.1
env: DB=MYSQL PHPUNIT_TEST=1
env:
- DB=MYSQL
- REQUIRE_INSTALLER="4.x-dev"
- PHPUNIT_COVERAGE_TEST=1
- PHPCS_TEST=1
- php: 7.3
env: DB=MYSQL PHPUNIT_TEST=1

before_script:
# Init PHP
- export CORE_RELEASE=$TRAVIS_BRANCH
- phpenv rehash
- phpenv config-rm xdebug.ini
- export PATH=~/.composer/vendor/bin:$PATH
- echo 'memory_limit = 2048M' >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

# Install composer
- composer validate
- composer install --prefer-dist --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile

script:
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/php; fi
env:
- DB=MYSQL
- REQUIRE_INSTALLER="4.x-dev"
- PHPUNIT_COVERAGE_TEST=1
- php: 7.4
env:
- DB=MYSQL
- REQUIRE_INSTALLER="4.x-dev"
- PHPUNIT_COVERAGE_TEST=1
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Populate Module

[![Build Status](https://secure.travis-ci.org/dnadesign/silverstripe-populate.png?branch=master)](http://travis-ci.org/dnadesign/silverstripe-populate)
[![Build Status](https://app.travis-ci.com/silverstripe/silverstripe-populate.svg?branch=master)](http://travis-ci.org/dnadesign/silverstripe-populate)

This module provides a way to populate a database from YAML fixtures and custom
classes. For instance, when a building a web application the pages and default
Expand All @@ -9,7 +9,9 @@ objects can be defined in YAML and shared around developers. This extends the

## Requirements

* SilverStripe 4 ([framework](https://github.com/silverstripe/silverstripe-framework) only)
* PHP 7.1
* SilverStripe [Framework ^4](https://github.com/silverstripe/silverstripe-framework)
* SilverStripe [Versioned ^1](https://github.com/silverstripe/silverstripe-versioned)

## Installation Instructions

Expand Down
7 changes: 3 additions & 4 deletions code/PopulateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,21 @@ public function createObject($class, $identifier, $data = null)
);

unset($data['PopulateMergeWhen']);

} else if (isset($data['PopulateMergeMatch'])) {
} elseif (isset($data['PopulateMergeMatch'])) {
$filter = [];

foreach ($data['PopulateMergeMatch'] as $field) {
$filter[$field] = $data[$field];
}

if (!$filter) {
throw new \Exception('Not a valid PopulateMergeMatch filter');
throw new Exception('Not a valid PopulateMergeMatch filter');
}

$lookup = DataList::create($class)->filter($filter);

unset($data['PopulateMergeMatch']);
} else if (isset($data['PopulateMergeAny'])) {
} elseif (isset($data['PopulateMergeAny'])) {
$lookup = DataList::create($class);

unset($data['PopulateMergeAny']);
Expand Down
7 changes: 4 additions & 3 deletions code/PopulateTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class PopulateTask extends BuildTask
* @param HTTPRequest $request
* @throws Exception
*/
public function run($request) {
Populate::requireRecords();
}
public function run($request)
{
Populate::requireRecords();
}
}
151 changes: 76 additions & 75 deletions code/extensions/PopulateMySQLExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,84 +18,85 @@
* Opt into this extension by adding this to your populate.yml configuration.
*
* <code>
* PopulateMySQLExportExtension:
* export_db_path: ~/path.sql
* PopulateMySQLExportExtension:
* export_db_path: ~/path.sql
*
* Populate:
* extensions
* - PopulateMySQLExportExtension
* Populate:
* extensions
* - PopulateMySQLExportExtension
* </code>
*
* @package populate
*/
class PopulateMySQLExportExtension extends Extension {
use Configurable;

/**
* @config
*/
private static $export_db_path;

public function getPath() {
$path = Config::inst()->get(__CLASS__, 'export_db_path');

if(!$path) {
$path = Controller::join_links(TEMP_FOLDER . '/populate.sql');
} else {
$path = (substr($path, 0, 1) !== "/") ? Controller::join_links(BASE_PATH, $path) : $path;
}

return $path;
}

/**
*
*/
public function onAfterPopulateRecords() {
$path = $this->getPath();

DB::alteration_message("Saving populate state to $path", "success");
$result = DB::query('SHOW TABLES');
$tables = $result->column();
$return = '';

foreach($tables as $table) {
$return.= 'DROP TABLE IF EXISTS `'.$table.'`;';
$row2 = DB::query("SHOW CREATE TABLE `$table`");
$create = $row2->nextRecord();
$create = str_replace("\"", "`", $create);
$return.= "\n\n".$create['Create Table'].";\n\n";


$result = DB::query("SELECT * FROM `$table`");
while($row = $result->nextRecord()) {
$return.= 'INSERT INTO '.$table.' VALUES(';

foreach($row as $k => $v) {
$v = addslashes($v);
$v = str_replace("\n", "\\n", $v);

if($v) {
$return.= '"'.$v.'"' ;
} else {
$return.= '""';
}

$return.= ',';
}

$return = rtrim($return, ',');
$return.= ");\n";
}
}

$return.="\n\n\n";



$handle = fopen($path,'w+');

fwrite($handle, $return);
fclose($handle);
}
class PopulateMySQLExportExtension extends Extension
{
use Configurable;

/**
* @config
*/
private static $export_db_path;

public function getPath()
{
$path = Config::inst()->get(__CLASS__, 'export_db_path');

if (!$path) {
$path = Controller::join_links(TEMP_FOLDER . '/populate.sql');
} else {
$path = (substr($path, 0, 1) !== "/") ? Controller::join_links(BASE_PATH, $path) : $path;
}

return $path;
}

/**
*
*/
public function onAfterPopulateRecords()
{
$path = $this->getPath();

DB::alteration_message("Saving populate state to $path", "success");
$result = DB::query('SHOW TABLES');
$tables = $result->column();
$return = '';

foreach ($tables as $table) {
$return .= 'DROP TABLE IF EXISTS `' . $table . '`;';
$row2 = DB::query("SHOW CREATE TABLE `$table`");
$create = $row2->nextRecord();
$create = str_replace("\"", "`", $create);
$return .= "\n\n" . $create['Create Table'] . ";\n\n";

$result = DB::query("SELECT * FROM `$table`");

while ($row = $result->nextRecord()) {
$return .= 'INSERT INTO ' . $table . ' VALUES(';

foreach ($row as $k => $v) {
$v = addslashes($v);
$v = str_replace("\n", "\\n", $v);

if ($v) {
$return .= '"' . $v . '"';
} else {
$return .= '""';
}

$return .= ',';
}

$return = rtrim($return, ',');
$return .= ");\n";
}
}

$return .= "\n\n\n";

$handle = fopen($path, 'w+');

fwrite($handle, $return);
fclose($handle);
}
}
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
}],
"require": {
"php": ">=7.1",
"silverstripe/framework": "^4@stable",
"silverstripe/versioned": "^1@dev"
"silverstripe/framework": "^4",
"silverstripe/versioned": "^1"
},
"require-dev": {
"phpunit/phpunit": "~4.8"
"sminnee/phpunit": "^5.7",
"squizlabs/php_codesniffer": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
39 changes: 39 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="SilverStripe">
<description>CodeSniffer ruleset for SilverStripe coding conventions.</description>

<file>code</file>
<file>tests</file>

<!-- base rules are PSR-2 -->
<rule ref="PSR2" >
<!-- Current exclusions -->
<exclude name="PSR1.Methods.CamelCapsMethodName" />
<exclude name="PSR1.Files.SideEffects.FoundWithSymbols" />
<exclude name="PSR2.Classes.PropertyDeclaration" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration" /> <!-- causes php notice while linting -->
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenercase" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.WrongOpenerdefault" />
<exclude name="PSR2.ControlStructures.SwitchDeclaration.TerminatingComment" />
<exclude name="PSR2.Methods.MethodDeclaration.Underscore" />
<exclude name="Squiz.Scope.MethodScope" />
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
<exclude name="Generic.Files.LineLength.TooLong" />
<exclude name="PEAR.Functions.ValidDefaultValue.NotAtEnd" />
</rule>

<rule ref="Squiz.Strings.ConcatenationSpacing">
<properties>
<property name="spacing" value="1" />
<property name="ignoreNewlines" value="true"/>
</properties>
</rule>

<!-- use short array syntax (less thirdparty) -->
<rule ref="Generic.Arrays.DisallowLongArraySyntax">
<exclude-pattern>/thirdparty/*</exclude-pattern>
</rule>

<!-- include php files only -->
<arg name="extensions" value="php,lib,inc,php5"/>
</ruleset>
Loading

0 comments on commit 7b0764e

Please sign in to comment.