Running PHPUnit TestCase( not the framework native) on Yii2 - yii2

I am trying to run PHPUnit_Framework_TestCase on Yii2-advanced. Installed it via composer in folder PHPUnit in the root directory. From there I tried to test with simple test but what I get is an error:
phpunit TestLogin.php
PHP Fatal error: Class 'dektrium\user\models\LoginForm' not found in Z:\toma\toma-metropizza\htdocs\frontend\models\LoginForm.php on line 8
Fatal error: Class 'dektrium\user\models\LoginForm' not found in Z:\toma\toma-metropizza\htdocs\frontend\models\LoginForm.php on line 8
This my TestLogin.php file from the PHPUnit directroy:
<?php
require_once "vendor/autoload.php";
require_once "../frontend/models/LoginForm.php";
class TestLogin extends PHPUnit\Framework\TestCase
{
public function setUp()
{
$login = new LoginForm();
}
public function testStatic()
{
$this->assertClassHasStaticAttribute('test', 'LoginForm');
}
}
This is my LoginForm.php:
<?php
namespace frontend\models;
use Yii;
use \dektrium\user\models\LoginForm as BaseLoginForm;
class LoginForm extends BaseLoginForm
{
public static $test = [];
public function attributeLabels()
{
return [
'login' => Yii::$app->OutData->getLabel(208),
'password' => Yii::$app->OutData->getLabel(98),
'rememberMe' => Yii::t('app','app.Remember me'),
];
}
}
It extends the base dektrium user model. But it seems like the test can't find the base model or something. Can you give me advice? I don't want to use the Yii2 build in tests. Want to write my own. Thank you in advance!
EDIT Directroy structure:
app/
/frontned
/models
LoginForm.php
/PHPUnit
/vendor
composer.json
conposer.lock
TestLogin.php
/vendor
etc.

Related

how to use dynemodb with lumen or laravel

I want to use dynemodb and mysql both with lumen.
I have follow below steps,
https://github.com/aws/aws-sdk-php-laravel
from above url I have add package for aws sdk for lumen
and add my accesskey and secret key in .env file
in bootstrap/app.php
I have add $app->register(Aws\Laravel\AwsServiceProvider::class);
Now I want to use dynemodb with lumen to execute query
for execute dynemodb query same as eloquent I have used below package.
https://github.com/baopham/laravel-dynamodb
now I have write my code in model as below,
<?php
namespace App\Models;
use BaoPham\DynamoDb\Facades\DynamoDb;
use BaoPham\DynamoDb\DynamoDbModel;
class CategoryMaster extends BaoPham\DynamoDb\DynamoDbModel
{
protected $table = 'category_master';
protected $fillable = ['id', 'category_name'];
public static function listname()
{
$model = DynamoDbModel::where(['category_name' => 'blue']);
$query = $model->get();
echo"<pre>";print_r($query);die;
}
}
it gives me arror like below,
FatalErrorException in CategoryMaster.php line 8:
Class 'App\Models\BaoPham\DynamoDb\DynamoDbModel' not found
can you help me to resolve thais issue to use dynemodb
I implemented dynamodb in laravel project using baopham package.
In .env file define dynamodb credentials
DYNAMODB_CONNECTION=aws
DYNAMODB_KEY=***
DYNAMODB_SECRET=****
DYNAMODB_REGION=us-east-1
In Model file
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends \BaoPham\DynamoDb\DynamoDbModel
{
protected $table = 'Users'; //table name
protected $guarded = [];
}
In controller file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User; //include your model file
class UserController extends Controller
{
public function index()
{
$user = User::all(); // to get all data from user table
return view('products.index')->with('user', $user);
}
}
for more referance refer https://github.com/baopham/laravel-dynamodb query section.
I found my solution,
I have followed below site step by step and I am able to connect to dynemo db with lumen and able to fire eloquent queries
https://github.com/aws/aws-sdk-php-laravel
https://github.com/baopham/laravel-dynamodb
https://github.com/laravelista/lumen-vendor-publish

