Skip to content

Commit

Permalink
feat: first php rendered case - update-cmpt-merge-data
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 29, 2019
1 parent 8a0909b commit 6ba4b91
Show file tree
Hide file tree
Showing 19 changed files with 8,354 additions and 1,623 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"php":"7.3.8","version":"2.15.1:v2.15.1#20064511ab796593a3990669eff5f5b535001f7c","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/tmp\/vW6aolH\/2\/precode.php":1055820987,"\/tmp\/vW6aolH\/5\/precode.php":1055820987,"\/tmp\/vW6aolH\/10\/precode.php":1055820987,"\/tmp\/v8lP4Pr\/19\/precode.php":3830120851,"\/tmp\/vglPZER\/1202\/San.php":1939724901}}
{"php":"7.3.8","version":"2.15.1:v2.15.1#20064511ab796593a3990669eff5f5b535001f7c","indent":" ","lineEnding":"\n","rules":{"blank_line_after_namespace":true,"braces":true,"class_definition":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_constants":true,"lowercase_keywords":true,"method_argument_space":{"on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_spaces_after_function_name":true,"no_spaces_inside_parenthesis":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_import_per_statement":true,"single_line_after_imports":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"visibility_required":true,"encoding":true,"full_opening_tag":true},"hashes":{"\/tmp\/vW6aolH\/2\/precode.php":1055820987,"\/tmp\/vW6aolH\/5\/precode.php":1055820987,"\/tmp\/vW6aolH\/10\/precode.php":1055820987,"\/tmp\/v8lP4Pr\/19\/precode.php":3830120851,"\/tmp\/vglPZER\/1202\/San.php":1939724901,"\/tmp\/v2hKxnm\/74\/ssr.php":2976412624}}
8 changes: 5 additions & 3 deletions bin/auto-complete.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#
# 2. Run `source ./bin/auto-complete.sh`
#
# 3. Input `./bin/render.js` and press <Tab>
# 3. Input `./bin/test.js` and press <Tab>

compdef _render render.js
compdef _test test.js
compdef _test render.js
compdef _test render.php

