bwrigley

bwrigley

Member Since 1 Year Ago

Experience Points 5,300
Experience Level 2

4,700 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 12
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

12 Apr
1 week ago

bwrigley left a reply on Using Laravel Cashier How Can I Access User's Subscribed Plan?

@BRAUNSON - @braunson

Thanks for your reply. Yes I'm familiar with how to get the user's subscription but it's the plan I'm interested in and then the product associated to plan.

In my set up, different products grant the user different permissions, so when I set a user's permissions I need to access the product they have purchased which would ideally be:

$user->subscription->plan->product;

I can get to the product but through a more convoluted route and was wondering if there was a better way.

Thanks!

06 Apr
2 weeks ago

bwrigley started a new conversation Laravel Cashier Access Current Plan Model

Is there a way to access the plan a User is subscribed to directly from the description?

$user->subscription('default')->stripe_plan;

gives me the id and I can look up the plan from that, but I'm curious why there isn't

$plan = $user->subscription('default')->plan;

perhaps there is something similar that I haven't spotted?

04 Apr
2 weeks ago

bwrigley left a reply on Notifications Using UUID

Just stumbled across this whilst looking for something else.

UUID is far more scalable for larger applications potentially over distributed databases

25 Mar
4 weeks ago

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

@talinon - thank you again. I was just getting the elements of code together to show you and I'm embarrassed to admit that I found out that createUser() is actually getting called by a job which is basically running in realtime so I didn't spot it!

So sorry to waste your time! Now I just need to find out how I can fake this job in every test but not other jobs. But that's a different problem!

Thank you again. So grateful.

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

@talinon so sorry to come back to this, but now I have written the full test, I seem to be having the same issue again.

Firstly, I have updated Mockery as you suggested and that is now all working fine as we had it above.

Now the real test looks like this:

        $mock = Mockery::mock(IntercomGateway::class)->makePartial();
        $mock->shouldReceive('createUser')->andReturn('mocked');
        $this->app->instance(IntercomGateway::class, $mock);

        $user = $this->createTestUser();

        $loginCredentials = [
            'email' => $user->email,
            'password' => 'wrong_password',
        ];

        $this->fromUrl(route('login'))
            ->post('/login', $loginCredentials)
            ->assertEIMErrorExists('login_fail');

the createTestUser() method sets up a user just for the purposes of the test, but also creates a new Intercom user which is the bit I want to mock as it's not relevant to the test.

However, running the above produces Original method executed as before.

So sorry to ask you again, but would appreciate your thoughts on what I might be doing wrong here.

Thank you!

22 Mar
1 month ago

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

Thank you so much, so grateful for your time. No way I would have figured that one out!

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

@talinon you genius, it works! Thank you.

However I do not understand that at all!

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

my method looks like this at the moment:

    public function createUser(User $user): void
    {

        dump('Original method executed');
        try{
            Intercom::users()->create([
                'user_id' => $user->id,
                'name' => $user->fullName,
                'email' => $user->email,
                'phone' => optional($user->profile)->getPrimaryTelephone(),
                'signed_up_at' => $user->created_at,
            ]);
        } catch (\GuzzleHttp\Exception\ConnectException $e) {
            Log::Alert('Cannot connect to Intercom ' . $e->getCode() . ' ' . $e->getMessage());
        }
    }

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

@TaI've tried yours and I get mocked too. So it's something to do with my class?

bwrigley left a reply on Using Mockery, But Original Method Still Being Called

@talinon thanks for such a fast response!

I have tried that and now I get null but I think I should get mocked no?

bwrigley started a new conversation Using Mockery, But Original Method Still Being Called

I'm sure I'm missing something really obvious here as I have mocks with Mockery working elsewhere in my code.

This is what I have in a test currently:

        $mock = Mockery::mock(IntercomGateway::class)->makePartial();
        $mock->shouldReceive('createUser')->andReturn('nothing');
        $this->app->instance(IntercomGateway::class, $mock);

        $gw = new IntercomGateway;

        dump($gw->createUser(new \App\User));

