Magento 2 instance AccountManagement on Command - magento2

How can I instance an Magento\Customer\Model\AccountManagement inside a Command in a custom module?
I'm trying to create a command to reset all customers passwords

You can probably just use DI for the AccountManagementInterface, like this snippet taken from Magento's code:
class Command
{
public function __construct(
AccountManagementInterface $customerAccountManagement
) {
$this->customerAccountManagement = $customerAccountManagement;
}
public function whatever() {
$this->customerAccountManagement->initiatePasswordReset(
$email,
AccountManagement::EMAIL_RESET
);
}
}

Related

How to integrate non Laravel related classes in framework?

I have a class which I would like to use inside of my Laravel App. Where do I place this and how do I integrate in Laravel workflow
what I tried
creating
/app/libraries
--myClass.php
than in composer
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/services",
"app/facades",
"app/libraries",
"public/site"
]
},
composer dump-autoload
Than I would like to initiate inside of a custom Model How would I do that?
under vendor/composer/autoloaded the class is present
than in my model I init autoloaded
public function MyMethod(){
$instance = new MyClass();
var_dump($instance);
// pulled Error : Class 'App\Models\MyClass' not found
}
The work is almost done, now you have to make sure your class appears in
vendor/composer/autoload_classmap.php
Then you just need to use it anywhere:
class Post extends Eloquent {
public function doThing()
{
$instance = new MyClass;
}
}
If you are somehow using namespaces, you have to use them in the top of your class:
use MyClass;
use App\Classes\MyOtherClass;
class Whatever {
public function MyMethod()
{
$instance = new MyClass();
$other = new MyOtherClass();
var_dump($instance);
}
}

Error in using my own Curl class in laravel

