Skip to content

Commit

Permalink
[test] add phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
connorhu committed Dec 18, 2023
1 parent 7ab9598 commit c90eb8e
Show file tree
Hide file tree
Showing 369 changed files with 4,890 additions and 6,317 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,4 @@ jobs:
run: php data/bin/check_configuration.php

- name: Run Tests
run: php data/bin/symfony symfony:test --trace
run: php vendor/bin/phpunit
5 changes: 5 additions & 0 deletions lib/autoload/sfCoreAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ class sfCoreAutoload
'sftesterresponse' => 'test/sfTesterResponse.class.php',
'sftesteruser' => 'test/sfTesterUser.class.php',
'sftesterviewcache' => 'test/sfTesterViewCache.class.php',
'symfony1applicationtestcase' => 'test/Symfony1ApplicationTestCase.php',
'symfony1projecttestcase' => 'test/Symfony1ProjectTestCase.php',
'testcasedrivenapplicationconfiguration' => 'test/TestCaseDrivenApplicationConfiguration.php',
'testcasedrivenconfigurationinterface' => 'test/TestCaseDrivenConfigurationInterface.php',
'testcasedrivenprojectconfiguration' => 'test/TestCaseDrivenProjectConfiguration.php',
'sfbasicsecurityuser' => 'user/sfBasicSecurityUser.class.php',
'sfsecurityuser' => 'user/sfSecurityUser.class.php',
'sfuser' => 'user/sfUser.class.php',
Expand Down
1 change: 1 addition & 0 deletions lib/cache/sfAPCCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function has($key)
* @see sfCache
*
* @param mixed|null $lifetime
*
* @return bool|array
*/
public function set($key, $data, $lifetime = null)
Expand Down
4 changes: 2 additions & 2 deletions lib/cache/sfFileCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function get($key, $default = null)

$rawData = $data[self::READ_DATA];

if ($rawData === 'FaLSe') {
if ('FaLSe' === $rawData) {
$rawData = false;
}

Expand Down Expand Up @@ -93,7 +93,7 @@ public function set($key, $data, $lifetime = null)
$this->clean(sfCache::OLD);
}

if ($data === false) {
if (false === $data) {
$data = 'FaLSe';
}

Expand Down
4 changes: 2 additions & 2 deletions lib/cache/sfSQLiteCache.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function get($key, $default = null)
$data = $this->dbh->singleQuery(sprintf("SELECT data FROM cache WHERE key = '%s' AND timeout > %d", sqlite_escape_string($key), time()));
}

if ($data === 'FaLSe') {
if ('FaLSe' === $data) {
$data = false;
}

Expand Down Expand Up @@ -102,7 +102,7 @@ public function set($key, $data, $lifetime = null)
$this->clean(sfCache::OLD);
}

