Skip to content
Snippets Groups Projects
Commit bd8571d0 authored by Sebastien ANDRE's avatar Sebastien ANDRE
Browse files

change detect official comment

parent e1901797
No related merge requests found
Pipeline #35322 canceled with stage
This commit is part of merge request !5006. Comments created here will be created in the context of that merge request.
<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Migration_SplitTestsFiles
{
use Trait_StormFileSystem;
const INDENT = ' ';
protected ?Class_Migration_SplitTestsFiles_TestClass $_current_class = null;
protected array $_classs = [];
public function run(): self
{
foreach ($this->_getAllTestsFiles() as $file_name)
$this->_runOne($file_name);
return $this;
}
public static function isClosedContent(string $content): bool
{
return preg_match('/\s*\}\s*/', $content);
}
public static function isOpenedContent(string $content): bool
{
return preg_match('/\s*\{\s*/', $content);
}
public static function indent(int $depth, bool $with_car_return = true): string
{
if ($depth < 0)
$depth = 0;
$indent = $with_car_return ? "\n" : '';
for ($pos = 0; $pos < $depth; $pos++)
$indent .= static::INDENT;
return $indent;
}
protected function _runOne(string $file_name): self
{
foreach ($this->getFileSystem()->fileGetContentAsArray($file_name) as $line)
$this->_addLine($line ?? '');
return $this;
}
protected function _getAllTestsFiles(): array
{
$all_files = [];
exec('find ./tests -type f -name \\TemplatesTest.php',
$all_files);
return $all_files;
}
protected function _addLine(string $line): self
{
if ( ! $this->_current_class)
$this->_current_class = new Class_Migration_SplitTestsFiles_TestClass;
if ( ! $this->_current_class
->addContent($line)
->isClosed())
return $this;
$this->_classs [] = $this->_current_class;
$this->_current_class = null;
return $this;
}
}
<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Migration_SplitTestsFiles_TestClass
{
const OFFICIAL = "<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
\n";
protected string $_name = '';
protected string $_extend = '';
protected bool $_is_abstract = false;
protected bool $_is_opened = false;
protected bool $_is_closed = false;
protected array $_functions = [];
protected ?Class_Migration_SplitTestsFiles_TestFunction $_current_function = null;
protected ?Class_Migration_SplitTestsFiles_TestFunction $_setup = null;
protected ?Class_Migration_SplitTestsFiles_TestFunction $_teardown = null;
protected ?Class_Migration_SplitTestsFiles_TestComment $_comment = null;
public function addContent(string $content): self
{
if ($this->isClosed())
return $this;
return ('' !== $this->_name && '' !== $this->_extend)
? $this->_addFunction($content)
: $this->_addClass($content);
}
public function isClosed(): bool
{
return $this->_is_opened && $this->_is_closed;
}
public function contents(): string
{
if ($this->_teardown)
$this->_functions = [$this->_teardown, ...$this->_functions];
if ($this->_setup)
$this->_functions = [$this->_setup, ...$this->_functions];
$this->_functions = array_filter($this->_functions,
fn($func) => ! $func->isEmpty());
return $this->isClosed()
? $this->_contentComments() . $this->_contentClass() . $this->_contentFunctions()
: '';
}
protected function _contentComments(): string
{
return static::OFFICIAL . $this->_comment()->contents();
}
protected function _contentClass(): string
{
return Class_Migration_SplitTestsFiles::indent(0)
. ($this->_is_abstract ? 'abstract ' : '')
. 'class ' . $this->_name
. ' extends ' . $this->_extend;
}
protected function _contentFunctions(): string
{
return Class_Migration_SplitTestsFiles::indent(0) . "{\n\n"
. implode("\n\n", array_map(fn($func) => $func->contents(), $this->_functions))
. Class_Migration_SplitTestsFiles::indent(0, false) . "}\n";
}
protected function _addClass(string $content): self
{
if ( ! preg_match('/\s*(abstract)?\s*class\s+(\w+)\s+extends\s+(\w+)\s*(\{)?\s*/i',
$content, $matches))
return $this->_addComment($content);
$this->_is_abstract = ('' !== ($matches[1] ?? ''));
$this->_name = ($matches[2] ?? '');
$this->_extend = ($matches[3] ?? '');
$this->_is_opened = ('' !== ($matches[4] ?? ''));
return $this;
}
protected function _addFunction(string $content): self
{
if ( ! $this->_is_opened)
{
$this->_is_opened = Class_Migration_SplitTestsFiles::isOpenedContent($content);
return $this;
}
if ( ! $this->_current_function)
$this->_current_function = new Class_Migration_SplitTestsFiles_TestFunction;
if ($this->_doClosed($content))
return $this->_reset();
return ($this->_current_function
->addContent($content)
->isClosed())
? $this->_reset()
: $this;
}
protected function _doClosed(string $content): bool
{
if ($this->_currFunctionIsClosed()
&& Class_Migration_SplitTestsFiles::isClosedContent($content))
$this->_is_closed = true;
return $this->_is_closed;
}
protected function _currFunctionIsClosed(): bool
{
return ! $this->_current_function
|| $this->_current_function->isEmpty()
|| $this->_current_function->isClosed();
}
protected function _reset(): self
{
$function = $this->_current_function;
$this->_current_function = null;
if ($function->isSetup())
{
$this->_setup = $function;
return $this;
}
if ($function->isTeardown())
{
$this->_teardown = $function;
return $this;
}
$this->_functions [] = $function;
return $this;
}
protected function _addComment(string $content): self
{
$this->_comment()->addContent($content);
return $this;
}
protected function _comment(): Class_Migration_SplitTestsFiles_TestComment
{
return $this->_comment ??= new Class_Migration_SplitTestsFiles_TestComment;
}
}
<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Migration_SplitTestsFiles_TestComment
{
protected array $_contents = [];
protected bool $_has_test = false;
protected bool $_is_multiple = false;
protected string $_depend = '';
protected string $_see = '';
public function addContent(string $content): self
{
if (preg_match('/@test/i', $content))
{
$this->_has_test = true;
$content = preg_replace('/@test/i', '', $content);
}
if (preg_match('/@depends\s*(\w+)/i', $content, $matches))
{
$this->_depend = ($matches[1] ?? '');
$content = preg_replace('/@depends\s*(\w+)/i', '', $content);
}
if (preg_match('/@see\s*(.+)/i', $content, $matches))
{
$this->_see = trim($matches[1] ?? '');
return $this;
}
return $this->_filterContent($content);
}
protected function _filterContent(string $content): self
{
preg_match('/\s*(\/*\**\/*)\s*(.+)/i', $content, $matches);
$add_content = trim($matches[2] ?? '');
if (preg_match('/copyright\s+\(c\)/i', '', $add_content))
return $this;
if (preg_match('/bokeh\s+is\s+free\s+software/i', '', $add_content))
return $this;
if (preg_match('/it\s+under\s+the\s+terms\s+of/i', '', $add_content))
return $this;
if (preg_match('/the\s+free\s+software/i', '', $add_content))
return $this;
if (preg_match('/there\s+are\s+special\s+exceptions/i', '', $add_content))
return $this;
if (preg_match('/is\s+applied\s+to\s+this\s+software/i', '', $add_content))
return $this;
if (preg_match('/bokeh\s+is\s+distributed\s+in\s+the\s+hope/i', '', $add_content))
return $this;
if (preg_match('/but\s+without\s+any\s+warranty/i', '', $add_content))
return $this;
if (preg_match('/marchantability\s+or\s+fitness/i', '', $add_content))
return $this;
if (preg_match('/gnu\s+affero\s+general\s+public/i', '', $add_content))
return $this;
if (preg_match('/You\s+should\s+have\s+received\s+a\s+copy/i', '', $add_content))
return $this;
if (preg_match('/along\s+with\s+bokeh/i', '', $add_content))
return $this;
if (preg_match('/foundation\,\s+inc/i', '', $add_content))
return $this;
$add_content = preg_replace('/\*+\/+/', '', $add_content);
$add_content = preg_replace('/\*+/', '', $add_content);
$this->_contents [] = $add_content;
return $this;
}
public function contents(): string
{
$this->_contents = array_filter($this->_contents);
if ($this->_has_test)
$this->_contents [] = '@test';
if ($this->_depend)
$this->_contents [] = '@depends ' . $this->_depend;
if ($this->_see)
$this->_contents [] = '@see ' . $this->_see;
if ( ! $this->_contents)
return '';
$this->_is_multiple = count($this->_contents) > 1;
return $this->_start() . $this->_comment() . $this->_end();
}
protected function _start(): string
{
return Class_Migration_SplitTestsFiles::indent(1, false)
. ($this->_isMultiple()
? ($this->_is_multiple ? '/**' : '/** ')
: '// ');
}
protected function _comment(): string
{
$comment = '';
$indent = $this->_is_multiple
? Class_Migration_SplitTestsFiles::indent(1) . ' * '
: '';
foreach ($this->_contents as $content)
$comment .= $indent . $content;
return $comment;
}
protected function _end(): string
{
$indent = $this->_is_multiple
? Class_Migration_SplitTestsFiles::indent(1)
: '';
return $this->_isMultiple()
? ($indent . ' */')
: '';
}
protected function _isMultiple(): bool
{
return $this->_is_multiple || $this->_has_test || $this->_depend;
}
}
<?php
/**
* Copyright (c) 2012-2024, Agence Française Informatique (AFI). All rights reserved.
*
* BOKEH is free software; you can redistribute it and/or modify
* it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
* the Free Software Foundation.
*
* There are special exceptions to the terms and conditions of the AGPL as it
* is applied to this software (see README file).
*
* BOKEH is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
* along with BOKEH; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class Class_Migration_SplitTestsFiles_TestFunction
{
protected string $_type = '';
protected string $_name = '';
protected string $_params = '';
protected bool $_is_abstract = false;
protected bool $_is_opened = false;
protected bool $_is_closed = false;
protected ?Class_Migration_SplitTestsFiles_TestComment $_comment = null;
protected array $_contents = [];
public function addContent(string $content): self
{
if ($this->isClosed())
return $this;
return '' !== $this->_name
? $this->_addContent($content)
: $this->_addFunction($content);
}
public function isClosed(): bool
{
return $this->_is_opened && $this->_is_closed;
}
public function isEmpty(): bool
{
return ! $this->_name;
}
public function isSetup(): bool
{
return preg_match('/setup/i', $this->_name);
}
public function isTeardown(): bool
{
return preg_match('/teardown/i', $this->_name);
}
public function contents(): string
{
return $this
->_contentForSetup()
->_contentForTeardown()
->isClosed()
? $this->_contentComments() . $this->_contentFunction() . $this->_insideContent()
: '';
}
protected function _contentForTeardown(): self
{
if ( ! $this->isClosed() || ! $this->isTeardown())
return $this;
if ( ! (new Storm_Collection($this->_contents))
->detect(fn($content) => preg_match('/parent::teardown/i', $content)))
$this->_contents [] = Class_Migration_SplitTestsFiles::indent(2, false)
. "parent::tearDown();\n";
return $this;
}
protected function _contentForSetup(): self
{
if ( ! $this->isClosed() || ! $this->isSetup())
return $this;
if ( ! (new Storm_Collection($this->_contents))
->detect(fn($content) => preg_match('/parent::setup/i', $content)))
$this->_contents = [(Class_Migration_SplitTestsFiles::indent(2, false)
. "parent::setUp();\n"),
...$this->_contents];
return $this;
}
protected function _contentComments(): string
{
return ($comment = $this->_comment()->contents())
? ($comment . "\n")
: '';
}
protected function _contentFunction(): string
{
return Class_Migration_SplitTestsFiles::indent(1, false)
. ($this->_is_abstract ? 'abstract ' : '')
. $this->_type
. ' function ' . $this->_name()
. '(' . $this->_params . ')';
}
protected function _name(): string
{
if ($this->isSetup())
return 'setUp';
return $this->isTeardown()
? 'tearDown'
: $this->_name;
}
protected function _insideContent(): string
{
return Class_Migration_SplitTestsFiles::indent(1) . "{\n"
. implode('', $this->_contents)
. Class_Migration_SplitTestsFiles::indent(1, false) . "}\n";
}
protected function _addFunction(string $content): self
{
if ( ! preg_match('/\s*(abstract)?\s*(protected|public)?\s+function\s+(\w+)\s*\(([a-zA-Z0-9_\$ ,]*)\)\s*(\{)?\s*/i',
$content, $matches))
return $this->_addComment($content);
$this->_is_abstract = ('' !== ($matches[1] ?? ''));
$this->_type = ($matches[2] ?? '');
$this->_name = ($matches[3] ?? '');
$this->_params = ($matches[4] ?? '');
$this->_is_opened = ('' !== ($matches[5] ?? ''));
return $this;
}
protected function _addContent(string $content): self
{
if ( ! $this->_is_opened)
{
$this->_is_opened = Class_Migration_SplitTestsFiles::isOpenedContent($content);
return $this;
}
if (Class_Migration_SplitTestsFiles::isClosedContent($content))
{
$this->_is_closed = true;
return $this;
}
$this->_contents [] = $content;
return $this;
}
protected function _addComment(string $content): self
{
$this->_comment()->addContent($content);
return $this;
}
protected function _comment(): Class_Migration_SplitTestsFiles_TestComment
{
return $this->_comment ??= new Class_Migration_SplitTestsFiles_TestComment;
}
}
<?php
require(__DIR__ . '/../console.php');
ini_set('display_errors', true);
error_reporting(E_ALL);
(new Class_Migration_SplitTestsFiles)->run();
......@@ -10,14 +10,14 @@ class TemplatesTest extends AbstractControllerTestCase {
}
/** @Test */
/*** @Test */
public function shouldDisplayProfiles() {
$this->dispatch('/admin/template/apply/template/INTONATION');
$this->assertXPath('//table//td//a[contains(@href, 'admin/template/apply/template/INTONATION/on/3')]');
}
/** @test */
/** ** @test **/
public function shouldRedirectToProfile2() {
$this->dispatch('/admin/template/apply/template/INTONATION/on/2');
$this->assertRedirectTo('/opac/index/index/id_profil/2');
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment