Getting html script when running a unit test in yii2 framework - php

When I run the test in the console, I am getting HTML and CSS code. The other tests are running fine I have issue only with this one... Can any one help?
thank you
This is my DemoFeature
namespace app\tests\fixtures;
use yii\test\ActiveFixture;
class DemoFixture extends ActiveFixture{
public $modelClass = '\website\models\DemoForm';
}
and this is my DemoTest
namespace app\tests\unit;
use app\tests\fixtures\DemoFixture;
use website\models\DemoForm;
class DemoTest extends \Codeception\Test\Unit{
protected function _before() {
parent::_before();
}
public function _fixtures() {
return ['demo' => DemoFixture::className()];
}
protected function _after() {
}
public function testDemoProcess(){
$demo = new DemoForm;
$demo->name = 'Sample Name';
$demo->company = 'cloudadic'.rand(115, 54785);
$demo->email = 'demo'.rand(115, 547).'#example.com';
$demo->message = 'Sample Company';
$this->assertTrue($demo->save());
}
}

Related

how to send a scheduled report via email to user in laravel

i am trying to send a schedule report (containing charts and graphs) every week to the user via email using Laravel
so , what is the best way to do that
thank you
hi you can make a command and register it in the app/Console/Kernel.php
1)
app/Console/Commands/Reporter.php
use Mail;
use Illuminate\Console\Command;
class Reporter extends Command {
protected $name = 'Reporter';
protected $description = 'This is a test.';
public function __construct()
{
parent::__construct();
}
public function fire()
{
Mail::send('emails.pdf', function($message) use ($path) {
$message->to('testuser1#test.com', 'testuser')->subject
('pdf Report '. Carbon::now()->format('m-d-Y'));
$message->attach($path);
$message->from('no-reply#test.com','test-tech');
});
Storage::disk('pdf')->delete(Storage::disk('pdf')->allFiles());
$this->info('Mail has fired.');
}
}
2.app/Console/Kernel.php
protected $commands = [
'App\Console\Commands\Inspire',
'App\Console\Commands\Reporter',
];
protected function schedule(Schedule $schedule)
{
$schedule->command('inspire')->hourly();
$schedule->command('Reporter')->weekly();
}
you can create pdf with mpdf
https://github.com/mccarlosen/laravel-mpdf
use PDF;
class ReportController extends Controller {
public function generate_pdf()
{
$data = [
'foo' => 'bar'
];
$pdf = PDF::loadView('pdf.document', $data);
return $pdf->stream('document.pdf');
}
}

irazasyed/telegram-bot-sdk get update object on commands

I was using irazasyed/telegram-bot-sdk for a Telegram bot in Laravel. I just wanted to access the update object on my StartCommand class and send a welcome message to a user with his name. The StartCommand class looks like this:
<?php
namespace App\TelegramCommands;
use Telegram\Bot\Commands\Command;
class StartCommand extends Command
{
protected $name = "start";
protected $description = "Lets you get started";
public function handle()
{
$this->replyWithMessage(['text' => 'Welcome ']);
}
}
And the route (which is inside api.php) is:
Route::post('/'.env('TELEGRAM_BOT_TOKEN').'/webhook', function (Request $request) {
$update = Telegram::commandsHandler(true);
return 'ok';
});
I just wanted to access users data when he sends /start command and replay with a message like, "Welcome [his name]". Thank you in advance.
I just have got the answer. It was mainly adding the following to the handle method of StartCommand class:
$update= $this->getUpdate();
So the final file will look like:
<?php
namespace App\TelegramCommands;
use Telegram\Bot\Commands\Command;
class StartCommand extends Command
{
protected $name = "start";
protected $description = "Lets you get started";
public function handle()
{
$update= $this->getUpdate();
$name = $update['message']['from']['first_name'];
$this->replyWithMessage(['text' => 'Welcome '.$name]);
}
}
A good tutorial is here

APCu Adapter & Symfony 3.3 -> Error