if ($data === false) {
if (false === $data) {
$data = 'FaLSe';
}

Expand Down
55 changes: 55 additions & 0 deletions lib/test/Symfony1ApplicationTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

Check warning on line 1 in lib/test/Symfony1ApplicationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: php_unit_internal_class

Check warning on line 1 in lib/test/Symfony1ApplicationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: php_unit_test_class_requires_covers

Check warning on line 1 in lib/test/Symfony1ApplicationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: phpdoc_separation

Check warning on line 1 in lib/test/Symfony1ApplicationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: single_line_empty_body

Check warning on line 1 in lib/test/Symfony1ApplicationTestCase.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: single_blank_line_at_eof

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;

class Symfony1ApplicationTestCase extends TestCase
{
public function resetSfConfig()
{
sfConfig::set('sf_symfony_lib_dir', realpath(__DIR__.'/../lib'));
sfConfig::set('sf_test_cache_dir', sys_get_temp_dir().'/sf_test_project');
}

public function getEnvironment()
{
return 'test';
}

public function getDebug()
{
return true;
}

public function getEventDispatcher()
{
return null;
}

public function getRootDir()
{
return null;
}

public function projectSetup(sfApplicationConfiguration $applicationConfiguration)
{
}

public function getApplicationConfiguration()
{
return new TestCaseDrivenApplicationConfiguration(
$this,
$this->getEnvironment(),
$this->getDebug(),
$this->getRootDir(),
$this->getEventDispatcher()
);
}
}
39 changes: 39 additions & 0 deletions lib/test/Symfony1ProjectTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use PHPUnit\Framework\TestCase;

class Symfony1ProjectTestCase extends TestCase
{
public function resetSfConfig()
{
sfConfig::set('sf_symfony_lib_dir', realpath(__DIR__.'/../lib'));
sfConfig::set('sf_test_cache_dir', sys_get_temp_dir().'/sf_test_project');
}

public function projectSetup(sfProjectConfiguration $configuration)
{
}

public function getEventDispatcher()
{
return null;
}

public function getRootDir()
{
return null;
}

public function getProjectConfiguration()
{
return new TestCaseDrivenProjectConfiguration($this, $this->getRootDir(), $this->getEventDispatcher());
}
}
37 changes: 37 additions & 0 deletions lib/test/TestCaseDrivenApplicationConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

Check warning on line 1 in lib/test/TestCaseDrivenApplicationConfiguration.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_superfluous_phpdoc_tags

Check warning on line 1 in lib/test/TestCaseDrivenApplicationConfiguration.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_empty_phpdoc

Check warning on line 1 in lib/test/TestCaseDrivenApplicationConfiguration.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_extra_blank_lines

Check warning on line 1 in lib/test/TestCaseDrivenApplicationConfiguration.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: single_blank_line_at_eof

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class TestCaseDrivenApplicationConfiguration extends sfApplicationConfiguration implements TestCaseDrivenConfigurationInterface
{
protected $testCase;

public function __construct($testCase, $environment, $debug, $rootDir = null, sfEventDispatcher $dispatcher = null)
{
$this->testCase = $testCase;
parent::__construct($environment, $debug, $rootDir, $dispatcher);
}

/**
* @return mixed
*/
public function getTestCase()
{
return $this->testCase;
}

public function getI18NGlobalDirs()
{
if (method_exists($this->testCase, 'getI18NGlobalDirs')) {
return $this->testCase->getI18NGlobalDirs();
}

return parent::getI18NGlobalDirs();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* file that was distributed with this source code.
*/

class ProjectConfiguration extends sfProjectConfiguration
interface TestCaseDrivenConfigurationInterface
{
}
public function getTestCase();
}
29 changes: 29 additions & 0 deletions lib/test/TestCaseDrivenProjectConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

Check warning on line 1 in lib/test/TestCaseDrivenProjectConfiguration.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: no_superfluous_phpdoc_tags

/*
* This file is part of the symfony package.
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

class TestCaseDrivenProjectConfiguration extends ProjectConfiguration implements TestCaseDrivenConfigurationInterface
{
protected $testCase;

public function __construct($testCase, $rootDir = null, sfEventDispatcher $dispatcher = null)
{
$this->testCase = $testCase;

parent::__construct($rootDir, $dispatcher);
}

/**
* @return mixed
*/
public function getTestCase()
{
return $this->testCase;
}
}
2 changes: 1 addition & 1 deletion lib/validator/sfValidatorDate.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected function doClean($value)

$format = $this->getOption('with_time') ? $this->getOption('datetime_output') : $this->getOption('date_output');

if ($format === 'U') {
if ('U' === $format) {
return isset($date) ? (int) $date->format($format) : (int) date($format, $cleanTime);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/validator/sfValidatorError.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function getArguments($raw = false)
continue;
}

$arguments["%{$key}%"] = $value !== null ? htmlspecialchars($value, ENT_QUOTES, sfValidatorBase::getCharset()) : '';
$arguments["%{$key}%"] = null !== $value ? htmlspecialchars($value, ENT_QUOTES, sfValidatorBase::getCharset()) : '';
}

return $arguments;
Expand Down
2 changes: 1 addition & 1 deletion lib/validator/sfValidatorString.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ protected function configure($options = array(), $messages = array())
*/
protected function doClean($value)
{
if ($value === null) {
if (null === $value) {
return $value;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/validator/sfValidatorTime.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function doClean($value)
$formattedClean = date($this->getOption('time_output'), $clean);

// don't change integer type
if ($this->getOption('time_output') === 'U') {
if ('U' === $this->getOption('time_output')) {
settype($formattedClean, 'integer');
}

Expand Down
25 changes: 11 additions & 14 deletions tests/PhpUnitSfTestHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,21 @@

trait PhpUnitSfTestHelperTrait
{
public function diag($message): void
{

}

public function arrays_are_equal($actual, $expected, string $message = ''): void
{
sort($actual);
sort($expected);

$this->is($expected, $actual, $message);
}
public function diag($message): void {}

/**
* @see self::assertSame
* @deprecated
*/
public function is($actual, $expected, string $message = ''): void
{
$this->assertSame($expected, $actual, $message);
}

/**
* @see self::assertNotSame
* @deprecated
*/
public function isnt($actual, $expected, string $message = ''): void
{
$this->assertNotSame($expected, $actual, $message);
Expand All @@ -50,12 +47,12 @@ public function ok($actual, string $message = ''): void

public function isa_ok($actual, $expected, string $message = ''): void
{
if ($expected === 'array') {
if ('array' === $expected) {
$this->assertIsArray($actual, $message);
} elseif (str_starts_with($expected, 'sf')) {
$this->assertInstanceOf($expected, $actual, $message);
} else {
throw new \RuntimeException('unhandled type: '. $expected);
throw new \RuntimeException('unhandled type: '.$expected);
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/action/sfComponentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class sfComponentTest extends sfEventDispatcherTestCase

public function setUp(): void
{
$this->context = sfContext::getInstance([
$this->context = sfContext::getInstance(array(
'routing' => 'sfNoRouting',
'request' => 'sfWebRequest',
]);
));

$this->testObject = new myComponent($this->context, 'module', 'action');
$this->dispatcher = $this->context->getEventDispatcher();
Expand Down Expand Up @@ -70,8 +70,8 @@ public function testSetter()
{
$component = new myComponent($this->context, 'module', 'action');

$component->foo = [];
$component->foo = array();
$component->foo[] = 'bar';
$this->assertSame(['bar'], $component->foo, '__set() populates component variables');
$this->assertSame(array('bar'), $component->foo, '__set() populates component variables');
}
}
6 changes: 4 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
require_once $libDir.'/util/sfToolkit.class.php';
sfConfig::set('sf_test_cache_dir', sys_get_temp_dir().'/sf_test_project');

require_once __DIR__.'/fixtures/symfony/config/ProjectConfiguration.class.php';

// remove all test cache
sf_unit_test_shutdown();

Expand All @@ -47,7 +49,7 @@ function sf_unit_test_shutdown()
$sessions = glob(sys_get_temp_dir().'/sessions*');
$tmp_files = glob(sys_get_temp_dir().'/sf*');

$files = array_merge(empty($sessions) ? [] : $sessions, empty($tmp_files) ? [] : $tmp_files);
$files = array_merge(empty($sessions) ? array() : $sessions, empty($tmp_files) ? array() : $tmp_files);
foreach ($files as $file) {
if (is_dir($file)) {
sfToolkit::clearDirectory($file);
Expand All @@ -61,5 +63,5 @@ function sf_unit_test_shutdown()
// Helper for cross platform testcases that validate output
function fix_linebreaks($content)
{
return str_replace(["\r\n", "\n", "\r"], "\n", $content);
return str_replace(array("\r\n", "\n", "\r"), "\n", $content);
}
Loading

0 comments on commit c90eb8e

Please sign in to comment.