how to create console command in a module?

console command, like ./yii hello/world.
I'm using yii-app-basic.
what I want is not create console command in the dir commands/ but in a module.
1) Your module should implements BootstrapInterface :
class Module extends \yii\base\Module implements \yii\base\BootstrapInterface
{
public function bootstrap($app)
{
if ($app instanceof \yii\console\Application) {
$this->controllerNamespace = 'app\modules\my_module\commands';
}
}
}
2) Create your console controller in your module commands folder :
namespace app\modules\my_module\commands;
class ConsoleController extends \yii\console\Controller
{
public function actionIndex()
{
echo "Hello World\n";
}
}
3) Add your module to your app console configuration config/console.php :
'bootstrap' => [
// ... other bootstrap components ...
'my_module',
],
'modules' => [
// ... other modules ...
'my_module' => [
'class' => 'app\modules\my_module\Module',
],
],
4) You can now use your command :
yii my_module/console/index
Here is a good Tutorial and Discussion.
Follow Below Steps on Tutorial:
1) Create a new module in your application.
2) Edit the Module.php.
3) Create your folder and command inside your module.
4) Add your module to app configurations.

Upgrading Laravel 4.2 to 5.0, getting [ReflectionException] Class App\Http\Controllers\PagesController does not exist

I was updating my project from laravel 4.2 to laravel 5.0. But, after I am facing this error and have been trying to solve it for the past 4 hours.
I didn't face any error like this on the 4.2 version. I have tried composer dump-autoload with no effect.
As stated in the guide to update, I have shifted all the controllers as it is, and made the namespace property in app/Providers/RouteServiceProvider.php to null. So, I guess all my controllers are in global namespace, so don't need to add the path anywhere.
Here is my composer.json:
"autoload": {
"classmap": [
"app/console/commands",
"app/Http/Controllers",
"app/models",
"database/migrations",
"database/seeds",
"tests/TestCase.php"
],
Pages Controller :
<?php
class PagesController extends BaseController {
protected $layout = 'layouts.loggedout';
public function getIndex() {
$categories = Category::all();
$messages = Message::groupBy('receiver_id')
->select(['receiver_id', DB::raw("COUNT('receiver_id') AS total")])
->orderBy('total', 'DESC'.....
And, here is BaseController.
<?php
class BaseController extends Controller {
//Setup the layout used by the controller.
protected function setupLayout(){
if(!is_null($this->layout)) {
$this->layout = View::make($this->layout);
}
}
}
In routes.php, I am calling controller as follows :
Route::get('/', array('as' => 'pages.index', 'uses' => 'PagesController#getIndex'));
Anyone please help. I have been scratching my head over it for the past few hours.
Routes are loaded in the app/Providers/RouteServiceProvider.php file. If you look in there, you’ll see this block of code:
$router->group(['namespace' => $this->namespace], function($router)
{
require app_path('Http/routes.php');
});
This prepends a namespace to any routes, which by default is App\Http\Controllers, hence your error message.
You have two options:
Add the proper namespace to the top of your controllers.
Load routes outside of the group, so a namespace isn’t automatically prepended.
I would go with option #1, because it’s going to save you headaches in the long run.

CakePhp 3.0 - Missing Helper

I need to use the UploadPack plugin in my CakePhp project, but my cake doesn't find the plugin's Helper. I actually tried to create some custom helpers for myself as well, but none has worked. I have the helper files saved in app/src/View/Helper/ (my custom helper) and app/plugins/UploadPack/src/View/Helper/ (the plugin's helper, I didn't create nor modify this), I have referenced the helpers in $helpers[] in the right controllers, and I don't know what else is left to be done. I don't know if code is relevant, but I'll post them below anyway. Please help.
app/src/Controller/UsersController.php
<?php
namespace App\Controller;
use App\Controller\AppController;
class UsersController extends AppController {
var $name = 'Users';
var $uses = array('User');
var $helpers = ['Html', 'Form', 'UploadPack.Upload'];
public function add() {
// Do not let logged in users register a new account
if(!isset($this->request->session()->read('Auth')['User']) ||
$this->request->session()->read('Auth')['User']['role']==='admin'){
$user = $this->Users->newEntity($this->request->data);
$user->properties = 'user';
if ($this->request->is('post')) {
if ($this->Users->save($user)) {
$this->Flash->success('The user has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The user could not be saved. Please, try again.');
}
}
$this->set(compact('user'));
} else {
$this->Flash->error('You are already logged in.');
return $this->redirect(['controller' => 'comments', 'action' => 'index']);
}
}
// Irrelevant code below
}
Some file architecture (including some probably irrelevant directories)
app/
bin/
config/
plugins/
UploadPack/
src/
Model/
View/
Helper/
UploaderHelper.php
src/
Controller/
UsersController.php
Templates/
Comments/
Users/
add.ctp
edit.ctp
index.ctp
view.ctp
Output when accessing any file in localhost:8765/users/
Error: UploadPack.UploadHelper could not be found. Make sure your plugin was
loaded from config/bootstrap.php and Composer is able to autoload its classes,
see Loading a plugin and Plugins - autoloading plugin classes
Error: Create the class UploadHelper below in file:
/home/eburn/comments/plugins/UploadPack/src/View/Helper/UploadHelper.php

Namespaced helper won't load in Laravel 4

I have a controller that calls a helper class in my app/helpers directory and then that helpers calls another class within it's namespace, but it can't find that class.
So here is my controller:
<?php
namespace App\Controllers\Dash;
use \App\Models\SalesFlyer;
use \App\Helpers\MyPdf;
class FlyerBuilderController extends BaseController {
public function getPdf($flyerId = null) {
$flyer = new SalesFlyer();
$flyerData = $flyer->getSalesFlyerName($flyerId);
$flyerPath = public_path().'/assets/media/flyers/'.Session::get('userid').'/'.$flyerData->name.'-'.$flyerId.'.html';
return MyPdf::downloadPdf($flyerPath, $flyerData->name);
}
}
It catches MyPdf class perfectly fine. Here is MyPdf class:
<?php
namespace App\Helpers;
class MyPdf {
public static function downloadPdf($filePath, $filename) {
$client = new PdfCrowd("anthonythomas", "1ebd0d6e3ec1dfa83a6c5f3dd32906f0");
// other code here
}
}
The PdfCrowd class is within App\Helpers namespace like so:
<?php
namespace App\Helpers;
//
// Pdfcrowd API client.
//
class PdfCrowd { }
Class 'App\Helpers\PdfCrowd' not found
Here is my start/global.php file:
<?php
ClassLoader::addDirectories(array(
app_path().'/commands',
app_path().'/controllers',
app_path().'/controllers/dash',
app_path().'/controllers/dash/product',
app_path().'/models/Product',
app_path().'/models',
app_path().'/database/seeds',
app_path().'/helpers',
));
Then here is my composer:
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/controllers/dash",
"app/controllers/dash/product",
"app/models",
"app/models/Product",
"app/helpers",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php"
]
}
Any idea why I'm getting that error?..
Everything looks fine but you also have to remember to
composer dump-autoload
Every time you create a new class. Also, check the file
vendor/composer/autoload_classmap.php
You must see your Helper class there.
But if you use PSR-4, you can use the same namespace and you won't have execute composer dump-autoload again:
"autoload": {
"psr-4": {
"App\\Helpers\\": "app/helpers"
}
},
Just remember to remove "app/helpers", from the classmap.
Ok for a shared hosting provider... EVERYTIME you add a new namespace and update composer even with psr-4 it seems, you have to replace the vendor directory with the current one on your local machine for it to actually go through! This saved me so much hours of time after I realized I had to replace the vendor directory everytime a composer dump-autoload was issued locally.