I have also added this line to the original createUser()

dump('Original method executed');

When I run the test I see Original method executed

Any thoughts on what I might have missed? Thank you

21 Mar
1 month ago

bwrigley started a new conversation 'Risky' Test

I have a feature test which is giving me this error:

Test code or tested code did not (only) close its own output buffers

which I understand can be triggered by the buffer being larger/smaller than expected. But I have no idea who to begin tracing that.

I have isolated the issue to this call in my test calls followRedirects()

        $loginCredentials = [
            'email' => $this->email,
            'password' => $this->password,
        ];

        $this->fromUrl(route('dashboard'))
            ->post('/login', $loginCredentials)
            ->followRedirects()
            ->assertViewIs('subscription.editCard');

which is a macro that I use across ~150 tests with no problem

        TestResponse::macro('followRedirects', function ($testCase = null) use ($test) {
            $response = $this;
            $testCase = $testCase ?: $test;

            while ($response->isRedirect()) {
                $response = $testCase->get($response->headers->get('Location'));
            }

            return $response;
        });

if I dump the contents of the HTML produced by my post I get a pretty standard redirect:

<!DOCTYPE html>\n
<html>\n
    <head>\n
        <meta charset="UTF-8" />\n
        <meta http-equiv="refresh" content="0;url=http://domain.test/dashboard" />\n
\n
        <title>Redirecting to http://domain.test/dashboard</title>\n
    </head>\n
    <body>\n
        Redirecting to <a href="http://domain.test/dashboard">http://domain.test/dashboard</a>.\n
    </body>\n
</html>

if I dump after the followRedirects() I see the HTML output of the view I'm expecting.

Does anyone know how I can trace the issue here?

13 Mar
1 month ago

bwrigley left a reply on How To Test Mail Sent From Notification

@nash that's the one brilliant thank you! Yes I wasn't wanting to test the actual mail was sent but couldn't figure out how to do what you have just suggested.

Thanks again!

12 Mar
1 month ago

bwrigley left a reply on How To Test Mail Sent From Notification

@nash

Thanks for your reply, and that's great for testing Notifications but Notification::assertSentTo() is channel agnostic.

So if the user's preferred channel is nexmo then Notification::assertSentTo() will be true or if the channel is 'database' then 'Notification::assertSentTo()' is also true.

I specifically want to make sure that I am routing instant notifications via the mail channel if that's the channel the user has accepted, so I need to test if an email has been sent.

I hope that makes sense.

bwrigley left a reply on Testing Notifications

In the end I have opted to create a TestNotification Notification, which feels ugly, but it does the trick.

bwrigley started a new conversation How To Test Mail Sent From Notification

My users can specify if they have Notifications sent to them instantly or to be notified daily of any pending Notifications.

