mirror of
https://github.com/smarty-php/smarty.git
synced 2026-07-05 16:00:55 +02:00
ff2ef3b0cb
* Redirect test temp dirs to system temp directory. Fixes #1178 Move all test-generated output (compiled templates, cache files, and temporary template sources) from per-test-directory folders inside the working tree to a parallel structure under sys_get_temp_dir()/smarty-tests/. This removes 215 boilerplate .gitignore files from the repo and ensures running the test suite leaves zero uncommitted files in the working tree. All 2296 tests continue to pass with identical behavior. * Isolate each test class in a unique temp directory getTempDir() now appends a per-class uniqid token to the temp path, so concurrent or sequential test runs never share compiled/cached output. The token is generated lazily on first use and reset in tearDownAfterClass(), giving every test class a fresh isolated directory. As a result, the Bootstrap.php pre-run cleanup of smarty-tests/ is no longer needed for correctness (stale paths are unreachable) and was harmful to concurrent runs, so it has been removed. * Remove individualFolders dead code and spurious assertTrue from cleanDirs() - Remove the never-active individualFolders code path from setUpSmarty() (the constant was always true, making the branch unreachable) - Remove define('individualFolders') from Config.php and the constructor - Remove $this->assertTrue(true) from cleanDirs(): it existed solely to make testInit() count as a passing test; now that cleanDirs() is called from setUpSmarty() and from test methods directly, the assertion was spuriously inflating assertion counts - Add tests/**/templates_c/, cache/, templates_tmp/ to .gitignore to prevent stale test output from appearing as untracked files * Clean up each test class's unique temp dir in tearDownAfterClass() Add a private static removeDir() helper and call it from tearDownAfterClass() to recursively delete the per-class unique temp directory after each test class finishes. Cleanup failures are silently ignored (@ suppression) so they never cause test failures. Set KEEP_SMARTY_TEST_ARTIFACTS=1 in the environment to skip cleanup and keep the artifacts on disk for debugging. * cleanup of unused template files, non-shared files stored in __shared folder, no longer required calls to add template folders et cetera * fixed the unit tests * Apply suggestions from code review Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * remove useless resetting of static properties in tearDownAfterClass * changed an incorrect doc and formatted some code. * add changelog --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
114 lines
4.6 KiB
PHP
114 lines
4.6 KiB
PHP
<?php
|
|
|
|
require_once __DIR__ . '/../../../../Bootstrap.php';
|
|
|
|
class MatchesOperatorTest extends PHPUnit_Smarty {
|
|
public function setUp(): void
|
|
{
|
|
$this->setUpSmarty(__DIR__);
|
|
}
|
|
|
|
|
|
/**
|
|
* Test basic regex matching functionality
|
|
*/
|
|
public function testBasicMatches() {
|
|
// Test string matching pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "hello" matches "/^[a-z]+$/"}Match!{else}No match{/if}'));
|
|
|
|
// Test no match
|
|
$this->assertEquals('No match', $this->smarty->fetch('string:{if "123" matches "/^[a-z]+$/"}Match!{else}No match{/if}'));
|
|
|
|
// Test with variables
|
|
$this->smarty->assign('name', 'hello');
|
|
$this->smarty->assign('pattern', '/^[a-z]+$/');
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if $name matches $pattern}Match!{else}No match{/if}'));
|
|
|
|
// Test no match with variables
|
|
$this->smarty->assign('number', '123');
|
|
$this->assertEquals('No match', $this->smarty->fetch('string:{if $number matches $pattern}Match!{else}No match{/if}'));
|
|
}
|
|
|
|
/**
|
|
* Test different regex patterns
|
|
*/
|
|
public function testDifferentPatterns() {
|
|
// Test numeric pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "123" matches "/^[0-9]+$/"}Match!{else}No match{/if}'));
|
|
|
|
// Test email pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "test@example.com" matches "/^[^@]+@[^@]+\.[^@]+$/"}Match!{else}No match{/if}'));
|
|
|
|
// Test case insensitive pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "Hello" matches "/hello/i"}Match!{else}No match{/if}'));
|
|
|
|
// Test pattern with special characters
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "hello-world" matches "/^[a-z-]+$/"}Match!{else}No match{/if}'));
|
|
}
|
|
|
|
/**
|
|
* Test regex patterns with modifiers
|
|
*/
|
|
public function testPatternModifiers() {
|
|
// Test case insensitive modifier
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "HELLO" matches "/hello/i"}Match!{else}No match{/if}'));
|
|
|
|
// Test multiline modifier
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "hello\nworld" matches "/world$/m"}Match!{else}No match{/if}'));
|
|
|
|
// Test dotall modifier
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "hello\nworld" matches "/hello.world/s"}Match!{else}No match{/if}'));
|
|
}
|
|
|
|
/**
|
|
* Test complex expressions with matches operator
|
|
*/
|
|
public function testComplexExpressions() {
|
|
// Test with logical AND
|
|
$this->smarty->assign('name', 'hello');
|
|
$this->smarty->assign('pattern', '/^[a-z]+$/');
|
|
$this->smarty->assign('length', 5);
|
|
$this->assertEquals('Valid!', $this->smarty->fetch('string:{if $name matches $pattern && $length > 3}Valid!{else}Invalid{/if}'));
|
|
|
|
// Test with logical OR
|
|
$this->assertEquals('Valid!', $this->smarty->fetch('string:{if $name matches $pattern || $length < 3}Valid!{else}Invalid{/if}'));
|
|
|
|
// Test in elseif
|
|
$this->assertEquals('ElseIf!', $this->smarty->fetch('string:{if $name matches "/^[0-9]+$/"}No{elseif $name matches $pattern}ElseIf!{else}Else{/if}'));
|
|
|
|
// Test in while loop condition (should work with constant patterns)
|
|
$result = $this->smarty->fetch('string:{assign var="i" value=0}{while $i < 2 && "test" matches "/test/"}{$i}{assign var="i" value=$i+1}{/while}');
|
|
$this->assertEquals('01', $result);
|
|
}
|
|
|
|
/**
|
|
* Test edge cases and error handling
|
|
*/
|
|
public function testEdgeCases() {
|
|
// Test with empty string
|
|
$this->assertEquals('No match', $this->smarty->fetch('string:{if "" matches "/.+/"}Match!{else}No match{/if}'));
|
|
|
|
// Test with complex pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "abc123" matches "/^[a-z]+[0-9]+$/"}Match!{else}No match{/if}'));
|
|
|
|
// Test with single character pattern
|
|
$this->assertEquals('Match!', $this->smarty->fetch('string:{if "a" matches "/a/"}Match!{else}No match{/if}'));
|
|
}
|
|
|
|
/**
|
|
* Test that invalid patterns still work (they will cause PHP warnings but not break the template)
|
|
* Note: This test is commented out because invalid patterns cause PHP warnings that fail the test
|
|
* In production, invalid patterns would cause warnings but the template would still execute
|
|
*/
|
|
/*
|
|
public function testInvalidPatterns() {
|
|
// This will cause a PHP warning but should not break the template execution
|
|
$this->smarty->assign('invalid_pattern', 'not-a-valid-pattern');
|
|
$this->smarty->assign('name', 'hello');
|
|
|
|
// The template should execute and reach the else branch due to the invalid pattern
|
|
$result = $this->smarty->fetch('string:{if $name matches $invalid_pattern}Match!{else}No match{/if}');
|
|
$this->assertEquals('No match', $result);
|
|
}
|
|
*/
|
|
} |