tests for php and html dependencies
This commit is contained in:
parent
ffa9e410b4
commit
54fbc8ee22
4 changed files with 243 additions and 2 deletions
22
tests/samples/codefiles/html-with-php.html
Normal file
22
tests/samples/codefiles/html-with-php.html
Normal file
|
@ -0,0 +1,22 @@
|
|||
Name: <input type="text" name="name" value="<?php echo $name;?>">
|
||||
|
||||
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
|
||||
|
||||
Website: <input type="text" name="website" value="<?php echo $website;?>">
|
||||
|
||||
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
|
||||
<?php
|
||||
|
||||
use One\Full\Classname as Another, Two\Full\NSname;
|
||||
|
||||
?>
|
||||
|
||||
Gender:
|
||||
<input type="radio" name="gender"
|
||||
<?php if (isset($gender) && $gender=="female") echo "checked";?>
|
||||
value="female">Female
|
||||
<input type="radio" name="gender"
|
||||
<?php if (isset($gender) && $gender=="male") echo "checked";?>
|
||||
value="male">Male
|
||||
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
|
116
tests/samples/codefiles/php.php
Normal file
116
tests/samples/codefiles/php.php
Normal file
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
/**
|
||||
* Example PHP Code
|
||||
*/
|
||||
|
||||
namespace ThisIsMy\FakeNamespace;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
//Define autoloader
|
||||
function __autoload($className) {
|
||||
if (file_exists($className . '.php')) {
|
||||
require_once $className . '.php';
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
require 'ServiceLocator.php';
|
||||
|
||||
require "ServiceLocatorTwo.php";
|
||||
|
||||
require $classname . '.class.php';
|
||||
|
||||
use FooBarOne\Classname as Another;
|
||||
|
||||
// this is the same as use FooBarTwo\Full\NSname as NSnameTwo
|
||||
use FooBarTwo\Full\NSnameTwo;
|
||||
|
||||
// importing a global class
|
||||
use ArrayObject;
|
||||
|
||||
// importing a function (PHP 5.6+)
|
||||
use function FooBarThree\Full\functionNameThree;
|
||||
|
||||
// aliasing a function (PHP 5.6+)
|
||||
use function FooBarFour\Full\functionNameFour as func;
|
||||
|
||||
// importing a constant (PHP 5.6+)
|
||||
use const FooBarSix\Full\CONSTANT;
|
||||
|
||||
// multiple import statements combined
|
||||
use FooBarSeven\Full\ClassnameSeven as AnotherSeven, FooBarEight\Full\NSnameEight;
|
||||
|
||||
class ServiceManager implements ServiceLocatorInterface, ContainerInterface
|
||||
{
|
||||
/**@#+
|
||||
* Constants
|
||||
*/
|
||||
const SCOPE_PARENT = 'parent';
|
||||
const SCOPE_CHILD = 'child';
|
||||
/**@#-*/
|
||||
/**
|
||||
* Lookup for canonicalized names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $canonicalNames = [];
|
||||
/**
|
||||
* @var string|callable|\Closure|FactoryInterface[]
|
||||
*/
|
||||
protected $factories = [];
|
||||
/**
|
||||
* @var AbstractFactoryInterface[]
|
||||
*/
|
||||
protected $abstractFactories = [];
|
||||
/**
|
||||
* @var array[]
|
||||
*/
|
||||
protected $delegators = [];
|
||||
|
||||
/**
|
||||
* Add abstract factory
|
||||
*
|
||||
* @param AbstractFactoryInterface|string $factory
|
||||
* @param bool $topOfStack
|
||||
* @return ServiceManager
|
||||
* @throws Exception\InvalidArgumentException if the abstract factory is invalid
|
||||
*/
|
||||
public function addAbstractFactory($factory, $topOfStack = true)
|
||||
{
|
||||
if (true) {
|
||||
array_unshift($this->abstractFactories, $factory);
|
||||
} else {
|
||||
array_push($this->abstractFactories, $factory);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister a service
|
||||
*
|
||||
* Called when $allowOverride is true and we detect that a service being
|
||||
* added to the instance already exists. This will remove the duplicate
|
||||
* entry, and also any shared flags previously registered.
|
||||
*
|
||||
* @param string $canonical
|
||||
* @return void
|
||||
*/
|
||||
protected function unregisterService($canonical)
|
||||
{
|
||||
$types = ['invokableClasses', 'factories', 'aliases'];
|
||||
foreach ($types as $type) {
|
||||
if (isset($this->{$type}[$canonical])) {
|
||||
unset($this->{$type}[$canonical]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (isset($this->instances[$canonical])) {
|
||||
unset($this->instances[$canonical]);
|
||||
}
|
||||
if (isset($this->shared[$canonical])) {
|
||||
unset($this->shared[$canonical]);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -336,3 +336,106 @@ class DependenciesTestCase(utils.TestCase):
|
|||
dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']
|
||||
self.assertListsEqual(dependencies, expected_dependencies)
|
||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||
|
||||
def test_php_dependencies_detected(self):
|
||||
response = Response()
|
||||
response.status_code = 0
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
||||
now = u(int(time.time()))
|
||||
entity = 'tests/samples/codefiles/php.php'
|
||||
config = 'tests/samples/configs/good_config.cfg'
|
||||
|
||||
args = ['--file', entity, '--config', config, '--time', now]
|
||||
|
||||
retval = execute(args)
|
||||
self.assertEquals(retval, 102)
|
||||
self.assertEquals(sys.stdout.getvalue(), '')
|
||||
self.assertEquals(sys.stderr.getvalue(), '')
|
||||
|
||||
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||
|
||||
heartbeat = {
|
||||
'language': u('PHP'),
|
||||
'lines': ANY,
|
||||
'entity': os.path.realpath(entity),
|
||||
'dependencies': ANY,
|
||||
'project': u(os.path.basename(os.path.realpath('.'))),
|
||||
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
||||
'time': float(now),
|
||||
'type': 'file',
|
||||
}
|
||||
stats = {
|
||||
u('cursorpos'): None,
|
||||
u('dependencies'): ANY,
|
||||
u('language'): u('PHP'),
|
||||
u('lineno'): None,
|
||||
u('lines'): ANY,
|
||||
}
|
||||
expected_dependencies = [
|
||||
'Interop',
|
||||
'FooBarOne',
|
||||
'FooBarTwo',
|
||||
'FooBarThree',
|
||||
'FooBarFour',
|
||||
'FooBarSeven',
|
||||
'FooBarEight',
|
||||
'ArrayObject',
|
||||
"'ServiceLocator.php'",
|
||||
"'ServiceLocatorTwo.php'",
|
||||
]
|
||||
|
||||
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
||||
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||
dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']
|
||||
self.assertListsEqual(dependencies, expected_dependencies)
|
||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||
|
||||
def test_php_in_html_dependencies_detected(self):
|
||||
response = Response()
|
||||
response.status_code = 0
|
||||
self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response
|
||||
|
||||
now = u(int(time.time()))
|
||||
entity = 'tests/samples/codefiles/html-with-php.html'
|
||||
config = 'tests/samples/configs/good_config.cfg'
|
||||
|
||||
args = ['--file', entity, '--config', config, '--time', now]
|
||||
|
||||
retval = execute(args)
|
||||
self.assertEquals(retval, 102)
|
||||
self.assertEquals(sys.stdout.getvalue(), '')
|
||||
self.assertEquals(sys.stderr.getvalue(), '')
|
||||
|
||||
self.patched['wakatime.session_cache.SessionCache.get'].assert_called_once_with()
|
||||
self.patched['wakatime.session_cache.SessionCache.delete'].assert_called_once_with()
|
||||
self.patched['wakatime.session_cache.SessionCache.save'].assert_not_called()
|
||||
|
||||
heartbeat = {
|
||||
'language': u('HTML+PHP'),
|
||||
'lines': ANY,
|
||||
'dependencies': ANY,
|
||||
'entity': os.path.realpath(entity),
|
||||
'project': u(os.path.basename(os.path.realpath('.'))),
|
||||
'branch': os.environ.get('TRAVIS_COMMIT', ANY),
|
||||
'time': float(now),
|
||||
'type': 'file',
|
||||
}
|
||||
stats = {
|
||||
u('cursorpos'): None,
|
||||
u('dependencies'): ANY,
|
||||
u('language'): u('HTML+PHP'),
|
||||
u('lineno'): None,
|
||||
u('lines'): ANY,
|
||||
}
|
||||
expected_dependencies = [
|
||||
'"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"',
|
||||
]
|
||||
|
||||
self.patched['wakatime.offlinequeue.Queue.push'].assert_called_once_with(heartbeat, ANY, None)
|
||||
self.assertEquals(stats, json.loads(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][1]))
|
||||
dependencies = self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0]['dependencies']
|
||||
self.assertListsEqual(dependencies, expected_dependencies)
|
||||
self.patched['wakatime.offlinequeue.Queue.pop'].assert_not_called()
|
||||
|
|
|
@ -61,10 +61,10 @@ class PhpParser(TokenParser):
|
|||
|
||||
def _process_literal_string(self, token, content):
|
||||
if self.state == 'include':
|
||||
if content != '"':
|
||||
if content != '"' and content != "'":
|
||||
content = content.strip()
|
||||
if u(token) == 'Token.Literal.String.Double':
|
||||
content = u('"{0}"').format(content)
|
||||
content = u("'{0}'").format(content)
|
||||
self.append(content)
|
||||
self.state = None
|
||||
|
||||
|
|
Loading…
Reference in a new issue