This all works fine with manual testing, so now I'd like to write a feature test around this. This is what I have so far:

    public function test_instant_notification_is_sent(): void
    {
        Mail::fake();

        $this->user = $user = factory(User::class)->create();

        $this->user->update(
            [
                'notification_frequency' => 'instant',
                'notification_method' => 'email',
                'notification_email' => $this->email,
            ]
        );

        $this->user->save();

        $email = ['subject' => 'subject', 'data' => 'data'];
        $sms = 'Test Notification';
        $database = ['some' => 'data'];

        $this->user->notify(new TestNotification($email, $sms, $database));

        Mail::assertSent(MailMessage::class);

and I've written the TestNotification to go with it:

class TestNotification extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @param string[] $email
     * @param string $sms
     * @param string[] $database
     * @return void
     */
    public function __construct(array $email, string $sms, array $database)
    {
        $this->email = $email;
        $this->sms = $sms;
        $this->database = $database;
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return string[]
     */
    public function via($notifiable): array
    {
        return [$notifiable->preferredNotificationMethod()];
    }

    /**
     * @param  mixed  $notifiable
     * @return string[]
     */
    public function toDatabase($notifiable): array
    {
        return $this->database;
    }

    /**
     * @param  mixed  $notifiable
     * @return string[]
     */
    public function toNexmo($notifiable): array
    {
        return (new NexmoMessage)
            ->content($this->sms);
    }

    /**
     * @param  mixed  $notifiable
     * @return string[]
     */
    public function toMail($notifiable): MailMessage
    {
        return (new MailMessage)
            ->greeting($this->email['subject'])
            ->line('Testing Notifications!')
            ->line('Thank you for using our application!');
    }
}

So my test fails because, as I now see, MailMessage is not a mailable and I think Mail::fake() and the mail assertions only work with mailables.

Incidentally, if I remove Mail::fake() the email is sent fine.

How have others managed to test this please without actually sending the email.

bwrigley left a reply on Scheduler Caching Job?

Solved! Embarrassingly I forgot:

protected $frequency;

11 Mar
1 month ago

bwrigley left a reply on Testing Notifications

@PAULCLARKE - No problem. Thanks again!

bwrigley left a reply on Scheduler Caching Job?

So I was doing this:

$schedule->job(new NotificationsPending('morning'))->dailyAt('8:00');

which is what the docs say, and causes the error above.

however, this works instead:

$schedule->call(function (): void {
       $job = new NotificationsPending('evening');
       $job->handle();
 })->dailyAt('8:00');

bwrigley left a reply on Testing Notifications

@paulclarke

Out of interest, how do you test instant notifications? As these don't touch the database DatabaseNotification can't be used.

Thanks!

bwrigley left a reply on Testing Notifications

@borisu

Thank you, but for the functionality to test, I want to make sure that it works with or without notifications and with different user settings.

Thanks though!

bwrigley left a reply on Testing Notifications

@PAULCLARKE - Brilliant! Thank you. It was the DatabaseNotification class I couldn't find I was trying 'Notification'.

Slightly cheeky to point out that this also needs:

'notifiable_type'  => 'App\User'

in the factory or passed in on creation.

Thank you again!

bwrigley started a new conversation Testing Notifications

I have set up my project so that a user can specify if they would like their notifications instantly, daily, weekly and via sms, email, or just in their dashboard.

So if a user has said 'instantly' then I fire off an sms/email right away.

If they have said daily/weekly, then I run a job with the Scheduler which checks (at the right time) if a user has any unread notifications and send them an email/sms alerting them to how many unread notifications they have.

All good. Now I want to write a test around this.

This means I need to seed the database with some notifications against a user's account, but I can't figure out a good, clean, reproducible way to do this. I'm sure I'm just being dumb. The options I've thought of are:

  1. In my test, create a number of instances of an existing Notification class. This feels wrong as my test shouldn't expect the existence of a certain Notification class to run.
  2. Create a TestNotification class. This is doable, but seems messy and under which namespace would it be stored? Along with the other Notification classes?
  3. Seed the database directly with raw queries. Again feels messy not to use the mechanisms that are built-in for notifications, and what would I store in the type field?
  4. Write a factory around the Notification model. I tried this but couldn't get it to work, but maybe this is the correct way?

I would really appreciate your thoughts.

Thanks

Ben

08 Mar
1 month ago

bwrigley left a reply on Scheduler Caching Job?

not sure if this helps but I've noticed something else.

here are my constructor and my handle:

    public function __construct(string $frequency)
    {
        $this->frequency = $frequency;
        Log::debug('here ' . $this->frequency);
    }

    public function handle(): void
    {
        $dbUsers = DB::table('users')
            ->leftJoin('notifications', 'users.id', '=', 'notifications.notifiable_id')
            ->selectRaw('users.id as id , count(*) as pending')
            ->where('notification_frequency', $this->frequency)
            ->where('read_at', null)
            ->groupby('users.id')
            ->get();

        Log::Info('SCHEDULE JOB: NotificationsPending:' . $this->frequency . ' sending to ' . count($dbUsers) . ' users');

        foreach ($dbUsers as $dbUser) {
            $user = User::find($dbUser->id);
            $user->notify(new PendingNotifications($dbUser->pending, $this->frequency));

        }
    }

I've cleared the cache by restarting homestead to I know this is the code that's running.

The Log in the constructor works fine, but the handle fails

Undefined property: App\Jobs\NotificationsPending::$frequency which refers to the line in the query.

bwrigley left a reply on Scheduler Caching Job?

hi @snapey,

Yes it terminates when I run in tinker.

07 Mar
1 month ago

bwrigley started a new conversation Scheduler Caching Job?

Please forgive me if this is an obvious question but does the scheduler cache classes? Or perhaps unique to using homestead?

The reason I ask is that my scheduler runs a job which has standard __construct() and handle() methods.

If I change something in the __construct() method like add a logging command, it logs fine next time the scheduler runs. However, if I change code in the handle() method nothing changes until I reboot homestead.

I can't find anyone else with this issue. Am I missing something obvious?

Thanks!

06 Mar
1 month ago

bwrigley left a reply on Selecting Model On Join With Count

@JLRDW - ah! I wasn't grouping! Thanks (which means I don't need to query pending)

