I am trying to start PHPUnit testing.
I am using composer to load PHPUnit 4.5 , Yii 1.1.14 and some custom Yii packages that we have built.
Inside those custom packages, we autoload some files that set some aliases using the Yii class.
When running our application, we include the base Yii file manually, and then run the composer generated autoloads.
The trouble is, when we run PHPUnit.. the composer autoloads get run first. Even when specifying a bootstrap file with the include:
bin/phpunit --bootstrap carcass/phpunit.bootstrap.php
Leading to the following Exception:
Fatal error: Class 'Yii' not found
In fact it appears the autoloads are run even before the -- options are parsed:
bin/phpunit --help
results in the same error. Removing the autoloads allows PHPunit to run.
Is there any way around this?
I tried placing an autoload for the Yii base file in our main composer.json, but the sub-packages' autoloads get run first.. same error.
I also tried placing an autoload for the Yii base file in each of the sub-packages.. but then we get redeclaration errors as composer uses require. I'm also not a massive fan of this option as it rigidly defines where the Yii definition comes from to sub-packages that don't really need to know.
As the autoload classmap section is run first before all the files sections (including those from the sub-packages).
Placing the yii and YiiBase files in the classmap of the main composer.json for our project solved this issue:
"autoload": {
"classmap": [
"composer_packages/yiisoft/yii/framework/YiiBase.php",
"composer_packages/yiisoft/yii/framework/yii.php"
],
"files": [
...
]
}
Related
I'm using requireJS with karma and jasmine to test Dojo code.
As we know, require brings in all dependencies within each file, which is fine, however I get a script error when any of the files have language files (e.g. dojox/date/buddhist.js) as a dependency.
The language file is laid out as follows:
define([
"..",
"dojo/_base/lang",
"dojo/date",
"./buddhist/Date"
], function(
dojox,
lang,
dd,
BDate
) { /* etc etc */
When referencing e.g. "dojo/_base/lang" this is easy to map to the file, however, notice the mapping to dojox is ".." which actually goes back to a directory and not a single file.
This is the error I get in the terminal or console in browser:
Error: Script error for "dojox", needed by: dojox/date/buddhist, dojox/date/buddhist/locale, dojox/date/hebrew, dojox/date/hebrew/locale, dojox/date/islamic, dojox/date/islamic/locale
http://requirejs.org/docs/errors.html#scripterror
I've tried defining dojox in my test-main file but same error.
So what I need to do is to mock dojox, is there a workaround for this?
I have fixed this by changing the dojox date files to reference an actual file:
Changed ".." to "dojox/main" and mapped to that file.
I changed this within the actual dojo files as I use a copy of them in my test project, not sure if this would work in production but I think it would as it seems dojo have switched to this also in later versions.
I am using ZendFramwork3, i'm new in this so don't know how to autoload the module after creation without terminal command.
I am creating module with programme just by taking module name from the user the following things will be done :
- Module structure for new module with namespace and controller & view files will be placed under module folder
- ModuleName is initialised in modules.config.php in config folder
- Also entry in composer.json in autoload --> psr-4 initialisation done
After that i have to manually run the command "composer dump-autoload" to perfectly work my module but i don't want to do that manually rather i want solution for that to run it programmatically.
Thank you for the solution in advance.
I'm looking for the best way to arrange my code base with common resources shared between different projects. I have three aurelia apps. In each one, I added some resources in its resources folder (custom elements, attributes, etc). When I needed one already wrote in another project, I just pasted it. Now I have time to refacto, I'd like to move all this resources in a dedicated repository. Then I want to be able to pick only the resources I need in each project.
I've tried by putting all me resources in a repo with a gulp build task from aurelia skeleton which allow me to build AMD modules of all my modules. Then, I've been able to load some modules individually by adding them in aurelia.json. For exemple for an attribute:
{
"name": "aurelia-resources-progress-button",
"path": "../node_modules/SHG-aurelia-resources/dist/amd/resources/attributes",
"main": "progress-button"
}
or a custom element:
{
"name": "aurelia-resources-avatar-upload",
"path": "../node_modules/SHG-aurelia-resources/dist/amd/resources/elements/avatar-upload",
"main": "avatar-upload",
"resources": [
"avatar-upload.html",
"avatar-upload.css"
]
}
It worked like a charme but it failed for a value converter which import a module from relative path.
The file is located in :
"projectRoot/node_modules/SHG-aurelia-resources/dist/amd/resources/value-converters/duration-format.js",
it import from '../utils./strings'
and
I get the following error when I au run:
"Error: ENOENT: no such file or directory, open
'/Users/hadrien/Documents/dev/SportHeroes/united-heroes/src/resources/utils/strings.js'".
The strange thing is when I require a relative module from a template (like in my progress-button custom attribute) there is no problem.
I don't want to make a plugin because I don't want to load every modules of my repo. What I'd like, if it is possible, would be to be able to set .feature('../node_modules/path/resources') and load them like I load my local resources.
What should I do ?
I'm answering the question as reworded in the comments above.
If you have an npm package, you can simply require in resources from it using a require element. This npm package could package itself as a plugin, and you simply choose not to load it that way as you only want a subset up the stuff it provides.
I've created a set of sample projects that show this off: https://github.com/AshleyGrant/sample-app-so41961759
This application has a dependency on https://github.com/AshleyGrant/sample-resources-so41961759/
This dependency packages itself as a plugin, but it also can be consumed piecemeal as I am doing in the application by only using one of the two resources the plugin has. Note that the other resource isn't loaded since I'm not using it.
This is what it looks like in the app when I pull in a resource from within the dependency:
<template>
<require from="sample-resources-so41961759/custom-elements/my-echo"></require>
<h1>${message}</h1>
<my-echo say="Echo!"></my-echo>
</template>
I would like to ask what is the purpose of using browserify with noparse option set to true (or how browserify works). For instance:
if browserify does not parse files at all, does it means that it will not find require statements?
if it does not find require statements, then how to force code to load module? For instance, I have toastr & jQuery. toastr requires jQuery. But when I used browserify to create a bundle with noparse set to true, and I added to this bundle both files:
var bundler = browserify();
bundler.add('jquery.js');
bundler.add('toastr.js');
bundler.bundle();
then I get the error, that jQuery module it not found.
Normally when you bundle a file with browserify, it parses the file for require() calls so it can build a dependency graph and bundle the required files. The purpose of the noParse option is to skip that parsing when you don't need or want it. For example, if you're bundling a large library file like jQuery and you know it doesn't contain any require() calls that need to be processed, it will save time in bundling if you noParse that file. Also, it's currently difficult to require() a previously browserified bundle when making a new bundle. In that case you can sometimes resolve the issue by setting noParse for the previously browserified bundle.
if browserify does not parse files at all, does it means that it will not find require statements?
Yes.
I realize that "lib" is no longer autoloaded by default. However, I have this in my application.rb file:
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/datatables)
I have a module in lib called utility.rb, declared as follows:
module MyApp
module Utility
I have some utility methods in there, for example a method that takes an array and turns it into values that can be queried from MySQL. I have:
include MyApp
at the top of the classes that need that method, so that I can then just call:
Utility::array_to_query_string
Unfortunately, this does not work. Whether running a rake task or the application, I am met with:
uninitialized constant MyApp
I don't know how to make Rails require other than what I have above. In the console, if I explicitly type require 'utility' and then I can successfully do the include. What do I have to do to make Rails autoload this module?
The problem could be the directory structure in your lib folder. That the rails autoloader can find your file, it needs to be placed in the right spot. Your MyApp::Utility module should live in a file called: lib/my_app/utility.rb.
If you place the file directly in lib lib/utility.rb the autoloader won't find it.
In some of my apps, I add an initializer that loads my custom code.
In config/initializers/utility.rb,
require "#{Rails.root}/lib/utility"