Skip to content

Commit

Permalink
JsUtils->JavascriptUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
jcheron committed Nov 13, 2020
1 parent 86f2f1b commit c3aa2d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/PHPMV/js/JsUtils.php → src/PHPMV/js/JavascriptUtils.php
Expand Up @@ -3,22 +3,22 @@

/**
* Javascript utilities.
* PHPMV\js$JsUtils
* PHPMV\js$JavascriptUtils
* This class is part of php-ui-common
*
* @author jc
* @version 1.0.0
*
*/
class JsUtils {
class JavascriptUtils {

/**
* Returns a JSON string from an object.
*
* @param mixed $object
* @return string
*/
public static function objectToJSON($object): string {
public static function toJSON($object): string {
if (\is_object($object)) {
if (\method_exists($object, 'toArray')) {
$object = $object->toArray();
Expand All @@ -37,9 +37,9 @@ public static function objectToJSON($object): string {
*/
public static function wrapScript(string $script): string {
if ($script == null) {
return "";
return '';
}
if (\substr($script, 0, strlen("<script>")) !== "<script>") {
if (\substr($script, 0, \strlen('<script>')) !== '<script>') {
$script = "<script>$script</script>";
}
return $script;
Expand Down
@@ -1,35 +1,35 @@
<?php
use PHPMV\js\JsUtils;
use PHPMV\js\JavascriptUtils;

include_once dirname(__FILE__) . '/../files/classes/TestClass1.php';
include_once dirname(__FILE__) . '/../files/classes/TestClass2.php';

/**
* JsUtils test case.
* JavascriptUtils test case.
*/
class JsUtilsTest extends \Codeception\Test\Unit {
class JavascriptUtilsTest extends \Codeception\Test\Unit {

/**
* Tests JsUtils::objectToJSON()
* Tests JavascriptUtils::objectToJSON()
*/
public function testObjectToJSON() {
$c1 = new TestClass1();
$c1->id = 11;
$c1->name = 'Foo';
$this->assertEquals(json_decode('{"id":11,"name":"Foo"}'), json_decode(JsUtils::objectToJSON($c1)));
$this->assertEquals(json_decode('{"id":11,"name":"Foo"}'), json_decode(JavascriptUtils::objectToJSON($c1)));

$c2 = new TestClass2(11, "Bar");
$this->assertEquals(json_decode('{"id":11,"name":"Bar"}'), json_decode(JsUtils::objectToJSON($c2)));
$this->assertEquals(json_decode('{"id":11,"name":"Bar"}'), json_decode(JavascriptUtils::objectToJSON($c2)));
}

/**
* Tests JsUtils::wrapScript()
* Tests JavascriptUtils::wrapScript()
*/
public function testWrapScript() {
$s1 = "alert('test');";
$this->assertEquals("<script>alert('test');</script>", JsUtils::wrapScript($s1));
$this->assertEquals("<script>alert('test');</script>", JavascriptUtils::wrapScript($s1));
$s2 = "<script>alert('test');</script>";
$this->assertEquals($s2, JsUtils::wrapScript($s2));
$this->assertEquals($s2, JavascriptUtils::wrapScript($s2));
}
}

0 comments on commit c3aa2d5

Please sign in to comment.