i have laravel 4 installed in my wamp server. this what i did :
1-add this "app/classes" to composer.json.
2-create folder classes in app and put Curl.php class in that folder.
3-add this app_path().'/classes', to global.php inside app/start.
4-run composer dump-autoload in command in www directory.
5-for using like Curl::help() must add this alias to app/config/app.php aliases section 'Curl'=>'Curl' .
after doing this when i return return Curl::hello(); in router this page comes :
http://www.mediafire.com/view/h9489jr5s2699ty/err.PNG
my Curl's class : Curl class
any help??
This is not how Laravel aliases works, you need more code (create Facades and Service Providers) to make it work.
So you have some options:
1) Remove the Alias from app/config/app.php and instantiate your class:
$curl = new Curl;
$curl->help();
2) Instantiate your class and bind it to the IoC container, in global.php, filters.php or create a file for that:
App::bindShared('mycurl', function($app)
{
return new Curl;
});
And create a Facade:
<?php namespace MyClasses\Facades;
use Illuminate\Support\Facades\Facade;
class MyCurlFacade extends Facade {
protected static function getFacadeAccessor()
{
return 'mycurl';
}
}
Your Alias has to point to this Facade script file, like all the others you see in app.php.
'Curl' => 'MyClasses\MyCurlFacade',
And it should work like this Curl::hello();.
3) Create the usual (correct?) Laravel structure, which also includes a ServiceProvider to instantiate your class and bind it to the IoC container in the application Boot:
<?php namespace MyClasses;
use Illuminate\Support\ServiceProvider;
class MyCurlServiceProvider extends ServiceProvider {
protected $defer = false;
public function boot()
{
}
public function register()
{
{
$this->app['mycurl'] = $this->app->share(function($app)
{
return new MyCurl;
});
}
public function provides()
{
return array('mycurl');
}
}
THIS IS UNTESTED CODE, SO DO NOT EXPECT IT TO WORK IN THE FIRST RUN

PHPUnit: How to run tests from browser?

I know PHPUnit tests can be executed from the command line, but is there an easy way to run it from the browser. For example, I have:
class testsSuite extends PHPUnit_Framework_TestSuite
{
public function __construct ()
{
$this->setName('testsSuite');
$this->addTestSuite('MyFirstTest');
}
public static function suite ()
{
return new self();
}
}
And then I have:
class MyFirstTest extends PHPUnit_Framework_TestCase
{
protected function setUp ()
{
parent::setUp();
}
protected function tearDown ()
{
parent::tearDown();
}
public function __construct ()
{
}
public function test__construct ()
{
}
public function test__destruct () {
}
public function testCalculateCost ()
{
print 1; die();
}
}
Is there a URL I can enter in my browser to execute this test? Something like:
http://localhost/tests/testsSuite/calculateCost
Or something similar
There is VisualPHPUnit.
At work we sometimes run from browser, using in its basic form a php-script containing:
$argv = array(
'--configuration', 'phpunit.xml',
'./unit',
);
$_SERVER['argv'] = $argv;
PHPUnit_TextUI_Command::main(false);
So you basically put all parameters you normally type on the commandline in an array, and set it in the $_SERVER-global. PHPUnit_TextUI_Cmmand::main(false); then runs your tests. The false-parameter makes sure no exit() is called.
In the PHPUnit.xml I configure to log to a JSON file, and the php-script reads that file to show the results (which it can do after the tests because no exit was called).
Note: this is very barebone, simplistic and crude.
I have found a solution that works well:
<?php
define("PHPUNIT_COMPOSER_INSTALL","vendor/autoload.php");
require PHPUNIT_COMPOSER_INSTALL;
$query = $_SERVER["QUERY_STRING"];
//$_SERVER["QUERY_STRING"] to $_SERVER['argv'];
$_SERVER['argv'] = explode("&",$query);
//PHPUnit use $_SERVER['argv'] for input value
PHPUnit\TextUI\Command::main(false);
/*Use
command line "./vendor/bin/phpunit param1 param2 param..."
browser URI "localhost?param1&param2&param..."
*/

Zend: Action helper not found

I am trying to create a helper of my own. The Safecheck folder is located in the library folder and contains a Helper folder. The class is called Safecheck_Helper_Authority.php (inside library/Safecheck/Helper).
In Bootstrap.php:
protected function _initHelper()
{
Zend_Controller_Action_HelperBroker::addPrefix('Safecheck_Helper');
}
In Safecheck_Helper_Authority.php:
class Safecheck_Helper_Authority extends Zend_Controller_Action_Helper_Abstract
{
public function hasAuthority($userId, array $ids)
{
}
}
I want to user the functions inside this class. But I get the error "Message: Action Helper by name Authority not found", triggered by the following code:
$this->_helper->authority('hasAuthority');
Maybe I am not calling it with the right code? Am I missing something?
in order to call an action helper in this manner $this->_helper->authority('hasAuthority'); you need to define the direct() method in your helper.
class Safecheck_Helper_Authority extends Zend_Controller_Action_Helper_Abstract
{
public function direct($userId, array $ids)
{
// do helper stuff here
}
}
an easy way to register the helper path and prefix is to use the application.ini:
resources.frontController.actionhelperpaths.Safecheck_Helper = APPLICATION_PATH "/../library/Safecheck/Helper"
to do it in bootstrap (not sure if addPrefix() works with library namespaces):
protected function _initHelper()
{
//addPath(path_to_helper, helper_prefix)
Zend_Controller_Action_HelperBroker::addPath('/../library/Safecheck/Helper', 'Safecheck_Helper');
}
a Simple example of an action helper:
class Controller_Action_Helper_Login extends Zend_Controller_Action_Helper_Abstract
{
//prepares a login form for display
public function direct()
{
$form = new Application_Form_Login();
$form->setAction('/index/login');
return $form;
}
}
Have in your application.ini something similar to
resources.frontController.actionhelperpaths.Application_Action_Helper = APPLICATION_PATH "/../classes/Application/Action/Helper"
The path should be changed to reflect your file path.

Probable reasons why autoloading wont work in Zend Framework 1.10.2?

Iam writing an application using Zend Framework 1.10.2.
I created few model classes and a controller to process them.
When Iam executing my application and accessing the admin controller. Iam seeing this error.
Fatal error: Class 'Application_Model_DbTable_Users' not found in C:\xampp\htdocs\bidpopo\application\controllers\AdminController.php on line 16
The error clearly shows its an autoloading error.
Hence I wrote this code in the bootstrap file.
protected function initAutoload()
{
$modeLoader = new Zend_Application_Module_AutoLoader(array
('namespace'=>'','basePath'=>APPLICATION_PATH ));
//echo(APPLICATION_PATH);
return $modeLoader;
}
Still the error remains :( . Can anyone suggest me what Iam missing out here?
This is the location of the Model Users class.
C:\xampp\htdocs\bidpopo\application\models\DbTable\Users.php
This is its code.
class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
//put your code here
protected $_name='users';
public function getUser($id)
{
$id = (int)$id;
$row = $this->fetchrow('id='.$id);
if(!$row)
{throw new Exception("Could not find row id - $id");}
return $row->toArray();
}
public function addUser($userDetailArray)
{
$this->insert($userDetailsArray);
}
public function updateUser($id,$userDetailArray)
{
$this->update($userDetailArray,'id='.(int)$id);
}
public function deleteUser($id)
{
$this->delete('id='. (int)$id);
}
}
This is the Admin Controller's code
class AdminController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$this->view->title= "All Users";
$this->view->headTitle($this->view->title);
$users = new Application_Model_DbTable_Users();
$this->view->users = $users->fetchAll();
}
public function addUserAction()
{
// action body
}
public function editUserAction()
{
// action body
}
public function deleteUserAction()
{
// action body
}
You application classes don't follow the proper naming convention for the namespace you've set. The Zend_Application_Module_AutoLoader is a little different than the normal autoloader in that it doesn't simply change the '_' in the class name with '/'. It looks at the second part of the class name and then checks a folder for the existence of the class based on that.
You need to change the line:
$modeLoader = new Zend_Application_Module_AutoLoader(array(
'namespace'=>'Application',
'basePath'=>APPLICATION_PATH
));
This means it will autoload all module classes prefixed with 'Application_'. When it the second part of the class is 'Model_' it will look in "{$basePath}/models" for the class. The '_' in the rest of the class name will be replaced with '/'. So the file path of the file will be "{$basePath}/models/DbTable/Users.php".
Read more here.