I am trying to plug APCu into the Symfony 3.3 test project.
I am getting an error, when I add ApcuAdapter to AppKernel.php.
Here is the list of what I have done:
in ./app/AppKernel.php i have added a "new" line to $bundles in public function registerBundles():
public function registerBundles()
{
$bundles = [
... ,
new Symfony\Component\Cache\Adapter\ApcuAdapter()
];
...
return $bundles;
}
Opened the project's site. It shows an error:
Attempted to call an undefined method named "getName" of class "Symfony\Component\Cache\Adapter\ApcuAdapter".
(./ means the root folder of the project)
Please, tell me why does this error happen and how to plug this adapter into the symfony framework. Thank you.
me have found the solution somewhere on the framework's website.
somehow, we should use not the Adapter, but the Simple instead.
seems very un-logical to me.
so, the Service now works and looks this way:
<?php
namespace AppBundle\Service;
use Symfony\Component\Cache\Simple\ApcuCache;
class ApcuTester
{
public function __construct
(
)
{
}
public function testMe()
{
$cache = new ApcuCache();
$TestVar_dv = 0;
$TestVar_vn = 'TestVar';
$TestVar = NULL;
//$cache-> deleteItem($TestVar_vn); // dbg
// Read
if ( $cache->hasItem($TestVar_vn) )
{
$TestVar = $cache->get($TestVar_vn);
}
else
{
$cache->set($TestVar_vn, $TestVar_dv);
$TestVar = $TestVar_dv;
}
// Modify
$TestVar++;
// Save
$cache->set($TestVar_vn, $TestVar);
// Return
return $TestVar;
}
}
And the Controller which executes this Service looks as this:
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Service\MessageGenerator;
use AppBundle\Service\ApcuTester;
class LuckyController extends Controller
{
/**
* #Route("/lucky/number", name="lucky")
*/
public function numberAction
(
Request $request,
MessageGenerator $messageGenerator,
ApcuTester $apcuTester
)
{
$lucky_number = mt_rand(0, 100);
$message = $messageGenerator->getHappyMessage();
$testvar = $apcuTester->testMe();
$tpl = 'default/lucky_number.html.twig';
$tpl_vars =
[
'lucky_number' => $lucky_number,
'message' => $message,
'testvar' => $testvar
];
return $this->render($tpl, $tpl_vars);
}
}
If i wrote the same thing in pure PHP i would have done it an hour earlier :) Oh these crazy frameworks...

Transactions doesn't work on some tests

I'm working with Laravel 5.2 and phpunit and i'm writing some test for my application. until now i got no problem, and today i encounter something weird and I can't find a way to handle it.
Some of my test file doesn't use transactions although the others does.
i use use DatabaseTransactions; in my TestCase class with is extended in every test file i got.
Most of my test works without any troubles but some of them does not.
Here is one which works withotut any troubles :
class V2LikeTest extends TestCase {
protected $appToken;
protected $headers;
public function setUp() {
parent::setUp();
$this->generateTopic(1);
}
/** LIKE TOPICS */
/** #test */
public function it_likes_a_topic() {
$job = new ToggleLikeJob(Topic::first());
$job->handle();
$topic = Topic::first();
$this->assertTrue($topic->present()->isLiked);
$this->assertEquals(1, $topic->nb_likes);
}
}
and this one with troubles:
class V2TopicTest extends TestCase {
private $appToken;
private $headers;
public function setUp() {
parent::setUp();
$this->generateCompany(1);
}
/** #test */
public function it_create_a_topic() {
$new_topic_request = new Request([
'content' => str_random(100),
'type' => Topic::TYPE_FEED_TEXT
]);
$job = new CreateFeedTopicJob($new_topic_request->all());
$job->handle();
$this->assertCount(1, Topic::all());
}
}
It's been a while now that i'm looking for the solution but not able to find it. Did someone already meet this troubles?
edit: GenerateTopic function use generateCompany just in case ;)

how to write a phptest for controller in fuelphp

I only have a controller and view, I want to mock a http request to test this controller, I use fuelphp, hope someone can give me some advice or demo
class Controller_Index extends Controller_Template{
public function action_index(){
$view = View::forge('index');
$this->template->content = $view;
}
}
I write like this
class Test_Controller_index extends TestCase{
public function TestController(){
$expected = View::forge('index');
$response = Request::forge('index')
->set_method('GET')
->execute()
->response();
$assertValue = $response->body->content;
$this->assertSame($expected, $assertValue);
}
}
php oil test result
There was 1 failure:
1) Warning
No tests found in class "Test_Controller_index".
what's wrong
All unit tests in fuelphp need to take the form of test_ or they won't be recognised.
Try this: (not the different function name)
class Test_Controller_index extends TestCase{
public function test_controller(){
$expected = View::forge('index');
$response = Request::forge('index')
->set_method('GET')
->execute()
->response();
$assertValue = $response->body->content;
$this->assertSame($expected, $assertValue);
}
}

Categories