branch: elpa/php-mode
commit 1a1b285b786afc94fd553cbd7958221f48a792db
Author: USAMI Kenta <[email protected]>
Commit: USAMI Kenta <[email protected]>
Test HTML template handling with polymode and web-mode
Add tests covering the reworked behaviour: PHP chunks of a PHP-in-HTML
polymode must indent, and a PHP file that contains HTML must derive to
php-html-template-major-mode.
Install polymode and web-mode in the test target so the tests run in CI.
The poly test defines its innermode inline rather than depending on the
poly-php package, because poly-php requires php-mode and would install a
released copy from an archive that shadows the php-mode under test. Both
tests skip when their optional package is unavailable.
---
Eask | 2 ++
Makefile | 1 +
tests/php-mode-test.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)
diff --git a/Eask b/Eask
index 6c3ad83793..6fe9bb805e 100644
--- a/Eask
+++ b/Eask
@@ -33,6 +33,8 @@
(depends-on "projectile")
(depends-on "smart-jump")
(depends-on "shut-up")
+ (depends-on "polymode")
+ (depends-on "web-mode")
)
(setq network-security-level 'low) ; see
https://github.com/jcs090218/setup-emacs-windows/issues/156#issuecomment-932956432
diff --git a/Makefile b/Makefile
index 9b722e9be6..ff55d9071d 100644
--- a/Makefile
+++ b/Makefile
@@ -49,6 +49,7 @@ dev:
# for an example of using a script like this with the 'git bisect run'
# command.
test: clean all
+ $(EASK) install polymode web-mode
$(EASK) test ert ./tests/php-mode-test.el
.PHONY: all authors autoloads clean test
diff --git a/tests/php-mode-test.el b/tests/php-mode-test.el
index 6881ec5417..8985c2d6ce 100644
--- a/tests/php-mode-test.el
+++ b/tests/php-mode-test.el
@@ -653,6 +653,52 @@ Meant for `php-mode-test-issue-503'."
;; Proper alignment arglist that contains empty lines.
(with-php-mode-test ("indent/issue-793.php" :indent t :magic t)))
+(ert-deftest php-mode-test-poly-php-html-indentation ()
+ "Indentation must work inside PHP chunks of a PHP-in-HTML polymode.
+Regression: `php-check-html-for-indentation' returned nil in polymode
+buffers, which disabled indentation of the PHP chunks entirely.
+
+The innermode is defined here (mirroring the `poly-php' package) rather
+than depending on `poly-php', because that package pulls in a released
+`php-mode' from an archive that would shadow the one under test."
+ (skip-unless (require 'polymode nil t))
+ ;; `php-in-poly-php-html-mode' checks the `poly-php-html-mode' variable
+ ;; by name, so define the polymode under exactly that name.
+ (eval '(progn
+ (define-hostmode php-mode-test--poly-html-hostmode :mode 'html-mode)
+ (define-innermode php-mode-test--poly-php-innermode
+ :mode 'php-mode
+ :head-matcher "<\\?php\\|<\\?="
+ :tail-matcher "\\?>"
+ :head-mode 'host :tail-mode 'host)
+ (define-polymode poly-php-html-mode
+ :hostmode 'php-mode-test--poly-html-hostmode
+ :innermodes '(php-mode-test--poly-php-innermode)))
+ t)
+ (with-temp-buffer
+ (insert "<div>\n<?php\nif ($x) {\necho 'hello';\n}\n?>\n</div>\n")
+ (poly-php-html-mode)
+ (font-lock-ensure)
+ (goto-char (point-min))
+ (search-forward "echo 'hello';")
+ (beginning-of-line)
+ (indent-according-to-mode)
+ (should (= 4 (current-indentation)))))
+
+(ert-deftest php-mode-test-derive-html-template-major-mode ()
+ "A PHP file that contains HTML tags derives to
`php-html-template-major-mode'."
+ (skip-unless (fboundp 'web-mode))
+ (let ((php-html-template-major-mode 'web-mode)
+ (php-project-php-file-as-template 'auto))
+ (with-temp-buffer
+ (setq buffer-file-name (expand-file-name "template.php"
temporary-file-directory))
+ (unwind-protect
+ (progn
+ (insert "<div>\n<?php echo 'hi'; ?>\n</div>\n")
+ (should (eq 'web-mode (php-derivation-major-mode))))
+ (set-buffer-modified-p nil)
+ (setq buffer-file-name nil)))))
+
(ert-deftest php-mode-test-php74 ()
"Test highlighting language constructs added in PHP 7.4."
(with-php-mode-test ("7.4/arrow-function.php" :faces t))