Jump to content

fastsol

Moderators
  • Content Count

    796
  • Joined

  • Last visited

  • Days Won

    9

fastsol last won the day on July 26 2015

fastsol had the most liked content!

Community Reputation

39 Good

About fastsol

  • Rank
    Prolific Member
  • Birthday 10/17/1978

Contact Methods

  • Website URL
    http://amecms.com

Profile Information

  • Gender
    Male
  • Location
    Minnesota

Recent Profile Visitors

203,254 profile views
  1. This is the piece of information that made it work how I want in all environments. I now understand that in the boot() it was just adding the scope to the list but not actually running it at that moment. By putting the admin/* in the apply() of the scope itself the request()->path() had been populated and worked as expected in testing also. Thank you so very much, I've been beating my head for a while on this as I kept coming back to it when I'd make a new test that it was affecting. This is what I ended up with: class IsActiveScope implements Scope { /** * @param Builder $builder * @param Model $model * @return Builder|void */ public function apply(Builder $builder, Model $model) { if(!request()->is('admin/*')) { return $builder->where($model->getTable() . '.active', '=', 1); } return $builder; } } And just this in the boot of the model: public static function boot(){ parent::boot(); static::addGlobalScope(new IsActiveScope()); }
  2. It appears that I can set a static method within the unit test itself that does transfer over when doing $this->get(route(''blah));. So I should be able to do the middleware method as you suggested, but then manually set the $enabled to false within the test before using $this-get(). This is a workaround I guess, but it still feels icky since I having to go around the actual code that runs the site normally to make it work just for the test. Maybe it's a compromise I'll have to live with. I had previously tried your last suggestion with the env settings and that made it work in reverse where now the customer side of the code was failing and the admin side was passing because I still couldn't set a true of false value based on a condition, the env value was just set as soon as the test started. I know I'm no where near a "great" coder, especially with Laravel as this is only my second site using it, so many of these little quirks about testing and env are slightly new to me.
  3. Ok so I was wrong, my session method was not working either. I'm starting to see the issue here though. When I run the site normally the middleware is triggered BEFORE the route model binding, but when unit testing the middleware is triggered AFTER the route model binding. I have confirmed this by die dumping in multiple places in the chain to see which gets triggered first. The results are as I stated above, unit testing the model binding is first in the chain which is why no matter what I do here it will likely fail cause I am not able to set any variable/class/anything beforehand to tell the model not to use the scope. Like I said originally, this is only an issue during testing, and really only makes it so I can't test 1 thing on certain models on either the customer side or admin side depending on when the scope is being applied. So maybe I just give up on this and know that that 1 specific test can't be handled through unit testing. I may end up doing Dusk anyway, so it would be tested then correctly.
  4. I've been testing the last 30 minutes by making 2 middleware that uses session() to set the decision to add the scope. Session seems to be the only way to retain the decision throughout the application due to varying class scopes (not the model scope) and such. So far it's working in production and the quick unit tests I ran. I have to do some more testing to ensure it's working as I need, but so far it looks good. Thank you very much, I just needed an idea of how else to set the decision besides the request() and you helped me there.
  5. The way I have it now works great in production, although I do understand it being weird to manage it the way I have. The ONLY time it doesn't work is during unit testing. I like your way of applying it but I don't think it will solve the original issue I posted about. Reason being (primarily) is that when using route model binding, this would still fail to set I believe cause route mode binding appears to apply it's scopes at the route service provider level which is before the middleware level and hence would be too late to NOT apply the scope. Hopefully that made sense. Any other suggestions or maybe I'm wrong, but I feel I'm not after all the testing and messing around trying to make it work for several hours already.
  6. I am having a hard time figuring out a way to set the pathInfo within the request when testing. Here is the scenario: I am adding a global scope to a model in the boot() of that model that is conditional based on the route that given. This works great under normal usage but testing it fails the condition because the pathInfo within the request is only "/". What I don't understand is that AFTER the response is returned from the $this->get() in the test, the request has all the correct data, but it is NOT populated beforehand at the route model binding level. It is populated by the time it hits the middleware, but that is too late to attach or not attach a global scope for a route model binding situation. The page in question is protected behind auth middleware, so the $this->fakeUser(); is simply a quick function to be() a user; Here is the model code: public static function boot(){ parent::boot(); if(!request()->is('admin/*')){ static::addGlobalScope(new IsActiveScope()); } } As you can see the code is just checking if the route is "admin/*", so any admin pages essentially. But not matter what I have tried, the route always comes through as "/" when it hits that piece of code and the route model binding level. If you do a dd(request()); AFTER the $response in the test, the route has been populated at that point and is correct. This is the test: /** @test */ public function see_correct_active_icons_for_each_brand() { $this->fakeUser(); $brand1 = factory(Brand::class)->create(['name' => 'A', 'active' => true]); $brand2 = factory(Brand::class)->create(['name' => 'B', 'active' => false]); $brand3 = factory(Brand::class)->create(['name' => 'C', 'active' => true]); $response = $this->get(route('admin.brands.index')); $response->assertSeeInOrder([$brand1->displayStatus(),$brand2->displayStatus(), $brand3->displayStatus()]); } How can I fake this or is there another way to add the global scope in the manner that works like I have it now? @requinix I see that you know a good amount about Laravel, any insight?
  7. Thank you for the explanation. It's certainly something I've not encountered. This was supposed to be a simple sales tax couple functions that someone found that under certain sets of numbers it was off by 1 cent cause it wasn't rounding up for those instances. I messed around a bit more and got this to work so far and seems to product the correct amounts for a few different values. The functions are identical to what I was running before except I changed the 2nd param of the round() to a 3 instead of 2. Unless you feel this will cause other major issues, I'll probably run with this until I can implement a money type library. // Returns formatted number with sales tax added function addSalesTax($num, $rate) { return number_format(round(($num * $rate), 3), 2); } // Returns just the tax amount function calculateTax($num, $rate) { return number_format(round((($num * $rate) - $num), 3), 2); } $num = 635.00; $num2 = 583.00; $rate = 1.05500; echo addSalesTax($num, $rate); echo '<br>'; echo calculateTax($num, $rate); echo '<br><br>'; echo addSalesTax($num2, $rate); echo '<br>'; echo calculateTax($num2, $rate);
  8. I'm fighting with a very simple round() that isn't giving me the correct output. In the example below the first var_dump shows the value of the calculation performed. The calculation is correct. Next it runs through the round() native function and comes out incorrect. Then I run a round() again but set another var to the same value as the first calculation but by simply setting the number rather than letting php do the math. Then run it through the round() and it comes out to what I expect it should be. So why is the first one not coming out right? $num = 583.00; $rate = 1.05500; $t = (($num * $rate) - $num); var_dump($t); //comes out 32.065 float CORRECT $just_tax = round($t, 2); var_dump($just_tax); //comes out 32.06 float INCORRECT should be 32.07 $t2 = 32.065; //Explicitly set the same value as $t above $just_tax2 = round($t2, 2); var_dump($just_tax2); //comes out 32.07 float CORRECT
  9. Yes the id was the same no matter what I did. That was one of my thoughts too, but led me nowhere.
  10. I don't think this has anything to do with it, but there are some new lines in my htaccess that CPanel added during an update, but even if I comment out the lines it still doesn't work. ########################### RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC] RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L] ########################### ########################### RewriteCond %{SCRIPT_FILENAME} !-d RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule ^location/(.*)$ ./location-details.php?loc=$1 ########################### RewriteCond %{QUERY_STRING} ^product_id=(.*)$ RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule . %{REQUEST_URI}?product-id=%1 [R=301,L] RewriteBase / RewriteCond %{HTTP:Cookie} devicePixelRatio [NC] RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule \.(?:jpe?g|gif|png|bmp)$ /retinaimages.php [NC,L] RewriteCond %{REQUEST_FILENAME} !-s RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$ RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L] It's these 2 lines in each rewrite rule RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$ RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
  11. path = / domain = the correct domain. no www. my htaccess redirects everything to non www http_only = no I was able to resolve the issue for the moment by moving a couple files to the root, at least the site works now. This is not ideal and doesn't answer why this happened though. So I still would like any suggestions. I can't find anything wrong with any setting or anything. It worked last night and then didn't this morning. I totally understand that something had to have happened, I just can't find it.
  12. Also there is no htaccess restricting the viewing in the folder I'm trying to put the js file, or any other type of restriction that I know of since I can execute other php code in the file, just not session data.
  13. I'm getting somewhere I think. If I move the js file to the root folder it works. But if I have it at any level above root it won't pick up the session. What would cause that?
  14. You can visit the file in the script tag directly in the browser and there is no output in the file.
  15. I just changed the php version on the server to 7.1 like my local has and that made no difference.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.