bwrigley left a reply on Selecting Model On Join With Count

@ANONYMOUSE703 - no it's not but it sql you can usually query against an aliased field I thought

bwrigley left a reply on Selecting Model On Join With Count

@RDELORIER - no pending column, only the alias created in the statement

05 Mar
1 month ago

bwrigley started a new conversation Selecting Model On Join With Count

I'm sure I'm missing something obvious in eloquent here.

My users can set one of 5 notification frequencies instant,morning,afternoon,weekly,never

I want to set up a morning, afternoon and weekly job to notify the user of how many unread notifications they have to view.

so my morning job currently looks like this:

    public function handle(): void
    {
        $users = User::where('notification_frequency', 'morning');

        foreach ($users as $user) {
            if (count($user->unreadNotifications) !== 0) {
                //alert user to how many notifications are pending
            }
        }
    }

but I feel I should be able to do this efficiently in one query e.g.

    public function handle(): void
    {
        $users = DB::table('users')
            ->leftJoin('notifications', 'users.id', '=', 'notifications.notifiable_id')
            ->selectRaw('count(notifications.id) as pending')
            ->where('notification_frequency', 'morning')
            ->where('read_at', null)
            ->where('pending', '>', 0)
            ->get();

        foreach ($users as $user) {
            //notify the user of pending notifications
        }
    }

however this gives a SQL error of Unknown column 'pending' in 'where clause'

Can someone show me how to do this better in eloquent?

