Unit testing with PHPUnit: Testing a User model (4/10)

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ค. 2024
  • Want more? Explore the library at www.codecourse.com/lessons
    Official site
    www.codecourse.com
    Twitter
    / teamcodecourse

ความคิดเห็น • 42

  • @m41kdevelops41
    @m41kdevelops41 5 ปีที่แล้ว +29

    This tutorial is awesome thanks for sharing such info.
    Just an update; if you're using a newer version of phpunit then the classes are namespaced now so instead of \PHPUnit_Framework_TestCase it should be \PHPUnit\Framework\TestCase

  • @paulkonietzny4991
    @paulkonietzny4991 4 ปีที่แล้ว +2

    pure gold. thank you!

  • @urospopovic7756
    @urospopovic7756 4 ปีที่แล้ว +1

    So clean i love it.

  • @rsonea
    @rsonea 3 ปีที่แล้ว

    U are great ....U have been educating us for years.....u are just awesome ....

  • @takdzwiedz
    @takdzwiedz 3 ปีที่แล้ว +1

    I love these voices of children playing outside in the background :)

    • @Cursos7Br1
      @Cursos7Br1 2 ปีที่แล้ว

      True, really amazing!

  • @omotayoiginla1270
    @omotayoiginla1270 5 ปีที่แล้ว

    awesome tutorial so far..

  • @dianaeftaiha
    @dianaeftaiha 2 ปีที่แล้ว +5

    Does anyone know why I need to run composer dump-autoload every time I create a new class or I get a "class not found" error?

    • @sebastiankargl
      @sebastiankargl 2 ปีที่แล้ว

      you have to run the dump-autoload command which won't download anything new, but looks for all of the classes it needs to include again. It just regenerates the list of all classes that need to be included in the project (autoload_classmap.php).

  • @inambe3987
    @inambe3987 5 ปีที่แล้ว +1

    I think we should revert the positions of values in assert function for better failure reporting.
    Example:
    this is how we should do ...
    $this->assertEquals('Rabit', $user->getLastName());
    and NOT in this way ...
    $this->assertEquals($user->getLastName(), 'Rabit');

    • @mihrdat
      @mihrdat 5 ปีที่แล้ว

      spot on, I was going to mention the same

  • @hitwill
    @hitwill 4 ปีที่แล้ว

    Neat lessons, thanks

  • @mirlaniusUMK
    @mirlaniusUMK 4 ปีที่แล้ว

    thank you so much!!!

  • @m41kdevelops41
    @m41kdevelops41 5 ปีที่แล้ว

    I have a question, instead of checking if the emailVariables array contain keys then checking if each key contains the correct value; can we check if the emailVariables array equals the expected array?
    here's a sample of what I mean:
    $emailVariables = $user->getEmailVariables();
    $this->assertEquals( $emailVariables, [
    'full_name' => 'Billy Garrett',
    'email' => 'billy@codecourse.com'
    ] );
    isn't that the same or am I missing something ?

  • @davidcarmean5132
    @davidcarmean5132 3 ปีที่แล้ว

    I'm new to PHP (got thrown a maintenance project) but I want to say that when I saw/heard your first test case name began with "testThat..." I did a little dance in my chair! I've seen so many test cases with names that don't really tell the reader what to expect; this makes it so much easier for somebody new to a project to figure out what's going on... or when you go back to a project after your brain stack has overflowed with years (or months) of other projects. Bravo!

  • @kristapsvecvagars5049
    @kristapsvecvagars5049 4 ปีที่แล้ว

    Just wondering - I purposefully didn't declare "public $email;", but the test ran green. Shouldn't it throw an error about an undeclared variable or something?

    • @LeonardoAraujo-re7oj
      @LeonardoAraujo-re7oj 4 ปีที่แล้ว

      In a language such as C, C++, and Java the programmer must declare the name and type of the variable before use it. In PHP the type of the variable does not need to be declared before use it because types are associated with values rather than variables. As a result, a variable can change the type of its value as much as we want.

  • @toumalmojumder9487
    @toumalmojumder9487 6 ปีที่แล้ว +1

    I am stuck in 5:29 .....
    C:\xampp\htdocs>phpunit
    PHPUnit 3.7.21 by Sebastian Bergmann.
    Configuration read from C:\xampp\htdocs\phpunit.XML
    PHP Fatal error: Declaration of PHPUnit_Framework_Comparator_DOMDocument::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false) must be compatible with PHPUnit_Framework_Comparator_Object::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false, array &$processed = Array) in C:\xampp\php\pear\PHPUnit\Framework\Comparator\DOMDocument.php on line 114
    Fatal error: Declaration of PHPUnit_Framework_Comparator_DOMDocument::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false) must be compatible with PHPUnit_Framework_Comparator_Object::assertEquals($expected, $actual, $delta = 0, $canonicalize = false, $ignoreCase = false, array &$processed = Array) in C:\xampp\php\pear\PHPUnit\Framework\Comparator\DOMDocument.php on line 114

    • @ScimonInTheSkies
      @ScimonInTheSkies 6 ปีที่แล้ว +3

      try to update the version of phpunit from 3.7.21 to 5.6.3 like in this video or to 7.1.4 (or kind of).
      if you update the version to 7.1.4 you have to extend "PHPUnit\Framework\TestCase" instead of "PHPUnit_Framework_TestCase". Also if you don't update the version 'globally' but just with composer in that folder, you should run "./vendor/bin/phpunit" instead of just phpunit ... so far I solve the color issue just by adding --colors=always

    • @toumalmojumder9487
      @toumalmojumder9487 6 ปีที่แล้ว

      thanks for your reply .... i will try soon....

    • @scottverbeek2072
      @scottverbeek2072 5 ปีที่แล้ว

      তমাল মজুমদার Did you manage to fix the issue?

    • @andrewwwlife
      @andrewwwlife 5 ปีที่แล้ว

      @@scottverbeek2072 stackoverflow.com/questions/42696087/phpunit-version-3-7-21-instead-of-version-6-installed here is great solutions, see the answer for xampp issue :)

  • @bandwidth_fm
    @bandwidth_fm 5 ปีที่แล้ว +6

    At 3:33 I still get the error: Class 'App\Models\User' not found
    I use 6.5.13 version of phpunit
    Thanks

    • @ickokid
      @ickokid 5 ปีที่แล้ว +8

      require __DIR__ . "/../app/Models/User.php"; in file UserTest.php

    • @henriquepena92
      @henriquepena92 3 ปีที่แล้ว

      @@ickokid Thank you very much!!

  • @ondottr
    @ondottr 4 ปีที่แล้ว +1

    With PHPStorm, this course could be much faster than now...
    But thank you for awesome tutorial

  • @bobsort
    @bobsort 3 ปีที่แล้ว

    Cool! now write a test to test your test! ;)

  • @jan42
    @jan42 5 ปีที่แล้ว +7

    Learning to test is as easy as learning to draw owls. Step 1: Test if there are some simple variables, Step 2: Test the f*cking rest of your code.

  • @mioszrycko-bozenski4504
    @mioszrycko-bozenski4504 7 ปีที่แล้ว +1

    Hi. Correct me, if I'm wrong. In example: my full name would be Milosz Rycko Bozenski, Rycko Bozenski is last name. In this case trimming last name would remove space between Rycko and Bozenski. So test would fail. I think that assuming that last name is always one-part should be corrected.
    The same scenario with first name. They are people using double first names, i.e. Milosz Jan Rycko-Bozenski. So trimming test would fail, am I right?

    • @AminaKombo
      @AminaKombo 7 ปีที่แล้ว

      Do ltrim() and rtrim() work in this case?

    • @AminaKombo
      @AminaKombo 7 ปีที่แล้ว

      Oh, wait:
      public function setFirstName($string)
      {
      $namestuff=explode(' ',$string);
      $fname=implode(' ',$namestuff);
      $this->firstname=$fname;
      }

    • @noisevector
      @noisevector 6 ปีที่แล้ว +5

      trim() only removes spaces left and right of the string. Not in the middle.

  •  2 ปีที่แล้ว

    I am kinda annoyed that EXPECTED and ACTUAL are always reversed in the assertions .. xD - Otherwise good stuff thanks.

  • @YuraNarozhnyi
    @YuraNarozhnyi 6 ปีที่แล้ว +10

    I think to test a getter and a setter is a stupid thing, nobody doesn't write tests for getter and setters

    • @allenallada6754
      @allenallada6754 5 ปีที่แล้ว +7

      I think the purpose of the video is to show the procedure as simple as possible. People with less experience will have a problem understanding the content if he start with something complex.

    • @hattrickster33
      @hattrickster33 5 ปีที่แล้ว

      @@allenallada6754 Agreed. Getters and setters are the simplest methods so even someone who is new to PHP or OO programming in general will be able to understand the concept of how unit testing is done here.
      Besides, I actually think it is useful to write some quick unit tests for your getters and setters. It is extremely easy to do but it might save you a lot of trouble later when you overlook a simple error. These get overlooked because you start to think you are now an experienced programmer who will never make that kind of mistake. Until you do and you feel like an idiot because you overlooked it.

    • @jamesfoo8999
      @jamesfoo8999 5 ปีที่แล้ว

      @@hattrickster33 The reason it's pointless is because getters and setters generally have no logic etc, they literally just set or get the object property. If there's a bug in a getter/setter than you fail as a developer as all you need to do is test it gets or sets what you expect and that's it. No point in having a test for it. The class or object could fail somewhere more complex, but this is where you test, once that's all ok, a getter or setter is simple to "check" just as part of writing code.
      IMO

    • @kristapsvecvagars5049
      @kristapsvecvagars5049 4 ปีที่แล้ว +3

      I believe you completely missed the point of the video. While testing getters and setters might be redundant, it is also a simple and easy to understand process, perfect for beginners like myself, who learned PHPUnit even exists about 15 minutes ago.

    • @jack-sx9bw
      @jack-sx9bw 4 ปีที่แล้ว

      stupid? I think that you are the one that stupid here to not understand the purpose of those videos!

  • @justinasbei
    @justinasbei 2 ปีที่แล้ว

    Guys, don't waste my time! Why not follow 80/20 rule and test what's important? Age calculation from birth-date is important. Getting and setting "Jon Doe" is not important, because you'll see it working or not working in hours, days, weeks to come. Give me a break!