|
1 | 1 | ;;; php-runtime-test.el --- Unit tests for php-runtime package.
|
2 | 2 |
|
3 | 3 | ;; Copyright (C) 2017 USAMI Kenta
|
| 4 | +;; Copyright (C) 2018 Eric James Michael Ritz |
4 | 5 |
|
5 |
| -;; Author: USAMI Kenta <[email protected]> |
| 6 | +;; Authors: USAMI Kenta <[email protected]> |
6 | 7 | ;; Created: 28 Aug 2017
|
7 | 8 | ;; Version: 0.0.1
|
8 | 9 | ;; Keywords: processes php
|
|
37 | 38 | (require 'php-runtime)
|
38 | 39 | (require 'ert)
|
39 | 40 |
|
40 |
| -(ert-deftest php-runtime-test-eval-with-stdin () |
41 |
| - (dolist (v (list '("Input buffer as string" |
42 |
| - :expected "1 apple\n2 orange\n3 banana\n" |
43 |
| - :code "$i = 0; |
44 |
| -while (($line = fgets(STDIN)) !== false) { |
45 |
| - echo ++$i, ' ', trim($line), \"\n\"; |
46 |
| -}" |
47 |
| - :input-buf "apple\norange\nbanana"))) |
48 |
| - (let ((description (car v)) |
49 |
| - (expected (plist-get (cdr v) :expected)) |
50 |
| - (code (plist-get (cdr v) :code)) |
51 |
| - (input-buf (plist-get (cdr v) :input-buf))) |
52 |
| - (should (string= expected (php-runtime-eval code input-buf)))))) |
53 |
| - |
54 |
| -(ert-deftest php-runtime-test-eval-without-stdin () |
55 |
| - (dolist (v (list '("No input buffer" |
56 |
| - :expected "" |
57 |
| - :code "$i = 0; |
58 |
| -while (($line = fgets(STDIN)) !== false) { |
59 |
| - echo ++$i, ' ', trim($line), \"\n\"; |
60 |
| -}"))) |
61 |
| - (let ((description (car v)) |
62 |
| - (expected (plist-get (cdr v) :expected)) |
63 |
| - (code (plist-get (cdr v) :code))) |
64 |
| - (should (string= expected (php-runtime-eval code)))))) |
65 |
| - |
66 |
| -(ert-deftest php-runtime-test-expr () |
67 |
| - "Test that PHP expressions are evaluated and get results." |
| 41 | +(defun run-simple-php (code) |
| 42 | + "Utility function to run CODE for simple PHP testing. |
| 43 | +
|
| 44 | +This function accepts PHP code as a string and runs it using |
| 45 | +whichever PHP executable is available on the system. In order to |
| 46 | +reduce sources of potential problems, we do not load any PHP INI |
| 47 | +or other configurations. |
| 48 | +
|
| 49 | +The function returns the result of the code as a string." |
| 50 | + ;; We call PHP with two options: |
| 51 | + ;; |
| 52 | + ;; 1. `-n` - Causes PHP to ignore any system-wide INI, because we |
| 53 | + ;; don't want any such configuration screwing up our tests. |
| 54 | + ;; |
| 55 | + ;; 2. `-r` - Tells PHP to run the code which follows, which we must |
| 56 | + ;; quote appropriately as a shell argument. |
| 57 | + (shell-command-to-string (concat "php -n -r" (shell-quote-argument code)))) |
| 58 | + |
| 59 | +(ert-deftest runtime-expressions () |
| 60 | + "Test using `php-runtime-expr'. |
| 61 | +
|
| 62 | +We can make explicit invocations of the PHP executable to obtain |
| 63 | +the values of various runtime expressions. These tests compare |
| 64 | +those to the values we obtain via `php-runtime-expr', which |
| 65 | +should always be the same." |
| 66 | + ;; Testing simple constants. |
| 67 | + (should (string= (php-runtime-expr "PHP_VERSION") |
| 68 | + (run-simple-php "echo PHP_VERSION;"))) |
| 69 | + ;; Testing simple function calls. |
| 70 | + (should (string= (php-runtime-expr "strtoupper('foobar')") |
| 71 | + (run-simple-php "echo strtoupper('foobar');"))) |
| 72 | + ;; Testing basic math. |
68 | 73 | (should (string= "200" (php-runtime-expr "(1+1)*100")))
|
69 |
| - (should (string= "foo" (php-runtime-expr "'f' . 'oo'")))) |
| 74 | + ;; Testing string concatenation. |
| 75 | + (should (string= "foo" (php-runtime-expr "'f' . 'oo'"))) |
| 76 | + ;; Converting output to numbers. |
| 77 | + (should (= (string-to-number (php-runtime-expr "PHP_INT_MAX")) |
| 78 | + (string-to-number (run-simple-php "echo PHP_INT_MAX;"))))) |
70 | 79 |
|
71 |
| -(ert-deftest php-runtime-test-expr-syntax-error () |
72 |
| - "Test that PHP code syntax errors are reported as errors in Emacs." |
| 80 | +(ert-deftest php-syntax-errors () |
| 81 | + "Test that PHP code with errors cause appropriate failures." |
73 | 82 | (should-error (php-runtime-expr "a:b")))
|
74 | 83 |
|
75 | 84 | (provide 'php-runtime-test)
|
|
0 commit comments