(and perhaps it's not more efficient to do it this way anyway and I should stick with first option which is more readable?)

01 Mar
1 month ago

bwrigley left a reply on Starting ChromeDriver Manually

I can't explain why, but it seems that if I use the full path, then everything works fine.

/home/vagrant/Code/mysite/vendor/laravel/dusk/bin/chromedriver-linux --port=9515
26 Feb
1 month ago

bwrigley started a new conversation Starting ChromeDriver Manually

I'm writing a little artisan command line tool that I need to browse my site with javascript execution.

Initially I was calling:

```\Laravel\Dusk\TestCase::startChromeDriver()````

This started the driver fine running on port 9515 running as user vagrant (I'm using homestead)

But when I tried to use it I would get an error:

Failed to connect to localhost port 9515: Connection refused

If I started it manually on the command line:

$ ./vendor/laravel/dusk/bin/chromedriver-linux --port=9515

The driver started fine again running on port 9515 as user vagrant, but this time my tool runs fine.

I want to be able to start the ChromeDriver from within the tool. So I also tried:

         $process->start(); ```

Connection was refused again.

Any idea why this works when I start it from the command line but not within my artisan command
10 Feb
2 months ago

bwrigley started a new conversation Caching Issues During Feature Tests

I'm having an issue with what I think is caching during a unit test.

The block of my test that I'm struggling with is:

$subscriptionDetails = [
        'plan' => $plan->nickname,
        'stripeToken' => $token,
    ];

$this->post('/subscription/create', $subscriptionDetails)
        ->assertEIMSuccessExists('stripe_subscribe_success')
        ->followRedirects()
        ->assertViewIs('dashboard');

$this->assertTrue($this->user->currentPlan() === $plan->nickname);

assertEIMSuccessExists() is just a little macro that checks for a custom message in the session - this passes anyway.

the last two assertions fail:

 -'dashboard'
+'subscription.edit'

This is because some middleware checks if the user has a current subscription, if not redirects them to the subscription page.

Now if I alter the test thus:

$subscriptionDetails = [
        'plan' => $plan->nickname,
        'stripeToken' => $token,
    ];

$this->post('/subscription/create', $subscriptionDetails)
        ->assertEIMSuccessExists('stripe_subscribe_success');

$this->user = $this->user->fresh();

$this->assertTrue($this->user->currentPlan() === $plan->nickname);

everything passes fine. Presumable because it's cached. Ok I could just leave it like the latter, but then I can't test if the final view is the correct one, so what I really need is something like:

$this->post('/subscription/create', $subscriptionDetails)
        ->assertEIMSuccessExists('stripe_subscribe_success')
        ->clearCaches() //or similar
        ->followRedirects()
        ->assertViewIs('dashboard');

$this->assertTrue($this->user->currentPlan() === $plan->nickname);

but I have no idea how to write that macro, if it could even work that way.

Would love your thoughts!

30 Jan
2 months ago

bwrigley left a reply on Feature Test Fails While Dusk And Manual Test Pass

More info:

I have managed to replicate this in tinker

if I run

Sentinel::authenticate(['email' => '[email protected]', 'password' => 'wrong-password'])

I get false as expected

If I run it 10 times, I still get false each time

if I then restart tinker and run the command again, I get the throttling exception

29 Jan
2 months ago

bwrigley started a new conversation Feature Test Fails While Dusk And Manual Test Pass

I am having a curious issue with a feature test not working as expected.

With Sentinel you can set login throttling, which I have set to 5 attempts, on the 6th attempt a ThrottlingException is thrown which I catch and set a different error message. This works fine manually in the browser, or in a Dusk test.

The last assertion always fails while the others in the foreach all pass no matter how high I set $loginAttempts to.

However, if I head over the browser after this test has run and try to login with the same credentials I get the correct error message and the correct line in my log file is generated.

All the records that Sentinel captures for throttling are in the DB and present and correct.

All my own log files show that the correct final error is never triggered by the test.

    public function test_wrong_login_locks_account()
    {

        $loginCredentials = [
            'email' => '[email protected]',
            'password' => 'wrong_password'
        ];

    $loginAttempts=5;

        foreach (range(0, $loginAttempts) as $i) {
            $this->fromUrl(route('login'))
            ->post('/login', $loginCredentials)
            ->assertEIMErrorExists('login_fail')
            ->followRedirects();
        }

        $this->fromUrl(route('login'))
        ->post('/login', $loginCredentials)
        ->assertEIMErrorExists('login_locked');

    }

'assertEIMErrorExists()is a macro to check the session for the existence of a custom messagefollowRedirect()` is a macro to follow any redirect commands (needed to trigger the session flash)

  • these are both used in many other tests and are working fine.

I appreciate this is not a straightforward one to explain, so please ask me any questions you have, Thanks!

25 Jan
2 months ago

bwrigley left a reply on Impersonator Login

yeah that sounds like a good plan. Thanks again!

bwrigley left a reply on Impersonator Login

@snapey this is a great implementation thank you. If you don't mind I shall steal it for mine. We have Sentinel but it should be relatively easy to swap out.

Just out of interest, in your example, when I am in impersonation mode, is there a way to access my real userid for logging and audit purposes or is this something separate you would store on the session?

bwrigley left a reply on Impersonator Login

@rodrigo.pedra thanks! I don't know how I missed this. I shall watch it to get get the full detail.

bwrigley started a new conversation Impersonator Login

We've got three user types on our system, guest, customer and admin.

I'd like for admins to be able to proxy login as a customer (guests don't count) so that they can see exactly what the customer would see.

