I am very new to codeigniter and postmark, is there a way on how to integrate the two?
I created a controller called cron.php and put the postmark code inside the method inbound, here's my code:
public function inbound()
{
try {
require_once '../lib/Postmark/Autoloader.php';
\Postmark\Autoloader::register();
// this file should be the target of the callback you set in your postmark account
$inbound = new \Postmark\Inbound(file_get_contents('php://input'));
$this->data['client'] = $this->client_model->get_where($m_where);
if(!empty($this->data['client']))
{
$m_insert = array('cme_username' => $inbound->FromName(),
'cme_title' => $inbound->Subject(),
'cme_message' => $inbound->TextBody(),
'cme_client' => $this->data['client']['ccl_id'],
'cme_from' => 'client',
'cme_through' => 'email',
'cme_date_sent' => date('Y-m-d H:i:s'),
'cme_admin' => 0);
$this->message_model->insert($m_insert);
}
}
catch (Exception $e) {
echo $e->getMessage();
}}
I'm also wondering if I am doing it correctly? Many thanks!
You can simply pull one of the example classes from the PostMarkApp.com website (I used this one and renamed it simply postmark) in the application/libraries/ directory and use it like any other CI Library. Make sure you have the proper globals defined (I defined them in application/config/config.php).
application/config/config.php
define('POSTMARKAPP_API_KEY', 'YOUR-POSTMARKAPP-API-KEY');
define('POSTMARKAPP_MAIL_FROM_ADDRESS', 'you#yourdomain.com');
application/controllers/somecontroller.php
class somecontroller extends CI_Controller {
function someControllerMethod(){
$this->load->library('postmark');
$this->postmark->addTo('someone#somewhere.com', 'John Doe')
->subject('My email subject')
->messagePlain('Here is a new message to John!')
->tag('some-tag')
->send();
}
}
Hope that helps!
Related
I would like to write a test for my CommentObserver. This observer is only registered in the NovaServiceProvider but not the AppServiceProvider. This means I cannot test my observer by using my own Controllers.
In my eyes I have 3 ways to test my observer:
Either performing a feature test by sending a post request to the Nova API
Mocking the observer by calling the function in the observer to check if the function perfoms as desired
Trying to register my observer on the fly in the AppServiceProvider, performing a request and deregistering the observer in the AppServiceProvider again.
I tried to find a solution for any of these 3 ways to test my observer but unfortunately I faild with any of them.
Problems:
For way 1 I always get a validation error and Nova tells me that my input is invalid.
For way 2 I fail at mocking the observer function
For way 3 I didn't find any solution on how to register and deregister the oberserver on the fly at the AppServiceProvider
Do you guys have idea and solition on how I can test my CommentObserver (which is as written above only registered in my NovaServiceProvider).
Update:
So, here is the code of my observer. I need to have an valid request to test my observer in order to have the ability to access the $request->input('images') variable. I do know I can also use $comment->content instead of request()->input('content') because $comment->content already contains the new content which is not saved it this point.
The reason why I need a valid request is that the variable images is not part of the Comment model. So I cannot use $comment->images because it simply doesn't exist. That's why I need to access the request input. What my observer is basically doing is to extract the base64 images from the content, saves them to the server and replaces them by an image link.
class CommentObserver
{
public function updating(Comment $comment)
{
if (!request()->input('content')) {
return;
}
if (request()->input('content') == $comment->getRawOriginal('content')) {
return;
}
$images = request()->input('images');
if(!is_array($images)) {
$images = json_decode(request()->input('images'));
}
checkExistingImagesAndDeleteWhenNotFound($comment, request()->input('content'), 'comments', 'medium');
$comment->content = addBase64ImagesToModelFromContent($comment, request()->input('content'), $images, 'comments', 'medium');
}
}
This is my test so far. I choose way 1 but as described already this always leads to an validation error by the nova controller and I cannot figure out what is the error/what is missing or wrong.
class CommentObserverTest extends TestCase
{
/** #test */
public function it_test()
{
$user = User::factory()->create([
'role_id' => Role::getIdByName('admin')
]);
$product = Product::factory()->create();
$comment = Comment::factory()->create(['user_id' => $user->id, 'content' => '<p>Das ist wirklich ein super Preis!</p>', 'commentable_type' => 'App\Models\Product', 'commentable_id' => $product->id]);
$data = [
'content' => '<p>Das ist wirklich ein HAMMER Preis!</p>',
'contentDraftId' => '278350e2-1b6b-4009-b4a5-05b92aedaae6',
'pageStatus' => PageStatus::getIdByStatus('publish'),
'pageStatus_trashed' => false,
'commentable' => $product->id,
'commentable_type' => 'App\Models\Product',
'commentable_trashed' => false,
'user' => $user->id,
'user_trashed' => false,
'_method' => 'PUT',
'_retrieved_at' => now()
];
$this->actingAs($user);
$response = $this->put('http://nova.mywebsiteproject.test/nova-api/comments/' . $comment->id, $data);
dd($response->decodeResponseJson());
$das = new CommentObserver();
}
}
Kind regards and thank you
Why depend on the boot method in your NovaServiceProvider? It is possible to call the observe() method on the fly in your test:
class ExampleTest extends TestCase
{
/** #test */
public function observe_test()
{
Model::observe(ModelObserver::class);
// If you need the request helper, you can add input like so:
request()->merge([
'content' => 'test'
]);
// Fire model event by updating model
$model->update([
'someField' => 'someValue',
]);
// Updating should be triggered in ModelObserver
}
}
It should be now be possible in your observer class:
public function updating(Model $model)
{
dd(request()->input('content')); // returns 'test'
}
I have a Yii 1.x component loaded in the configuration file like so
$config['components']['simplesamlphp'] = array(
'class' => 'application.components.yii-simplesamlphp.components.Simplesamlphp',
'autoloadPath' => SAML_DIR.'/test2/lib/_autoload.php',
'authSource' => 'default-sp',
);
I need to make the autoloadPath property dynamic based on who the user is in the controller. Is this possible? And if so how do I overwrite it?
Probably the best way is to extend Simplesamlphp and configure property in init():
class MySimplesamlphp extends Simplesamlphp {
public $adminAutoloadPath;
public $nonAdminAutoloadPath;
public function init() {
if (Yii::app()->user->isAdmin()) {
$this->autoloadPath = $this->adminAutoloadPath;
} else {
$this->autoloadPath = $this->nonAdminAutoloadPath;
}
parent::init();
}
}
And use new component in config:
$config['components']['simplesamlphp'] = array(
'class' => 'MySimplesamlphp',
'adminAutoloadPath' => SAML_DIR.'/test2-admin/lib/_autoload.php',
'nonAdminAutoloadPath' => SAML_DIR.'/test2/lib/_autoload.php',
'authSource' => 'default-sp',
);
I figured it out overriding yii components is fairly easy even if you dont initialize it in the config.
$component = array(
'class' => 'application.components.yii-simplesamlphp.components.Simplesamlphp',
'autoloadPath' => SAML_DIR.'/'.$tenant_path.'/lib/_autoload.php',
'authSource' => 'default-sp',
); //where $tenant_path is the directory of the component i need based on the tenant
Yii::app()->setComponent('simplesamlphp',$component);
then use the component in your controller like so
Yii::app()->simplesamlphp;
Note that you will only have access to the component within your controller method so all i did was move the that code to its own class and call it when i needed to create a new instance of the component
I created new resources with this code:
class WebserviceRequest extends WebserviceRequestCore {
public static function getResources(){
$resources = parent::getResources();
// if you do not have class for your table
$resources['test'] = array('description' => 'Manage My API', 'specific_management' => true);
$resources['categoryecommerce'] = array('description' => 'o jacie marcin', 'class' => 'CategoryEcommerce');
$mp_resource = Hook::exec('addMobikulResources', array('resources' => $resources), null, true, false);
if (is_array($mp_resource) && count($mp_resource)) {
foreach ($mp_resource as $new_resources) {
if (is_array($new_resources) && count($new_resources)) {
$resources = array_merge($resources, $new_resources);
}
}
}
ksort($resources);
return $resources;
}
}
And new class:
class CategoryEcommerceCore extends ObjectModelCore {
public $category_id;
public $category_core_id;
public static $definition = array(
'table' => "category_ecommerce",
'primary' => 'category_id',
'fields' => array(
'category_core_id' => array('type' => self::TYPE_INT),
)
);
protected $webserviceParameters = array();
}
Webservice is override properly. My class WebserviceRequest is copying to
/override/classes/webservice/WebserviceRequest
but class isn't copying to /override/classes/ when i installing my module.
How to add new resourcess with own logic ? I want to add categories within relation to my table.
Regards
Martin
As soon as there is literally nothing regarding the API except Webkul tutorial... I tried to implement the "Webkul's" tutorial, but also failed. However seems that it's better to use hooks instead of overrides. I used my "reverse engineering skills" to determine the way to create that API, so-o-o-o, BEHOLD! :D
Let's assume you have a custom PrestaShop 1.7 module. Your file is mymodule.php and here are several steps.
This is an install method wich allows you to register the hook within database (you can uninstall and reinstall the module for this method to be executed):
public function install() {
parent::install();
$this->registerHook('addWebserviceResources');
return true;
}
Add the hook listener:
public function hookAddWebserviceResources($resources) {
$added_resources['test'] = [
'description' => 'Test',
'specific_management' => true,
];
return $added_resources;
}
That specific_management option shows you are going to use WebsiteSpecificManagement file instead of database model file.
Create WebsiteSpecificManagement file, called WebsiteSpecificManagementTest (Test - is CamelCased name of your endpoint). You can take the skeleton for this file from /classes/webservice/WebserviceSpecificManagementSearch.php. Remove everything except:
setObjectOutput
setWsObject
getWsObject
getObjectOutput
setUrlSegment
getUrlSegment
getContent (should return $this->output; and nothing more)
manage - you should rewrite it to return/process the data you want.
Add
include_once(_PS_MODULE_DIR_.'YOURMODULENAME/classes/WebserviceSpecificManagementTest.php');
to your module file (haven't figured out how to include automatically).
Go to /Backoffice/index.php?controller=AdminWebservice and setup the new "Auth" key for your application, selecting the test endpoint from the permissions list. Remember the key.
Visit /api/test?ws_key=YOUR_KEY_GENERATED_ON_STEP_4 and see the XML response.
Add &output_format=JSON to your URL to see the response in JSON.
You have to use something like $this->output = json_encode(['blah' => 'world']) within manage method at WebsiteSpecificManagementTest.
In my controller function I am using a require statement to include a file:
require app_path().'/plivo/plivo.php';
After this statement, I try to redirect from this controller using the following statement:
return Redirect::back()->with('success', 'Note added successfully');
However, this gives me the following error:
Call to undefined method Redirect::back()
How can I redirect from this function?
This is my full code:
public function sendSMS(){
require app_path().'/plivo/plivo.php';
$auth_id = "XXXXXXXXXXXX";
$auth_token = "XXXXXXXXXXXXXXXXXXXXX";
$p = new \RestAPI($auth_id, $auth_token);
$params = array(
'src' => '1XX7XX0',
'dst' => '91XXXXXXXXX7',
'text' => 'Test SMS',
'method' => 'POST'
);
$response = $p->send_message($params);
return Redirect::back()->with('success', 'Note added successfully');
}
This answer assumes that plivo.php is from this git repo.
The issue is that the plivo.php library defines a Redirect class in the global namespace. Because of this, Laravel does not register the global Redirect alias to point to the Illuminate\Support\Facades\Redirect facade.
So, in your final line return Redirect::back()->with(...);, the Redirect class being used is the class defined in the plivo.php library, not Laravel's Illuminate\Support\Facades\Redirect class.
The quickest fix would be to change your line to:
return Illuminate\Support\Facades\Redirect::back()->with('success', 'Note added successfully');
Another option would be to inject Laravel's redirector into your controller, and use that instead of using the facade:
class MyController extends BaseController {
public function __construct(\Illuminate\Routing\Redirector $redirector) {
$this->redirector = $redirector;
}
public function sendSMS() {
require app_path().'/plivo/plivo.php';
//
return $this->redirector->back()->with('success', 'Note added successfully');
}
}
A third option would be to update your code to use the plivo composer package, which has a namespace. The updates have been done in the dev branch of the repo, which you can find here. If you did this, you would get rid of your require statement and use the namespaced plivo classes.
I'm very new to Laravel and unit testing in general. I'm trying to write some tests for my AccountController and I've run into a road block.
I'm using Sentry to handle users and groups in the site. I'm trying to test that my controller is handling exceptions thrown by Sentry properly. So my controller method that handles the login POST looks like this:
public function postLogin(){
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
try{
$user = $this->authRepo->authenticate($credentials, true);
return Redirect::route('get_posts');
}
catch (Exception $e){
$message = $this->getLoginErrorMessage($e);
return View::make('login', array('errorMsg' => $message));
}
}
authRepository is just a repository that uses Sentry to handle authentication. Now I want to test that when an email address is not specified a LoginRequiredException is thrown and the user sees the error message. Here is my test:
public function testPostLoginNoEmailSpecified(){
$args = array(
'email' => 'test#test.com'
);
$this->authMock
->shouldReceive('authenticate')
->once()
->andThrow(new Cartalyst\Sentry\Users\LoginRequiredException);
$this->action('POST', 'MyApp\Controllers\AccountController#postLogin', $args);
$this->assertViewHas('errorMsg', 'Please enter your email address.');
}
However, the test is not passing. For some reason all it spits out is:
There was 1 error:
1) AccountControllerTest::testPostLoginNoEmailSpecified
Cartalyst\Sentry\Users\LoginRequiredException:
Am I using the andThrow() method incorrectly? If anyone can shed any light on what is going on it would be much appreciated.
Thanks in advance!
So I actually just figured the problem out. Turns out it wasn't a problem with my unit tests at all but was actually just a namespacing issue. I forgot the backslash on the Exception class. So in my controller it should have been:
try{
$user = $this->authRepo->authenticate($credentials, true);
return Redirect::route('get_posts');
}
catch (\Exception $e){
$message = $this->getLoginErrorMessage($e);
return View::make('account.login', array('errorMsg' => $message));
}