function _render {
function _test {
local line

_arguments -C \
Expand Down
8 changes: 1 addition & 7 deletions bin/build-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let html = ''
let specTpls = ''

// generate html
function genContent ({ componentClass, componentSource, compontentData, componentDataLiteral, specTpl, dirName, result }) {
function genContent ({ componentSource, componentDataLiteral, specTpl, dirName, result }) {
const id = dirName
const noDataOutput = /-ndo$/.test(dirName)

Expand Down Expand Up @@ -38,10 +38,8 @@ function genContent ({ componentClass, componentSource, compontentData, componen

function buildFile (caseDir) {
const files = fs.readdirSync(caseDir)
let componentClass
let componentSource
let specTpl
let compontentData
let componentDataLiteral
let result
let sourceFile = ''
Expand All @@ -61,7 +59,6 @@ function buildFile (caseDir) {
if (isFile) {
switch (filename) {
case 'component.js':
componentClass = require(abFilePath)
componentSource = fs.readFileSync(abFilePath, 'UTF-8')
.split('\n')
.map(line => {
Expand All @@ -82,7 +79,6 @@ function buildFile (caseDir) {
break

case 'data.json':
compontentData = require(abFilePath)
componentDataLiteral = fs.readFileSync(abFilePath, 'UTF-8')
break

Expand All @@ -105,9 +101,7 @@ function buildFile (caseDir) {
// generate html when it has source file
if (sourceFile) {
genContent({
componentClass,
componentSource,
compontentData,
componentDataLiteral,
specTpl,
dirName,
Expand Down
2 changes: 1 addition & 1 deletion bin/compile-to-js.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const { writeFileSync, readdirSync } = require('fs')
const san = require('../src/ssr')
const san = require('../src/js-ssr')
const { resolve, join } = require('path')
const caseRoot = resolve(__dirname, '../test/cases')

Expand Down
2 changes: 1 addition & 1 deletion bin/render-to-html.js → bin/js-render-all.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

const { writeFileSync, readdirSync } = require('fs')
const san = require('../src/ssr')
const san = require('../src/js-ssr')
const { resolve, join } = require('path')
const caseRoot = resolve(__dirname, '../test/cases')
const { read } = require('../src/data')
Expand Down
25 changes: 5 additions & 20 deletions bin/render.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
#!/usr/bin/env node

const { readFileSync } = require('fs')
const { resolve, join } = require('path')
const caseRoot = resolve(__dirname, '../test/cases')
const { read } = require('../src/data')

const caseName = process.argv[2]
console.log(caseName)

const caseDir = join(caseRoot, caseName)
const caseRoot = resolve(__dirname, '../test/cases')
const caseDir = resolve(caseRoot, caseName)
const jsSSRPath = join(caseDir, 'ssr.js')
const dataPath = join(caseDir, 'data.json')
const htmlPath = join(caseDir, 'result.html')
const ssrPath = join(caseDir, 'ssr.js')

const expected = readFileSync(htmlPath, 'utf8')
console.log('-----------expected--------------')
console.log(expected)
console.log()

const data = read(dataPath)
const noDataOutput = /-ndo$/.test(caseDir)
const jsRendered = require(ssrPath)(data, noDataOutput)
console.log('------------js-ssr---------------', result(jsRendered))
console.log(jsRendered)
console.log()

function result (html) {
return html === expected ? 'PASS' : 'FAIL'
}
const jsRendered = require(jsSSRPath)(data, noDataOutput)
process.stdout.write(jsRendered)
13 changes: 13 additions & 0 deletions bin/render.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env php
<?php
include('src/San.php');

$caseName = $argv[1];
$caseDir = "test/cases/" . $caseName;
include($caseDir . '/ssr.php');

$dataStr = file_get_contents($caseDir . "/data.json");
$data = json_decode($dataStr, true);
$noDataOutput = preg_match('/-ndo$/', $caseName);

echo $render($data, $noDataOutput);
58 changes: 58 additions & 0 deletions bin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env node

const chalk = require('chalk')
const { spawnSync } = require('child_process')
const { readFileSync, writeFileSync } = require('fs')
const compileToJSSource = require('../src/js-ssr').compileToSource
const compileToPHPSource = require('../src/php-ssr').compileToSource
const { resolve, join } = require('path')
const caseRoot = resolve(__dirname, '../test/cases')

const caseName = process.argv[2]
const caseDir = join(caseRoot, caseName)
const htmlPath = join(caseDir, 'result.html')
const jsSSRPath = join(caseDir, 'ssr.js')
const phpSSRPath = join(caseDir, 'ssr.php')
const compPath = join(caseDir, 'component.js')

// generate js ssr
const ComponentClass = require(compPath)
const fn = compileToJSSource(ComponentClass)
writeFileSync(jsSSRPath, `module.exports = ${fn}`)

// generate php ssr
delete require.cache[require.resolve(compPath)]
const ComponentClassForPHP = require(compPath)
const php = compileToPHPSource(ComponentClassForPHP)
writeFileSync(phpSSRPath, `<?php $render = ${php}; ?>`)

// check
const expected = readFileSync(htmlPath, 'utf8')
console.log(chalk.green('[EXPECTED]'))
console.log(expected)
console.log()

check(`[SSR: JS] ${jsSSRPath}`, render(caseName, 'js'))
check(`[SSR: PHP] ${phpSSRPath}`, render(caseName, 'php'))

function check (title, html) {
const color = html === expected ? 'green' : 'red'
console.log(chalk[color](title))
console.log(html)
console.log()
if (html !== expected) process.exit(1)
}

function render (caseName, target) {
const bin = resolve(__dirname, `./render.${target}`)
const proc = spawnSync(bin, [caseName])
if (proc.error || proc.stderr.length) {
if (proc.error) console.error(proc.error)
else {
console.log('STDOUT:', proc.stdout.toString())
console.error('STDERR:', proc.stderr.toString())
}
process.exit(1)
}
return proc.stdout.toString()
}
72 changes: 50 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"wdio-jasmine-framework": "^0.3.0",
"wdio-sauce-service": "^0.4.14",
"wdio-spec-reporter": "^0.1.0"
},
"dependencies": {
"chalk": "^2.4.2"
}
}
2 changes: 1 addition & 1 deletion src/San.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
final class San
{
public const componentRenderers = [];
public static $componentRenderers = [];
private const HTML_ENTITY = [
'&' => '&amp;',
'<' => '&lt;',
Expand Down
Empty file added src/compile-expr-js-source.js
Empty file.
Loading

0 comments on commit 6ba4b91

Please sign in to comment.