At the moment I'm thinking about simply logging the admin in as the customer and having a session variable impersonatedBy which is the admin's userID to be carried around the whole session and used to log any actions the admin takes etc.

Before I went ahead with that route, I was just wondering if there is a known 'best practise' with how this is done as I don't want to reinvent the wheel.

15 Jan
3 months ago

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@adamprickett that was truly lazy of me

the answer is that I need to collect the second argument of listener:

         $this->app['events']->listen('eloquent.created: *', function ($eventName, $model) {

             $this->registerModel($model[0]);
         });

thank you!

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@adamprickett at the risk of abusing your generous time (and of taking this thread off topic), I'm just trying to implement your elegant solution and I seem to be storing a log of model types rather than references to the models themselves.

so if just before I destroy I dump $this->testModels I get:

array:6 [
  0 => "eloquent.created: App\User"
  1 => "eloquent.created: Cartalyst\Sentinel\Activations\EloquentActivation"
  2 => "eloquent.created: Cartalyst\Sentinel\Persistences\EloquentPersistence"
  3 => "eloquent.created: App\User"
  4 => "eloquent.created: Cartalyst\Sentinel\Activations\EloquentActivation"
  5 => "eloquent.created: Cartalyst\Sentinel\Persistences\EloquentPersistence"
]
14 Jan
3 months ago

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@adamprickett ooh nice! I shall have a play thank you! So much still to learn....

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

Thank you! That's the one:

optional($model->fresh())->delete();

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@adamprickett yeah I thought that too. However when I tested it only tests that passed seemed to get cleaned up, I'd always have left over data. I thought I read that was a lot of people's experiences? If not, I'm not sure why mine was different...

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@adamprickett yes I had thought that, but I'm doing this a bit 'wrong' in that I'm working with a large dataset that I need to test with and don't want to refresh whilst coding the tests (if that makes sense).

And the rollback methods that already exist only executes when the test actually passes.

I appreciate this is unusual practice though, hence the annoying question!

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@realrandyallen I thought you had it there! Seems `optional()' helper also thinks it still exists as it's in memory.

bwrigley left a reply on How To Tell If A Model's DB Record Still Exists?

@realrandyallen yes I spotted that and agree that's probably the easiest way. Just wondering if there was anything more elegant. Only because I won't actually do anything in the catch...

bwrigley started a new conversation How To Tell If A Model Still Exists?

I've written a little helper class for my feature tests so that when I'm setting up a test and creating a bunch of models that I need, I add them all to an array:

    {
        array_push($this->testModels, $model);
    }

doesn't matter what the models are.

then in my tearDown() I destroy them all:

            $model->delete();
        }

this is working find, but I've hit a snag.

if, in the interim, the model's record has been removed in the database I get an exception. So I'm wondering how to tell if a model's record still exists in the DB before trying to run delete()

sadly $model->exists still returns true. Anyone know of another way?

Thanks

09 Jan
3 months ago

bwrigley left a reply on Feature Test Always Gets 'session Expired' Message

Just a final update on this and the reason I was having to instantiate the Session.

I am using Homestead which my default in it's config file has

variables:
    - key:  APP_ENV
      value: local

So even though my .env file was:

APP_NAME=Laravel
APP_ENV=testing

and my phpunit.xml had

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>

$~ php artisan env

always showed as local and it needs to be testing

Changing the homestead.yaml file and running vagrant provision fixed the issue.

Thanks again!

bwrigley left a reply on Feature Test To Replicated Delete Method In Route Model Binding

Ah yes I see thank you. I thought there was no delete() helper.

However, running this now gives me a TokenMismatchException so presumably I need to include my token in the delete request also?